I have this table:
+----+-------+-------------------+
| ID | Value | Date |
+----+-------+-------------------+
| 1 | 8 | 12/10/2015 8:00am |
| 1 | 10 | 12/10/2015 8:30am |
| 1 | 5 | 12/10/2015 9:00am |
| 1 | 11 | 12/10/2015 9:30am |
| 2 | 8 | 12/10/2015 8:00am |
| 2 | 6 | 12/10/2015 8:30am |
| 2 | 8 | 12/10/2015 9:00am |
| 2 | 9 | 12/10/2015 9:30am |
+----+-------+-------------------+
etc.
Now I have a query like this (simplified for clarity):
SELECT ID, MAX(Value), MIN(Date)
FROM Values
WHERE ID IN (1,2,3,8)
GROUP BY (DATEPART(MINUTE, Date) / 60), ID
ORDER BY ID, MIN(Date)
Which returns something like this:
+----+-------+-------------------+
| ID | Value | Date |
+----+-------+-------------------+
| 1 | 10 | 12/10/2015 8:00am |
| 1 | 11 | 12/10/2015 9:00am |
| 2 | 8 | 12/10/2015 8:00am |
| 2 | 9 | 12/10/2015 9:00am |
| 3 | 12 | 12/10/2015 8:00am |
| 3 | 6 | 12/10/2015 9:00am |
| 8 | 51 | 12/10/2015 8:00am |
| 8 | 58 | 12/10/2015 9:00am |
+----+-------+-------------------+
I need to calculate the sum of the Value field if, say, the identifier is 1, 2 or 3.
I expect something like this:
+------------+-------+-------------------+
| IDfield | Value | Date |
+------------+-------+-------------------+
| IDstring1 | 30 | 12/10/2015 8:00am |
| IDstring1 | 26 | 12/10/2015 9:00am |
| IDstring2 | 51 | 12/10/2015 8:00am |
| IDstring2 | 58 | 12/10/2015 9:00am |
+------------+-------+-------------------+
Or even better:
+--------+--------+-------------------+
| Value1 | Value2 | Date |
+--------+--------+-------------------+
| 30 | 51 | 12/10/2015 8:00am |
| 26 | 58 | 12/10/2015 9:00am |
+--------+--------+-------------------+
The identifiers that I need to summarize are static, so I think I can go for one of these solutions, but I can’t figure out how to do this.
I will try to better explain the expected result:
Value1 should contain the SUM of the Value field, where ID IN (1, 2, 3) are grouped by the clock, as I already did in my query,
Value2 should just contain the Value field, where ID = 8
: , ( MAX() Value SELECT)
# 2: