select * from 表1 group by col1

解决方案 »

  1.   


    create table 表1
    (col1 varchar(10),col2 int)insert into 表1
     select 'A',1 union all
     select 'B',1 union all
     select 'C',2 union all
     select 'D',3 union all
     select 'E',3 union all
     select 'A',3 union all
     select 'F',4 union all
     select 'F',5
    select col1,col2 from
    (select col1,col2,
            row_number() over(partition by col1 order by getdate()) 'rn'
     from 表1) t
    where rn=1/*
    col1       col2
    ---------- -----------
    A          1
    B          1
    C          2
    D          3
    E          3
    F          4(6 row(s) affected)
    */
      

  2.   

    SELECT  col1,min(col2) FROM tab group by col1 
      

  3.   


    create table T (col1 varchar(1) , col2 int)
    insert into Tselect 'A' , 1 union all
    select 'B' , 1 union all
    select 'C' , 2 union all
    select 'D' , 3 union all
    select 'E' , 3 union all
    select 'A' , 3 union all
    select 'F' , 4 union all
    select 'F' , 5 
    goSelect * from T a where not exists(select 1 from T where col1=a.col1 and col2<a.col2)
      

  4.   

    SELECT * INTO #temp FROM
    (
    SELECT 'A' col1,'1' col2 UNION ALL
    SELECT 'B','1' UNION ALL
    SELECT 'C','2' UNION ALL
    SELECT 'D','3' UNION ALL
    SELECT 'E','3' UNION ALL
    SELECT 'A','3' UNION ALL
    SELECT 'F','4' UNION ALL
    SELECT 'F','5'
    )a
    GOSELECT col1,col2 
    FROM(SELECT 
    col1,col2,
        ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY (SELECT 1)) showtimes
     FROM #temp)b 
    WHERE showtimes = 1
    GO
    /*col1 col2
    ---- ----
    A    1
    B    1
    C    2
    D    3
    E    3
    F    4(6 行受影响)*/
      

  5.   

    这里的关键在于如何标志先出现,如果不理解row_num(),或版本低于2005,可以用下面的思路,先增加一个自增列作为辅助列,再删除。
    -- 这里假设原表为 temp
    ALTER TABLE temp ADD ident INT IDENTITY(1,1);
    GoSELECT col1,col2 
    FROM temp 
    WHERE ident IN(SELECT MIN(ident)
                   FROM temp
                   GROUP BY col1);
    GoALTER TABLE temp DROP COLUMN ident;
    GO
    /*
    col1       col2
    ---------- ----------
    A          1
    B          1
    C          2
    D          3
    E          3
    F          4(6 行受影响)
    */
      

  6.   

    你这个选了两列,不能group by一列的,语法不对
      

  7.   


    USE tempdb;
    /*
    CREATE TABLE t1
    (
    col1 NVARCHAR(10) NOT NULL,
    col2 INT NOT NULL
    )
    INSERT INTO t1 
    VALUES('A',1),('B',1),('C',2),('D',3),('E',3),('A',3),('F',4),('F',5);
    */
    SELECT  temp.col1 ,
            temp.col2
    FROM    ( SELECT    * ,
                        ROW_NUMBER() OVER ( PARTITION BY col1 ORDER BY col1 ) AS ordernum
              FROM      t1
            ) AS temp
    WHERE   temp.ordernum = 1
      

  8.   

    select col1,col2  from (
     select col1,col2,  
     ROW_NUMBER() over(partition by col1 order by getdate() desc) as 'num'
     from t) tb
    where tb.num =1
     
      

  9.   

    order by getdate() 实现的是什么呢?
      

  10.   

    和3楼的方法倒是一样,就是多了个desc,为啥结果还一样呢。我不太懂这个order by getdate()
      

  11.   

    和3楼的方法倒是一样,就是多了个desc,为啥结果还一样呢。我不太懂这个order by getdate()按日期进行倒序排序