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

第一个选择查询是我想要的最终结果,而不是 NULL 作为 DatapointDate 和 DatapointValue 我想要来自 @temp 的值

我怎样才能做到这一点?

4

1 回答 1

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  

但是,这将增加行数,每个日期一个。您是否有选择其中一行的特定规则?

于 2012-10-05T17:35:06.587 回答