MS SQL 取分组后的几条数据

  SELECT uploaddate

        ,ptnumber

        ,instcount

  FROM   (

             SELECT ROW_NUMBER() OVER(

                        PARTITION BY uploaddate ORDER BY uploaddate DESC

                       ,instcount DESC

                    )  AS row

                   ,*

             FROM   (

                        SELECT uploaddate

                              ,ptnumber

                              ,COUNT(*) instcount

                        FROM   LtblLog.dbo.instlog201408

                        GROUP BY

                               uploaddate

                              ,ptnumber

                    )  AS T

         ) a

  WHERE  row<= 10

 执行结果:

MS SQL 取分组后的几条数据

 

另一条简单的语句:

select * from (select row_number() over(partition by login_name order by login_count) as rownum, * from (select a.*,(select count(*) from lit_test as b where a.login_name=b.login_name) as login_count from lit_test as a) as Z) as T where T.rownum = 1

 

你可能感兴趣的:(sql)