现在的sql中select 后加 row_number() over () 生成一个序号,
然后select * from (加序号后的sql) where 序号<=15
-------------------------
select aa.* from (
select xh,大类,sum(数量) as 数量,sum(净销售) as 净销售,ccusheadcode,row_number() over(partition by xh,大类,ccusheadcode order by sum(数量) desc,sum(净销售) desc) [i] from
(select CASE when isnull(e.xh,'')='' THEN '106' ELSE e.xh END xh ,
CASE when isnull(e.xh,'')='' THEN '其它品种' ELSE d.largeclassname END 大类 ,
c.year_bi,SUM(ISNULL(c.inatsum_bi,0)-isnull(c.thinatsum_bi,0))*b.prop_ywy/10000 AS 净销售,
sum(c.iquantity_bi-c.thiquantity_bi)*b.prop_ywy as 数量,f.ccusheadcode
from BI_sale_area_part a
left join BI_cus_ywy b on a.market=b.market
left join BI_customer f on b.ccusheadcode=f.ccusheadcode
left join BI_sale_cinvcode_day c on b.ccusheadcode=c.ccusheadcode
left join BI_InventoryNew d on c.cinvcode_bi=d.cinvcode
left join BI_InventorySort e on d.largeClassName=e.InventoryName
WHERE f.ccusheadcode= '000001'
GROUP BY d.largeclassname,c.year_bi, e.xh,b.prop_ywy,f.ccusheadcode
order by 数量 desc
) aa
group by 大类,xh,ccusheadcode
) t
inner join
(select CASE when isnull(e.xh,'')='' THEN '106' ELSE e.xh END xh ,
CASE when isnull(e.xh,'')='' THEN '其它品种' ELSE d.largeclassname END 大类 ,
c.year_bi,SUM(ISNULL(c.inatsum_bi,0)-isnull(c.thinatsum_bi,0))*b.prop_ywy/10000 AS 净销售,
sum(c.iquantity_bi-c.thiquantity_bi)*b.prop_ywy as 数量,f.ccusheadcode
from BI_sale_area_part a
left join BI_cus_ywy b on a.market=b.market
left join BI_customer f on b.ccusheadcode=f.ccusheadcode
left join BI_sale_cinvcode_day c on b.ccusheadcode=c.ccusheadcode
left join BI_InventoryNew d on c.cinvcode_bi=d.cinvcode
left join BI_InventorySort e on d.largeClassName=e.InventoryName
WHERE f.ccusheadcode= '000001'
GROUP BY d.largeclassname,c.year_bi, e.xh,b.prop_ywy,f.ccusheadcode
order by 数量 desc
) aa on t.大类=aa.大类 and t.xh=aa.xh and t.ccusheadcode=aa.ccusheadcode
where t.i<=15