SQL 排序经典题

2016-11-23

表结构、及表数据如下

CREATE TABLE t(user_id VARCHAR(10) CHARSET utf8,TYPE VARCHAR(20)  CHARSET utf8,score INT);

INSERT INTO t
SELECT ‘A‘ user_id,‘语文‘ AS TYPE,95 score
UNION ALL
SELECT ‘A‘ user_id,‘数学‘ AS TYPE,96 score
UNION ALL
SELECT ‘A‘ user_id,‘英语‘ AS TYPE,86 score
UNION ALL
SELECT ‘A‘ user_id,‘化学‘ AS TYPE,87 score
UNION ALL
SELECT ‘B‘ user_id,‘语文‘ AS TYPE,85 score
UNION ALL
SELECT ‘B‘ user_id,‘数学‘ AS TYPE,56 score
UNION ALL
SELECT ‘B‘ user_id,‘英语‘ AS TYPE,75 score
UNION ALL
SELECT ‘B‘ user_id,‘化学‘ AS TYPE,55 score
UNION ALL
SELECT ‘C‘ user_id,‘数学‘ AS TYPE,68 score
UNION ALL
SELECT ‘C‘ user_id,‘政治‘ AS TYPE,78 score ;

解法一:初级解法(只适用于面试题,不适用于生产环境)

SELECT * FROM (SELECT * FROM t WHERE user_id = ‘A‘ ORDER BY score DESC LIMIT 3) a
UNION ALL
SELECT * FROM (SELECT * FROM t WHERE user_id = ‘B‘ ORDER BY score DESC LIMIT 3) b
UNION ALL
SELECT * FROM (SELECT * FROM t WHERE user_id = ‘C‘ ORDER BY score DESC LIMIT 3) c;

解法二:关联子查询

SELECT
    user_id,
    TYPE,
    score
FROM
    (
        SELECT
            t.*, (
                SELECT
                    COUNT(*)
                FROM
                    t tt
                WHERE
                    tt.user_id = t.user_id
                AND tt.score >= t.score
            ) rn
        FROM
            t
    ) t
WHERE
    rn <= 3;

解法三:自连接

SELECT
    t1.* FROM
    t t1,
    t t2
WHERE
    t1.user_id = t2.user_id
AND t2.score >= t1.score
GROUP BY
    t1.user_id,
    t1.TYPE
HAVING
    COUNT(t2.score) <= 3
ORDER BY
    user_id,
    rn;

解法四:子查询

SELECT
    *
FROM
    t a
WHERE
    EXISTS (
        SELECT
            COUNT(1)
        FROM
            t b
        WHERE
            b.user_id = a.user_id
        AND b.score > a.score
        HAVING
            COUNT(1) < 3
    )

时间: 2024-10-16 02:42:33

SQL 排序经典题的相关文章

poj 2104:K-th Number(划分树,经典题)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 35653   Accepted: 11382 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

nyist oj 311 全然背包 (动态规划经典题)

全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是w. 求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题要求是背包恰好装满背包时,求出最大价值总和是多少. 假设不能恰好装满背包,输出NO 输入 第一行: N 表示有多少组測试数据(N<7). 接下来每组測试数据的第一行有两个整数M.V. M表示物品种类的数目,V表示背

hdu 3657 最小割的活用 / 奇偶方格取数类经典题 /最小割

题意:方格取数,如果取了相邻的数,那么要付出一定代价.(代价为2*(X&Y))(开始用费用流,敲升级版3820,跪...) 建图:  对于相邻问题,经典方法:奇偶建立二分图.对于相邻两点连边2*(X&Y),源->X连边,Y->汇连边,权值w为点权. ans=总点权-最小割:如果割边是源->X,表示x不要选(是割边,必然价值在路径上最小),若割边是Y-汇点,同理:若割边是X->Y,则表示选Y点且选X点, 割为w( 2*(X&Y) ). 自己的确还没有理解其本质

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点,轻轻松松构造了最大权闭合图.简单题.分分钟搞定. hdu3917 :road  constructions :这题题目看了半天没理解...感觉描述的不好...一个有向图,每条路有响应公司承保,若选了该公司,那么该公司的路必需全部选,还有,该公司的承保的路的下面的一条路对应公司也要选,求最大获利.构

CodeForces 22D Segments 排序水题

题目链接:点击打开链接 右端点升序,取右端点 暴力删边 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10

拓扑排序 --- 模板题

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10536    Accepted Submission(s): 4120 Problem Description 有 N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,