日期:2014-05-17  浏览次数:20559 次

疑难SQL语句 请教高手
这个是表内容


我想做一个查询
SELECT ID,编号,日期, 数量,MAX(日期2), (-这里加上MAX(日期2)  对应的ID2-) from T 
GROUP by ID,编号,日期, 数量

这个有办法实现吗?

------解决方案--------------------
select ID,编号,日期, 数量,[最大日期],ID2 from T where 日期2 in(
  SELECT [最大日期]from(
    SELECT ID,编号,日期, 数量,[最大日期]=MAX(日期2) from T 
    GROUP by ID,编号,日期, 数量
  )a
)
------解决方案--------------------
SELECT ID,编号,日期, 数量,日期2,ID2 from T a
where not exists (select 1 from T where ID=a.ID and 日期2>a.日期2)

------解决方案--------------------
SELECT ID,编号,日期, 数量,日期2,ID2 
from T a 
where not exists (
 select 1 
 from T 
 where ID=a.ID 
 and 编号 = a.编号
 and 日期 = a.日期
 and 数量 = a.数量
 and 日期2>a.日期2
 )