2012-10-05 3 views
0

내가이 선택 쿼리를 가입 함께

SELECT [DatapointID] 
    ,[AssetID] 
    ,[DatapointID] 
    ,[SourceTag] 
    ,'-' + [TimeStep] + [TimeStepValue] AS TimeStep 
    ,[DataRetrievalInterval] + [DataRetrievalIntervalValue] AS [RetInterval] 
    ,NULL AS DatapointDate 
    ,NULL AS DatapointValue 
    ,0 As DataFlagID 
    ,DateADD(-1, d @SearchDateEnd) + @SearchTimeEnd DateDataGrabbed 
    ,GetDate() DateTimeAddedtoDB 
FROM [dbo].[tblTSS_Assets_Datapoints] 
Where AssetID = 204  


Select DatapointDate, SUM(DataPointValue) as DataPointValue From @temp 
GROUP BY DatapointDate 
ORDER BY DatapointDate 

첫 번째 선택 쿼리 내가 최종 결과는 그러나 대신 DatapointDate으로 NULL이어야 할 것입니다 내가 결합해야 두 가지 선택 쿼리를 .... 가지고 DatapointValue @ temp의 값을 원합니다.

어떻게해야합니까?

답변

1

조인은 두 테이블의 값을 결합합니다.

SELECT [DatapointID], [AssetID], [DatapointID], [SourceTag], 
     '-' + [TimeStep] + [TimeStepValue] AS TimeStep, 
     [DataRetrievalInterval] + [DataRetrievalIntervalValue] AS [RetInterval], 
     d.DatePointDate, d.DatapointValue, 
     0 As DataFlagID, 
     DateADD(-1, d @SearchDateEnd) + @SearchTimeEnd DateDataGrabbed, 
     GetDate() DateTimeAddedtoDB 
FROM [dbo].[tblTSS_Assets_Datapoints] cross join 
    (Select DatapointDate, SUM(DataPointValue) as DataPointValue From @temp 
     GROUP BY DatapointDate 
    ) d 
Where AssetID = 204 

이, 그러나, 각 날짜에 대해 하나의 행 수를 곱합니다 : 당신이 십자가에 가입 할 것이다, 그래서이 경우, 명백한, 키가 조인하지 않습니다. 행 중 하나를 선택하기위한 특정 규칙이 있습니까?

관련 문제