表结构如下:
id int
type varchar2
detailtype varchar2
比如有如下记录
id type detailtype
1 报纸 日报
2 报纸 日报
3 报纸 晚报
4 媒体
想有如下结果:
type detailtype num
报纸 日报 2
晚报 1
媒体 1
with a as (select 1 id,'報紙' type,'日報' detailtype FROM DUAL
union
select 2 id,'報紙' type,'日報' detailtype FROM DUAL
union
select 3 id,'報紙' type,'晚報' detailtype FROM DUAL
union
select 4 id,'媒體' type, NULL detailtype FROM DUAL
)
select decode(row_number() over(partition by type order by type), 1, type, null) type,
detailtype,
count(*) num
from A
group by type, detailtype;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10768286/viewspace-83471/ ,如需转载,请注明出处,否则将追究法律责任。