/****** sqlserver 寫法 *****
*/select
*from[lx
].[dbo
].[全國銷售額
]where province='北京
'--group by [province],[name]
--按銷售額排序 取每個省的前4名,剩下的為其他
select
*from
(select a.province,a.name ,a.value from 全國銷售額 a where value in (select
top4 value from 全國銷售額 where province=a.province order
by value desc
) union
allselect a.province,'
其他',sum(a.value) value from 全國銷售額 a where value not
in (select
top4 value from 全國銷售額 where province=a.province order
--oracle 寫法(大於5的列到其他)select a.bm,'其他' mdd ,a.jssj,sum(b.jp) jp,sum(b.hc) hc ,sum(b.bzfy) bzfy,sum(b.zsfy) zsfy,sum(b.jtqt) jtqt,sum(b.qt) qt,sum(b.jtgj) jtgj,
sum(b.bmj) bmj,sum(b.ygj) ygj ,sum(b.gsj) gsj,sum(b.zjj) zjj,sum(b.ccts) ccts ,sum(b.shje) shje from
( select a.bm,a.mdd,a.jssj,jtgj, row_number() over(partition by a.bm,a.jssj
order by jtgj desc) as rn from nsc_oa_clhz a
) a left join nsc_oa_clhz b on nvl(a.bm,0) = nvl(b.bm,0) and nvl(a.mdd,0) = nvl(b.mdd,0) and nvl(a.jssj,0)=nvl(b.jssj,0)
and a.rn >5
group by a.bm,a.jssj
mysql分組取每組前幾條記錄
drop table if exists ho archives create table ho archives id mediumint 11 unsigned not null auto increment comment 自增id type smallint 5 unsigned not n...
SQL分組取每組前一 或幾 條記錄 排名
use test go object table dbo scores script date 2018 3 11 17 59 15 set ansi nulls on goset quoted identifier on goset ansi padding on gocreate table d...
sql取分組的前幾條 指定條數
注 sqlserver下 create table test areaid int,score int insert into test select 0,10 union all select 0,20 union all select 0,30 union all select 0,40 uni...