【发布时间】:2017-05-14 06:12:45
【问题描述】:
我想按两列分组。如果 b 不为空,我想按 a 和 b 获得 c 组的总数,如果 b 为空,我想按 a 组 我写了这个查询,但是如果 b 为空,它就不起作用!查询的结果是所有 b 不为空的行
select m.s.a ,
case when (m.l is not null)
then m.l.code end , coalesce(sum(m.c),0 )
from material m where m.Item.id =:itemId
group by m.s.a, case
when (m.l is not null)
then m.l.code end
+--+----+-------+---+
| | s | l | c |
+--+----+-------+---+
| | a | d | 1 |
| | a | d | 9 |
| | a | e | 3 |
| | a | f | 4 |
| | c | g | 5 |
| | c | g | 6 |
| | c | h | 20 |
| | d | null | 7 |
| | d | null | 8 |
预期结果:
+--+----+-------+---+
| | s | l | c |
+--+----+-------+---+
| | a | d | 10 |
| | a | e | 3 |
| | a | f | 4 |
| | c | g | 11 |
| | c | h | 20 |
| | d | | 15 |
【问题讨论】:
-
你的问题没有多大意义。请在问题中添加一些示例数据和预期输出以澄清。
-
@GurwinderSingh 在编辑我的帖子并添加示例数据和预期输出
标签: sql oracle hibernate group-by