我的查询有输出:
Item Type Qty
1 A 2
2 A 3
3 B 1
4 B 2
5 C 1
6 D 3
要分组的类型:A,B
我需要我的输出看起来像这样:(在将数量和分组相加之后)
Type Qty
A 5
B 3
OTHERS 4
如果未定义类型,则会将其分组为' OTHERS'。这是可以使用分析函数完成还是我需要为此创建自己的函数?
答案 0 :(得分:2)
假设您的表/视图名称为x,则可以精确地提供所需的输出:
SELECT CASE WHEN type IN ('A', 'B') THEN type ELSE 'OTHERS' END AS type,
SUM(qty) AS qty
FROM x
GROUP BY CASE WHEN type IN ('A', 'B') THEN type ELSE 'OTHERS' END
ORDER BY 1
答案 1 :(得分:0)
select type,sum(qty) from (select decode(type,c,'OTHERS',d,'OTHERS') type,qty from your_table) group by type;
(or)
select type,sum(qty) from (select (case when type in ('A','B') then type else 'others' end) type,qty from your_table) group by type;