某表 tab1: 
A    B    C 
1    2    3 
2    3    3 
3    4    3 
4    5    4 其中,A、B、C分别是列名
因为列C部分重复,我希望在查询全表信息的时候经过筛选,指定C列的值不重复,并且只取第一条查询出来的记录。
也就是查询出的结果如下表所示:A    B    C 
1    2    3 
4    5    4 查询结果按A列值升序排列,对于C列值重复的记录,只取第一条,其余记录去掉。那么对如下的语句:Select A,B,C from tab1 order by A ASC应该做出什么修改,可以实现上述所说的目的?请各位指教,谢谢~~

解决方案 »

  1.   

    Select A,B,C
    from tab1 
    where not exists(
    select 1 from tab1 t where t.c=c and t.a<a)
    )
      

  2.   

    select * from tab1 t where a=(select min(a) from tab1 where c=t.c)
      

  3.   

    select
      *
    from
      tb t
    where
      a=(select min(a) from tb where c=t.c)
      

  4.   


    /*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-09 11:49:41
    --  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)------------------------------------------------------------------*/
    --> 生成测试数据表:tab1IF OBJECT_ID('[tab1]') IS NOT NULL
    DROP TABLE [tab1]
    GO
    CREATE TABLE [tab1]([a] INT,[B] INT,[C] INT)
    INSERT [tab1]
    SELECT 1,2,3 UNION ALL
    SELECT 2,3,3 UNION ALL
    SELECT 3,4,3 UNION ALL
    SELECT 4,5,4
    GO
    --SELECT * FROM [tab1]-->SQL查询如下:select * from tab1 t where not exists(select 1 from tab1 where t.c=c and t.a<a)select * from tab1 t where a=(select min(a) from tab1 where c=t.c)select * from tab1 t where a=(select top 1 a from tab1 where c=t.c order by a)/*
    a           B           C
    ----------- ----------- -----------
    1           2           3
    4           5           4(2 行受影响)
    */
    三选一
      

  5.   

    select *f rom tab1 a where not exists(select 1 from tab1 where c=a.c and a<a.a)