原始数据:
id name age
a501 qq 20
f526 qq 20
g632 ww 25
k630 yy 32
k633 uu 24
要求结果:
f526 qq 20
g632 ww 25
k630 yy 32
k633 uu 24name相同的只保留其中的任意一条

解决方案 »

  1.   

    select * 
    from tb t 
    where id=(select max(ID) from tb where name=t.name)
      

  2.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([id] nvarchar(4),[name] nvarchar(2),[age] int)
    Insert tb
    select N'a501',N'qq',20 union all
    select N'f526',N'qq',20 union all
    select N'g632',N'ww',25 union all
    select N'k630',N'yy',32 union all
    select N'k633',N'uu',24
    Go
    select * 
    from tb t 
    where id=(select max(ID) from tb where name=t.name)
    /*
    id   name age
    ---- ---- -----------
    k630 yy   32
    g632 ww   25
    k633 uu   24
    f526 qq   20(4 個資料列受到影響)
    */
      

  3.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-13 09:27:12
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [tb]
    IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb] ([id] [nvarchar](10),[name] [nvarchar](10),[age] [int])
    INSERT INTO [tb]
    SELECT 'a501','qq','20' UNION ALL
    SELECT 'f526','qq','20' UNION ALL
    SELECT 'g632','ww','25' UNION ALL
    SELECT 'k630','yy','32' UNION ALL
    SELECT 'k633','uu','24'--SELECT * FROM [tb]-->SQL查询如下:
    select * from tb t where id=(select top 1 id from tb where name=t.name)
    /*
    id         name       age
    ---------- ---------- -----------
    a501       qq         20
    g632       ww         25
    k630       yy         32
    k633       uu         24(4 行受影响)
    */
      

  4.   

    with leno as
    (select id row_number() over(partition by name order by id desc) rownumber from tab )delete from leno where rownumber >= 2
      

  5.   

    if not object_id('tb') is null
        drop table tb
    Go
    Create table tb([ID] varchar(4),[name] varchar(2),age int)
    Insert tb
    select  'a501', 'qq', 20 union all
    select  'f526', 'qq', 20 union all
    select  'g632',  'ww', 25 union all
    select  'k630',  'yy', 32 union all
    select  'k633',  'uu', 24
    Go
    select * from tb t where id = (select top 1 id from tb where name=t.name order by newid())/*ID   name age         
    ---- ---- ----------- 
    a501 qq   20
    g632 ww   25
    k630 yy   32
    k633 uu   24(所影响的行数为 4 行)*/
      

  6.   

    top 再 order by 估计效率就不高了。
      

  7.   

    select * from (select name  from tb group by name)a
    outer apply(select top 1 id,age from tb where a.name=name order by newid()) bname id   age
    ---- ---- -----------
    qq   a501 20
    uu   k633 24
    ww   g632 25
    yy   k630 32(4 行受影响)