我有两个需要合并的选择查询....
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的值
我该怎么做?
答案 0 :(得分: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
然而,这将乘以行数,每个日期一行。您是否有选择其中一行的具体规则?