Reputation: 795
I have two select queries that I need to combine....
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
The first select query is what I want the end result to be however instead of NULL As the DatapointDate and DatapointValue I want the values from the @temp
How can I do this?
Upvotes: 0
Views: 226
Reputation: 1269463
A join will combine the values from the two tables. In this case, there is not an obvious join key, so you would have a cross join:
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
This, however, will multiply the number of rows, with one for each date. Do you have a specific rule for choosing one of the rows?
Upvotes: 1