按ID分组,但NULL记录除外 [英] Group by ID except the NULL records

查看:90
本文介绍了按ID分组,但NULL记录除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理下表:

**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

我有这个查询:

select id, sum(value), min(data)
from t
group by id;

但是查询将对NULL ID求和

But the query sums the NULL IDs

我该怎么办?

推荐答案

最简单的方法可能是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;

这篇关于按ID分组,但NULL记录除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆