字段1  字段2 字段3 现在字段1 和字段2 有重复数据 但是 字段3 缺不全是重复数据
先前我只需要查询字段1和字段2的数据 并且过滤掉重复数据是这样 
select distinct 字段1,字段2 from tableName
但是现在需要增加字段3 这样该怎么写

解决方案 »

  1.   

    select distinct 字段1,字段2,字段3 from tableName
      

  2.   

    try this,select 字段1,字段2,字段3 from
    (select 字段1,字段2,字段3,
            row_number() over(partition by 字段1,字段2 order by 字段3) 'rn'
     from [表名]) t
    where t.rn=1
      

  3.   


    select * 
    from [huang] a
    WHERE EXISTS (SELECT 1 FROM (SELECT [字段1],[字段2],MAX([字段3])[字段3] FROM huang GROUP BY [字段1],[字段2])b 
    WHERE a.[字段1]=b.字段1 AND a.[字段2]=b.字段2 AND a.[字段3]=b.[字段3])
      

  4.   

    select 字段1,字段2,min(字段3) as 字段3 from group by 字段1,字段2
      

  5.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-11-05 14:43:40
    -- Version:
    --      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
    -- Dec 28 2012 20:23:12 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([字段1] int,[字段2] int,[字段3] int)
    insert [huang]
    select 1,1,2 union all
    select 1,1,3 union all
    select 2,2,3 union ALL
    select 2,2,4
    --------------开始查询--------------------------select * 
    from [huang] a
    WHERE EXISTS (SELECT 1 FROM (SELECT [字段1],[字段2],MIN([字段3])[字段3] FROM huang GROUP BY [字段1],[字段2])b 
    WHERE a.[字段1]=b.字段1 AND a.[字段2]=b.字段2 AND a.[字段3]=b.[字段3])
    ----------------结果----------------------------
    /* 
    字段1         字段2         字段3
    ----------- ----------- -----------
    1           1           2
    2           2           3
    */
      

  6.   


    select 字段1,字段2,字段3 from
    (select 字段1,字段2,字段3,
            row_number() over(partition by 字段1,字段2 order by 字段3) 'rn'
     from [表名]) t
    where t.rn=1