日期:2014-05-19  浏览次数:20747 次

分类汇总的问题
现在我需要统计所有的物料中,日期最新的单价
字段分别是物料编码\单价\日期
本来想
select   物料编码,单价,max(日期)   from   icstockbill  
group   by   物料编码,单价

但是单价各有不同,统计出来以后一各物料有好几个最新单价,怎么能分开啊

还有一个特殊的问题,就是要统计所有的物料中,日期最新的三个单价
这个始终也没考虑好如何实现?

哪个大哥给看看


------解决方案--------------------
select * from icstockbill a where (select count(*) from icstockbill where 物料编码=a.物料编码 and 日期> a.日期) <3 order by 物料编码
------解决方案--------------------
select 物料编码,单价,日期 from icstockbill as a
where
not exists(select 1 from icstockbill where 物料编码=a.物料编码 and 日期 <a.日期)

------解决方案--------------------
select 物料编码,单价,日期 from icstockbill as a
where
(select count(1) from icstockbill where 物料编码=a.物料编码 and 日期> a.日期) <3
------解决方案--------------------
1、
select
i.*
from
icstockbill i
where
not exists(select 1 from icstockbill where 物料编码=i.物料编码 and 日期> t.日期)

2、
select
i.*
from
icstockbill i
where
i.日期 in(select top 3 日期 from icstockbill where 物料编码=i.物料编码 order by 日期 desc)