with temp as(
select 1 col1,2 col2 from dual
union all
select 2 col1,2 col2 from dual
union all
select 3 col1,2 col2 from dual
union all
select 4 col1,9 col2 from dual
union all
select 5 col1,9 col2 from dual
union all
select 6 col1,2 col2 from dual
union all
select 7 col1,2 col2 from dual
union all
select 8 col1,3 col2 from dual
)
select COL2,count(*)
from
(
select L.*,
ROW_NUMBER() OVER ( PARTITION BY COL2 ORDER BY COL1 DESC ) + COL1 AS GGM
FROM temp L
) A
group by A.COL2,GGM
order by min(A.COL1)
统计数值,如果隔行则算一行
本文通过一个具体的SQL查询案例,展示了如何使用WITH临时表、窗口函数ROW_NUMBER()以及GROUP BY进行复杂的数据分组和排序操作。通过对不同字段的组合使用,实现了特定条件下的数据汇总与展示。

被折叠的 条评论
为什么被折叠?



