select a.* from tb a,
(select 产品编号,日期=max(日期) from tb group by 产品编号) b
where a.产品编号=b.产品编号 and a.日期=b.日期

解决方案 »

  1.   


    试试这句(没测试)Select id, 产品编号,单价, max(日期) as 日期 from 表 group by id  产品编号 单价
      

  2.   


    create table tb(id int,  産品編號 varchar(20),單價 numeric(10,2), 日期 datetime)
    Insert into tb 
    select '1','01001','20','2004-11-1'
    union all select '2','01001','20','2004-11-2'
    union all select '3','01002','20','2004-11-10'
    union all select '4','01002','20','2004-11-11'
    union all select '5','01003','20','2004-11-21'select a.* from tb a,
    (select 産品編號,日期=max(日期) from tb group by 産品編號) b
    where a.産品編號=b.産品編號 and a.日期=b.日期
    order by a.id--結果
    id     产品编号 单价       日期
    ------------------------------------------------------------------
    2 01001 20.00 2004-11-02 00:00:00.000
    4 01002 20.00 2004-11-11 00:00:00.000
    5 01003 20.00 2004-11-21 00:00:00.000
      

  3.   

    也可以這樣寫select a.* from tb a where 日期 in(select max(日期) from tb where 産品編號=a.産品編號) order by a.id結果與上面一樣
      

  4.   

    刚才那个 写错了。这个select b.* from ttt1 b inner join 
    (select 产品编号 ,max(日期)as 日期  from ttt1 group by 产品编号)a
    on a.产品编号=b.产品编号 and a.日期=b.日期 order by id
      

  5.   

    select 产品编号r,max(日期) from abc a group by number
      

  6.   

    select * from ttt1 a where exists (
    select 1 from (select max( 日期)as 日期,产品编号  from ttt1 b group by b.产品编号)b where b.日期=a.日期 and b.产品编号=a.产品编号) order by a.产品编号
      

  7.   

    select t.* from tb t where
    t.日期 in(select max(日期) from tb b where t.日期=b.日期)
      

  8.   

    对不起,写错了!这个对!
    select t.* from tb t where
    日期 =(select max(日期) from tb b where t.产品编号=b.产品编号)
    order by t.id/*测试结果
    id          产品编号       单价          日期                                                     
    ----------- ---------- ----------- ------------------------------------------------------ 
    2           01001      20          2004-11-02 00:00:00.000
    4           01002      20          2004-11-11 00:00:00.000
    5           01003      20          2004-11-21 00:00:00.000(所影响的行数为 3 行)
    */