有表如下:
Name      Companyname    date            Price     identity 列
A产品     A公司            2008-05-12      5          1
A产品     A公司            2008-05-30      4          2
B产品     B公司            2008-05-13      5          3
B产品     B公司            2008-05-28      4          4得到的结果如下. 以日期值判断,取最大日期:
name     companyname    date         price
a产品      a公司            2008-05-30    4
b产品      b公司            2008-05-28    4
谢谢.

解决方案 »

  1.   

    select * from tb a
    where not exists(select 1 from tb where a.name=name and a.companyname=companyname and a.date<date)
      

  2.   

    select t.name ,   t.companyname  ,  t.date ,       t.price from tb t where
    not exists(select 1 from tb where name =t.name and companyname=t.companyname and date>t.date ) 
      

  3.   

    select s.* from table s where not exists(select 1 from table s where name=s.name and companyname=s.companyname and date>s.date)
      

  4.   


    select Name,Companyname,date,Price from tb a
    where not exists(select 1 from tb where a.name=name and a.companyname=companyname and a.date <date)
      

  5.   


    declare @tb table(Name nvarchar(10),      Companyname varchar(10),   date smalldatetime ,           Price  int,   id int)
    insert @tb
     select N'A产品',   'A公司' ,           '2008-05-12' ,     5    ,      1 union all
     select N'A产品' ,  'A公司',           ' 2008-05-30' ,     4  ,        2 union all
     select N'B产品'  , 'B公司' ,           '2008-05-13'  ,    5  ,        3  union all
     select N'B产品'   ,'B公司'  ,          '2008-05-28'  ,    4 ,         4  select * from @tb as a where not exists(select 1 from @tb where name=a.name and Companyname=a.Companyname and date>a.date )
    /*
    Name       Companyname date                                                   Price       id          
    ---------- ----------- ------------------------------------------------------ ----------- ----------- 
    A产品        A公司         2008-05-30 00:00:00                                    4           2
    B产品        B公司         2008-05-28 00:00:00                                    4           4
    */
      

  6.   

    select t.name ,t.companyname,t.date ,t.price  from tb t join (select min(date)date,Name,companyname from tb group by Name,companyname) b on t.Name=b.Name and t.date=b.date
      

  7.   

    select * 
    from 表 a 
    where 
      not exists(select 1 from 表 where a.name=name and a.companyname=companyname and date > a.date)
      

  8.   

    select A.Name,A.Companyname,A.Date,B.Price 
      from  table as B,
            (select max(date) As Date,Name,Companyname    
               from table 
               group by   Name,Companyname)As A     
     where A.Name = B.Name
       and A.Companyname=B.Companyname
       and A.Date=B.date 
    你看看啊 
      

  9.   

    select * from 
    (select name,company_name,date,price,
    row_number()over (partition by name,company_name  order by date desc)as u
    from table)h
    where h.u = 1
      

  10.   

    Name      Companyname    date            Price    identity 列 select * from '表名' tt where not exists (select * from '表名' where Name=tt.Name and CompanyName=tt.CompanyName and date<tt.date)