有一表table1   项目名称     日期       单价
   青菜1        207-8-1    12.2
   青菜1        207-8-2    15.2  
   青菜2        207-8-2    38我要求显示的效果为
   项目名称     日期       单价
   青菜1        207-8-2    15.2  
   青菜2        207-8-2    38该SQL怎么写?

解决方案 »

  1.   

    create table table1(项目名称 varchar(10),日期 datetime,单价 decimal(18,1))
    insert table1
    select '青菜1','2007-08-01',12.2
    union all
    select '青菜1','2007-08-02',15.2
    union all
    select '青菜2','2007-08-02',38select * from table1 T1 where not exists(select top 1 * from table1 T2 where T2.日期>T1.日期)drop table table1
      

  2.   


    select a.*
    from table1 a
    inner join
    (select 项目名称,max(日期) as 日期
    from table1
    group by 项目名称) b
    on a.项目名称=b.项目名称 and a.日期=b.日期
      

  3.   

    select * from t a
    where 日期=(select max(日期) from t where  项目名称=a. 项目名称)
    這樣是不是更簡單
      

  4.   

    select a.* from table1 a,
    (select 项目名称,max(日期) 日期 frm table1 group by 项目名称) b
    where a.项目名称 = b.项目名称 and a.日期 = b.日期
      

  5.   

    declare @table table(name varchar(100),date varchar(100),price varchar(100))
    insert into @table select '青菜1','207-1-1','20.6'
    union all select '青菜1','207-2-1','21.6'
    union all select '青菜2','207-4-1','21.6'select a.* from @table a where not exists(select 1 from @table where [name]=a.[name] and [date]>a.[date])
      

  6.   

      select * from table1 where 日期 in( select max(日期) from table1 group by  项目名称)
      

  7.   

    select 项目名称,max(日期) as 日期,max(单价) as 单价
    from table1
    group by 项目名称