Sql 将两个select查询连接在一起
我需要合并两个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 [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
但是,这将使行数相乘,每个日期一行。您有选择其中一行的特定规则吗