I have the following query:
SELECT d.currentDate, t.ConfigId, t.value as value
FROM #Dates AS d
OUTER APPLY
( SELECT t.value as value, t.FK_ShipDataSubsystemConfigId
FROM myTable AS t
WHERE t.[Timestamp] <= d.currentDate and t.ConfigId in (' + @IDList + ')
and t.FK_DataSystemId <> 1
) AS t
GROUP BY d.currentDate, t.ConfigId
)
where Dates
is just a table holding a bunch of datetimes I am using to ensure I get the data at the intervals I need.
My problem is with the group by
clause. The query doesn't work as is because value
is not in the group by
or an aggregate function. I tried grouping by value
also to make the error go away, but it just gives me every single date in the interval I am selecting on matched to every single value -- not what I want. I should end up with a table with one row for each date/ConfigId pairing.
When I remove value from the select
and just get the date and the ConfigId
, it works fine. I get the exact number of rows I should be getting.
The table I'm pulling from looks like this:
PK_DataId Timestamp value ConfigId
1 1/1/2015 12:00 234 5
2 1/1/2015 12:01 456 4
I am expecting to get back this:
Timestamp value ConfigId
1/1/2015 12:00:00 234 5
1/1/2015 12:00:00 456 4
Where I have a value for each configId/date pair from every fifteen minutes. When I add max(value) I only get one value for every time instead of different ones. When I group by value I get millions of rows, it looks like I am getting one row for each timestamp matched with each value from any other timestamp. I don't really understand what is happening.
How can I get those results while also selecting value
?