ID          产品           价格
----------- ----------    --------
433334      产品1           170
435420      产品1           170
435431      产品1           154
435439      产品2           180
435440      产品2           180
435454      产品2           180
找出同一产品价格最高的记录,要求得到的结果为
ID          产品           价格
----------- ----------    --------
433334      产品1           170
435439      产品2           180
怎么写语句

解决方案 »

  1.   

    select t.* from tb where 价格 = (select max(价格) from tb where 产品 = t.产品) order by t.产品
    select t.* from tb where not exists (select 1 from tb where 产品 = t.产品 and 价格 > t.价格) order by t.产品
      

  2.   

    create table tb(ID varchar(10),         产品 varchar(10),         价格 int)
    insert into tb values('433334' ,     '产品1' ,         170 )
    insert into tb values('435420' ,     '产品1' ,         170 )
    insert into tb values('435431' ,     '产品1' ,         154 )
    insert into tb values('435439' ,     '产品2' ,         180 )
    insert into tb values('435440' ,     '产品2' ,         180 )
    insert into tb values('435454' ,     '产品2' ,         180 )
    goselect t.* from tb t where 价格 = (select max(价格) from tb where 产品 = t.产品)  order by t.产品
    select t.* from tb t where not exists (select 1 from tb where 产品 = t.产品 and 价格 > t.价格) order by t.产品drop table tb/*
    ID         产品         价格          
    ---------- ---------- ----------- 
    433334     产品1        170
    435420     产品1        170
    435454     产品2        180
    435439     产品2        180
    435440     产品2        180(所影响的行数为 5 行)ID         产品         价格          
    ---------- ---------- ----------- 
    433334     产品1        170
    435420     产品1        170
    435439     产品2        180
    435440     产品2        180
    435454     产品2        180(所影响的行数为 5 行)*/
      

  3.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (ID int,产品 nvarchar(6),价格 int)
    insert into [tb]
    select 433334,N'产品1',170 union all
    select 435420,N'产品1',170 union all
    select 435431,N'产品1',154 union all
    select 435439,N'产品2',180 union all
    select 435440,N'产品2',180 union all
    select 435454,N'产品2',180
    select * from tb t
    where not exists(select 1 from tb where 产品=t.产品 and (价格>t.价格) or(价格=t.价格 and ID<t.ID) )
    /*
    ID          产品     价格
    ----------- ------ -----------
    433334      产品1    170
    435439      产品2    180(2 個資料列受到影響)*/
      

  4.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([ID] int,[产品] varchar(5),[价格] int)
    insert [tb]
    select 433334,'产品1',170 union all
    select 435420,'产品1',170 union all
    select 435431,'产品1',154 union all
    select 435439,'产品2',180 union all
    select 435440,'产品2',180 union all
    select 435454,'产品2',180select min([ID]) as [ID],[产品],max([价格]) as [价格] 
    from [tb]
    group by [产品]
    having count(1)>=2
    ------------------------433334 产品1 170
    435439 产品2 180
      

  5.   

    create table tb(ID varchar(10),         产品 varchar(10),         价格 int)
    insert into tb values('433334' ,     '产品1' ,         170 )
    insert into tb values('435420' ,     '产品1' ,         170 )
    insert into tb values('435431' ,     '产品1' ,         154 )
    insert into tb values('435439' ,     '产品2' ,         180 )
    insert into tb values('435440' ,     '产品2' ,         180 )
    insert into tb values('435454' ,     '产品2' ,         180 )
    goselect t.* from tb t where not exists (select 1 from tb where 产品 = t.产品 and (价格 > t.价格 or (价格 = t.价格 and id < t.id))) order by t.产品drop table tb/*
    ID         产品         价格          
    ---------- ---------- ----------- 
    433334     产品1        170
    435439     产品2        180(所影响的行数为 2 行)*/
      

  6.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (ID int,产品 nvarchar(6),价格 int)
    insert into [tb]
    select 433334,N'产品1',170 union all
    select 435420,N'产品1',170 union all
    select 435431,N'产品1',154 union all
    select 435439,N'产品2',180 union all
    select 435440,N'产品2',180 union all
    select 435454,N'产品2',180select distinct(产品) 产品,价格 from
    (
    select  * from tb t where not exists(select top 1 * from [tb] where 产品=t.产品 and 价格>t.价格)
    ) tt
      

  7.   


    --如果是sql server 2005,则如下:select ID , 产品 , 价格 from
    (
      select * , px = row_number() over(partition by 产品 order by 价格 desc , ID) from tb
    ) t
    where px = 1
      

  8.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (ID int,产品 nvarchar(6),价格 int)
    insert into [tb]
    select 433334,N'产品1',170 union all
    select 435420,N'产品1',170 union all
    select 435431,N'产品1',154 union all
    select 435439,N'产品2',180 union all
    select 435440,N'产品2',180 union all
    select 435454,N'产品2',180select ID,产品,价格 from
    (
    select ID,产品,价格,
          (select count(*)+1 from [tb] where 产品=tt.产品 and ID<tt.ID) as rank
       from [tb] tt
    ) t1
    where rank=1ID          产品     价格
    ----------- ------ -----------
    433334      产品1    170
    435439      产品2    180
      

  9.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (ID int,产品 nvarchar(6),价格 int)
    insert into [tb]
    select 433334,N'产品1',170 union all
    select 435420,N'产品1',170 union all
    select 435431,N'产品1',154 union all
    select 435439,N'产品2',180 union all
    select 435440,N'产品2',180 union all
    select 435454,N'产品2',180select ID,产品,价格 from tb t
    where not exists(select 1 from tb where 产品=t.产品 and (价格>t.价格) or (价格=t.价格 and ID<t.ID))
    /*
    ID          产品     价格          
    ----------- ------ ----------- 
    433334      产品1    170
    435439      产品2    180(所影响的行数为 2 行)*/