SQL TOP N 分組統計

2021-04-12 20:28:02 字數 2165 閱讀 4671

--分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where  basesal>4000

order by a.emp_id,basesal

--第一種top n 分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where (select   count(1)   from   hrsys.dbo.empbasesal

where   emp_id=a.emp_id   and   basesal>=a.basesal)<=3

and basesal>4000

order by a.emp_id,basesal desc

--第二種 top n 分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where basesal in (select  top  3 basesal  from   hrsys.dbo.empbasesal

where   emp_id=a.emp_id  order by basesal desc )

and basesal>4000

order by a.emp_id,basesal   

--分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where  basesal>4000

order by a.emp_id,basesal

--第一種top n 分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where (select   count(1)   from   hrsys.dbo.empbasesal

where   emp_id=a.emp_id   and   basesal>=a.basesal)<=3

and basesal>4000

order by a.emp_id,basesal desc

--第二種 top n 分組統計

select a.emp_id,b.emp_name,a.dept_no,a.bb_no,a.xh,a.idno,a.emp_no,a.beg_date,a.over_date,a.basesal

from hrsys.dbo.empbasesal a left join hrsys.dbo.employee b on a.emp_id=b.emp_id

where basesal in (select  top  3 basesal  from   hrsys.dbo.empbasesal

where   emp_id=a.emp_id  order by basesal desc )

and basesal>4000

order by a.emp_id,basesal   

linq 分組統計

好久不寫東東了,最近做linq分組統計的時候有點麻煩,用了一天時間才都到自己想要的效果,以下是entity framework的分組統計 iqueryablemm null entity.class1 ma new entity.class1 mm from t in context.subject...

ListMultimap分組統計

背景 分時段統計賬單報表,時間間隔為 30分鐘,1小時,2小時,4小時 對應時段為 00 00 00 30,00 30 01 00,01 00 01 30 00 00 01 00,01 00 02 00,02 00 03 00 00 00 02 00,02 00 04 00,04 00 06 00 ...

pandas groupby, agg分組統計

分組統計是資料分析中較為複雜的一種操作,但是使用起來非常有用,能避開大量的迴圈等等。dataframe.groupby by none axis 0 level none as index true sort true group keys true squeeze false kwargs 對da...