我想实现的功能是,在下表中:
ID    NAME      PRICE
1     wang      12
1     wang      13
1     wang      14
2     zhao      12
2     zhao      13
2     zhao      14
3     qian      12
3     qian      13
3     qian      14用什么语句能取到每个相同ID的第一项呢,即结果是:
ID    NAME      PRICE
1     wang      12
2     zhao      12
3     qian      12

解决方案 »

  1.   


    select * from tb a where not exists(
    select 1 from tb where id=a.id and name=a.name and price>a.price
    )
      

  2.   

    select t.* from tb t where price = (select top 1 price from tb where id = t.id order by price) order by t.id
      

  3.   


    select t.* from tb t where price = (select top 1 price from tb where id = t.id order by price) order by t.idselect t.* from tb t where price = (select min(price) from tb where id = t.id) order by t.idselect t.* from tb t where not exists(select 1 from tb where id = t.id and price < t.price) order by t.id
      

  4.   


    select id,name,min(price) from tb
    group by id,name
      

  5.   

    select
      *
    from
      tb t
    where
      PRICE=(select min(PRICE) from tb where id=t.id)
      

  6.   

    ;WITH CTE AS (
    SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY GETDATE()) AS NUM
    FROM TAB
    )
    SELECT ID,NAME,PRICE
    FROM CTE 
    WHERE NUM = 1
      

  7.   

    create table tb(ID int,NAME varchar(10),PRICE int)
    insert into tb values(1 ,'wang', 12)
    insert into tb values(1 ,'wang', 13)
    insert into tb values(1 ,'wang', 14)
    insert into tb values(2 ,'zhao', 12)
    insert into tb values(2 ,'zhao', 13)
    insert into tb values(2 ,'zhao', 14)
    insert into tb values(3 ,'qian', 12)
    insert into tb values(3 ,'qian', 13)
    insert into tb values(3 ,'qian', 14)
    go
    select t.* from tb t where price = (select top 1 price from tb where id = t.id order by price) order by t.idselect t.* from tb t where price = (select min(price) from tb where id = t.id) order by t.idselect t.* from tb t where not exists(select 1 from tb where id = t.id and price < t.price) order by t.id
    drop table tb
    /*
    ID          NAME       PRICE       
    ----------- ---------- ----------- 
    1           wang       12
    2           zhao       12
    3           qian       12(所影响的行数为 3 行)ID          NAME       PRICE       
    ----------- ---------- ----------- 
    1           wang       12
    2           zhao       12
    3           qian       12(所影响的行数为 3 行)ID          NAME       PRICE       
    ----------- ---------- ----------- 
    1           wang       12
    2           zhao       12
    3           qian       12(所影响的行数为 3 行)*/
      

  8.   

    use temp
    go
    if object_id('tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    id int,
    name char(10),
    price float
    )
    goinsert into tb  select '1', 'wang', '12'
    insert into tb  select '1', 'wang', '13' 
    insert into tb  select '1', 'wang', '14' 
    insert into tb select '2', 'zhao', '12' 
    insert into tb select '2', 'zhao', '13' 
    insert into tb select '2', 'zhao', '14' 
    insert into tb select '3', 'qian', '12' 
    insert into tb select '3', 'qian', '13' 
    insert into tb select '3', 'qian', '14' select * from tb
    select ID,name,price from
    (select *,ROW_NUMBER()over(partition by id order by price )newid from tb)b
    where newid = 1drop table tb