最后一次可以max来取
如果max取不到
可以用row_number
写法
select a.spid,a.spbh,a.spmch,a.shpgg,a.dw,a.shpchd,a.jixing,count(b.spid) as tiaosh, sum(b.chkshl) as shl,sum(case when b.bm=‘终端部‘ then b.chkshl else 0 end) as ‘终端销量‘,sum(case when b.bm=‘医院部‘ then b.chkshl else 0 end) as ‘医院销量‘,c.hshj as ‘最近进价‘,c.dwmch as ‘最近来货单位‘ from spkfk a,spls_ck b,(select a.rq,a.spid,a.hshj,c.dwmch,row_number() over (PARTITION BY a.spid order by a.rq desc) row from jh_rkmx a,jh_rkhz b,mchk c where a.djbh=b.djbh and c.dwbh=b.dwbh and a.rq <=:e_rq group by a.spid,a.hshj,c.dwmch,a.rq ) c where a.spid=b.spid and b.djbh like ‘xs%‘ and a.spid=c.spid and c.row=1 and b.rq >=:s_rq and b.rq <=:e_rq group by a.spid,a.spbh,a.spmch,a.shpgg,a.dw,a.shpchd,a.jixing,c.hshj,c.dwmch order by a.spbh
时间: 2024-10-08 12:59:32