Oracle group by与partition by

1. group by can only be grouped, and cannot be sorted after grouping.
Group by must be followed by all the fields selected by select, otherwise an error will be reported.
For example: select a,b from table group by a
there will be an error,
it should be changed to select a,b from table group by a,b
2. partition by can realize sorting after grouping

根据a1进行分组,b1进行排序
select row_number() over(partition by a1 order by b1 ) ;
----根据a1进行分组,b1进行排序,查询出c1,c2字段按照规则排序
select row_number() over(partition by a1 order by b1 ) c1,c2;
------例如,查询数据为按年分组,序号进行排序
 select * from(
		select t.*,row_number() over(partition by fyear order by XH) row_number
		from  table t)
		where 1=1 and fyear>=2021 and fyear<=2023

Through the above code, group by cannot be sorted after grouping, but partition by can be sorted.

Guess you like

Origin blog.csdn.net/sunxiaohong__/article/details/128079977
Recommended