zoukankan      html  css  js  c++  java
  • sql求分组数据中最小的前三条 (转)

    --方法1.求分组数据中最小的前三条
    SELECT  t1.*
    FROM    dbo.info t1
    WHERE   t1.name IN ( SELECT TOP 3
                                t2.name
                         FROM   info t2
                         WHERE  t2.class = t1.class
                         ORDER BY t2.score ASC )
                         ORDER BY t1.class DESC ,t1.score asc
    --方法2.求分组数据中最小的前三条
    SELECT * FROM
    (SELECT  * ,
            ( SELECT    COUNT(1)
              FROM      dbo.info t2
              WHERE     t1.score > t2.score
                        AND t1.class = t2.class
            ) AS num
    FROM    dbo.info t1) t
                WHERE num<=2
                ORDER BY t.class DESC,num ASC

    以下是表结构

    方法1查询后的结果

    方法2查询的结果

     

     
     
     
  • 相关阅读:
    hdu3829(最大独立集)
    hdu2444(判二分图+最大匹配)
    hdu2063+hdu1083(最大匹配数)
    hdu3622(二分+two-sat)
    poj3678(two-sat)
    hdu1824(two-sat)
    hdu3062(two-sat)
    POJ1067 取石子游戏
    POJ1066 Treasure Hunt
    POJ1065 Wooden Sticks
  • 原文地址:https://www.cnblogs.com/qanholas/p/3115491.html
Copyright © 2011-2022 走看看