日期:2014-05-18 浏览次数:20419 次
select item_no,convert(varchar(7),make_date,120),sum(item_num) from( select * from tb a where not exists(select 1 from tb where comp_id=a.comp_id and item_no=a.item_no and convert(varchar(7),make_date,120)=convert(varchar(7),a.make_date,120) and make_date>a.make_date) )t group by item_no,convert(varchar(7),make_date,120)
------解决方案--------------------
select a.item-no,sum(item-num) 'item-num' from tab a inner join (select compid,item-no,max(Make_date) maxMake_date from tab where datepart(m,Make_date)=[指定的月] group by compid,item-no) b on a.compid=b.compid and a.item-no=b.item-no and a.Make_date=b.maxMake_date group by a.item-no