The value of line strings, if their identifiers are in a certain range

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:

+4
2

, , , , :

DECLARE @tbl TABLE(ID INT, Value INT, Date DATETIME);
INSERT INTO @tbl VALUES
 (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');

WITH Values123 AS
(
    SELECT SUM(Value) AS Sum123,Date
    FROM @tbl
    WHERE ID IN(1,2,3)
    GROUP BY Date
)
SELECT Values123.Sum123
      ,(SELECT SUM(Value) FROM @tbl WHERE Date=Values123.Date AND ID=8)
      ,Values123.Date 
FROM Values123

Sum123  Sum8    Date
30      51      2015-10-12 08:00:00.000
26      58      2015-10-12 09:00:00.000
+2

: PostgreSql

:

select sum("Value") as Value1, date from "Values" where id in(1,2,3) group by date
Union ALL
select sum("Value") as Value1, date from "Values" where id=8 group by date
+2

Source: https://habr.com/ru/post/1619450/


All Articles