问题
I am working with the next table:
**ID Value Data**
1 30 25/4
1 20 26/4
1 20 27/4
3 10 25/4
4 20 26/4
5 30 26/4
NULL 50 25/4
NULL 10 26/4
And I need to query the table and have the next result:
**ID Value Data**
1 70 25/4
3 10 25/4
4 20 26/4
5 30 26/4
NULL 50 25/4
NULL 10 26/4
I have this query:
select id, sum(value), min(data)
from t
group by id;
But the query sums the NULL IDs
How can I do that?
回答1:
The simplest method is probably union all:
select id, sum(value), min(data)
from t
where id is not null
group by id
union all
select id, value, data
from t
where id is null;
来源:https://stackoverflow.com/questions/56345735/group-by-id-except-the-null-records