有一出库存明细表,数据如下:
记录号 日期 网点 货号 数量
1 2005-8-1 杭州 A 10
2 2005-9-1 杭州 A 30
3 2005-7-1 温州 C 50
4 2005-9-1 温州 B 90
5 2005-9-1 广州 D 40
6 2005-5-1 广州 A 90
需要得到的结果同,每一个网点最早一次出货的记录,就是每个网点日期最小的记录
记录号 日期 网点 货号 数量
1 2005-8-1 杭州 A 10
3 2005-7-1 温州 C 50
6 2005-5-1 广州 A 90
请高手指点,谢谢!
select * from 库存明细表 t
where not exists(select 1 from 库存明细表 where 网点=t.网点 and 日期<t.日期)
or
select * from 库存明细表 t
where 日期=(select min(日期) from 库存明细表 where 网点=t.网点)
or
select * from 库存明细表 t
where 日期=(select top 1 日期 from 库存明细表 where 网点=t.网点 order by 日期 desc)
最后一个改一下:
select * from 库存明细表 t
where 日期=(select top 1 日期 from 库存明细表 where 网点=t.网点 order by 日期)
select * from 库存明细表 where 日期+网点 in(select min(日期)+网点 from 库存明细表 group by 网点)
select * from 库存明细表 t
where 日期 in (select min(日期) from 库存明细表 where 网点=t.网点 order by 日期)
select * from 库存明细表 t
where 日期=(select min(日期) from 库存明细表 where 网点=t.网点)
这个好
select *from 库存明细表 group by 网点 having 日期=min(日期)