2012-10-05 161 views
0

我有我需要合併兩個SELECT查詢....加入兩個選擇查詢一起

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 

的第一選擇查詢是什麼,我想最終的結果是的但不是null作爲DatapointDate和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 

然而,這將成倍增加的行數,一個每個日期。你有選擇其中一行的特定規則嗎?