Group by ID except the NULL records

让人想犯罪 __ 提交于 2019-12-11 14:49:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!