这个是表内容
我想做一个查询
SELECT ID,编号,日期, 数量,MAX(日期2), (-这里加上MAX(日期2)  对应的ID2-) from T 
GROUP by ID,编号,日期, 数量这个有办法实现吗?

解决方案 »

  1.   

    select ID,编号,日期, 数量,[最大日期],ID2 from T where 日期2 in(
      SELECT [最大日期]from(
        SELECT ID,编号,日期, 数量,[最大日期]=MAX(日期2) from T 
        GROUP by ID,编号,日期, 数量
      )a
    )
      

  2.   

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

  3.   

    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
     )