Reputation: 43
I have a table like below:
ID | Date | Value
---+------------+------
H1 | 11/9/2015 | 3
H2 | 11/10/2015 | 5
H3 | 11/11/2015 | 2
H1 | 11/9/2015 | 1
H2 | 11/13/2015 | 4
H3 | 11/8/2015 | 5
H1 | 11/15/2015 | 8
I want to create a single query which can show me the result as below:
ID | All Values | Future Values
---+------------+--------------
H1 | 12 | 8
H2 | 9 | 9
H3 | 7 | 2
I am able to sum all the data by writing query
select ID, Sum(ID) from table group by ID
but how to sum future data in same query? I managed to create a separate query for future data:
select ID, sum(ID) from table where date > now() group by ID
Please help me out to make a single query... Thanks.
Upvotes: 1
Views: 37
Reputation: 49049
You could use CASE WHEN:
SELECT
ID,
SUM(value) AS all_values,
SUM(CASE WHEN date > current_date THEN value END) AS future_values
FROM
table
GROUP BY
ID
(I'm using current_date because I think that your date column contains just date information with no time, use now() if this is not the case)
Upvotes: 1