Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将两个select查询连接在一起_Sql_Sql Server 2005 - Fatal编程技术网

Sql 将两个select查询连接在一起

Sql 将两个select查询连接在一起,sql,sql-server-2005,Sql,Sql Server 2005,我需要合并两个select查询 SELECT [DatapointID] ,[AssetID] ,[DatapointID] ,[SourceTag] ,'-' + [TimeStep] + [TimeStepValue] AS TimeStep ,[DataRetrievalInterval] + [DataRetrievalIntervalValue] AS [RetInterval] ,NULL AS DatapointDate ,NULL AS Datap

我需要合并两个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
第一个select查询是我想要的最终结果,而不是NULL,因为DatapointDate和DatapointValue是我想要的@temp中的值


如何执行此操作?

联接将组合两个表中的值。在这种情况下,没有明显的连接键,因此您将具有交叉连接:

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  
但是,这将使行数相乘,每个日期一行。您有选择其中一行的特定规则吗