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

急,问个SQL语句
在一个数据表A里面的记录当中,有些记录(根据字段num+ITEM)是有多个,我要根据生效日期(effect_data)塞选它们,我想要的结果是:

    查询出来的   字段NUM   +   字段ITEM   的记录是唯一,如果不是唯一的话,就选择它的生效日期(effect_data)最晚的哪个记录.

谢谢!

------解决方案--------------------

select num,ITEM,min(effect_date),min(你所想要查询的字段) from A a
where (select count(*) from A where num=a.num and ITEM=a.ITEM and effect_date> a.effect_date) <1 --生效日期(effect_data)最晚的哪个记录.
group by num,ITEM
------解决方案--------------------
select * from 表A a,
(select max(effect_data) as effect_data,num,ITEM from 表A group by num+ITEM) b
where a.effect_date=b.effect_date and a.num=b.num and a.ITEM=b.ITEM