使用 group by 和 order by 时获得最低值
get lowest value while using group by and order by
我有一个 table 看起来有大约 50 行。 table 保存田径比赛的结果。这是结构:
我正在做的是为每个事件获取前十名列表。这是我目前所拥有的:
SELECT * FROM Times WHERE event='1600m' GROUP BY fname, lname ORDER BY time, event LIMIT 10
每个人每次活动只能出现在列表中一次。这行得通,但是如果您始终查看下方 1600 米的时间...
可以看到Kyle Rummens
的时间是4:32:00
。下图是上面sql语句的结果:
Kyle 的时间是 4:36:00
。
怎样才能做到1600米最快的前10名event
,每个人只能上榜一次,而且是每个人最快的时间
编辑:我被要求以文本而不是图像的形式添加我的结果,所以它们是:
这是上述查询的结果:
fname lname event time meetName date
====== =========== ===== ======== ==================== ==========
Jeron Tucker 1600m 04:09:00 Coaches Meet 2015-02-12
Chris Hughes 1600m 04:27:00 Distance Chalenge 2015-02-10
Andrew Rummens 1600m 04:28:00 Simplot Games 2015-02-20
Kyle Rummens 1600m 04:36:00 Simplot Games 2015-02-19
Alex Southerland 1600m 04:52:00 Coaches Meet 2015-02-12
Canyon Schmerse 1600m 04:54:00 Arcadia Invitational 2015-05-12
Joseph Stone 1600m 05:08:00 Coaches Meet 2015-02-12
Teron Tucker 1600m 05:09:00 Harvard Track Meet 2015-02-19
Little Jacob 1600m 05:12:00 Arcadia Invitational 2015-05-12
Connor Kleinman 1600m 05:54:00 BYU Invitational 2015-04-03
在SELECT之后添加单词DISTINCT
SELECT DISTINCT * FROM Times WHERE event='1600m' GROUP BY fname, lname ORDER BY MIN(time) LIMIT 10
对每个游泳者使用 min(time)
来找出每个游泳者的最快时间:
SELECT fname, lname, min(time) as tune
FROM Times
WHERE event = '1600m'
GROUP BY fname, lname
ORDER BY time
LIMIT 10
也不需要按 event
订购,因为它总是 '1600m'
。
使用相关子查询:
SELECT t1.*
FROM Times t1
WHERE t1.event='1600m' AND t1.time = (SELECT MAX(time)
FROM Times t2
WHERE t2.event='1600m' AND
t1.fname = t2.fname AND
t1.lname = t2.fname)
ORDER BY t1.time
LIMIT 10;
我有一个 table 看起来有大约 50 行。 table 保存田径比赛的结果。这是结构:
我正在做的是为每个事件获取前十名列表。这是我目前所拥有的:
SELECT * FROM Times WHERE event='1600m' GROUP BY fname, lname ORDER BY time, event LIMIT 10
每个人每次活动只能出现在列表中一次。这行得通,但是如果您始终查看下方 1600 米的时间...
可以看到Kyle Rummens
的时间是4:32:00
。下图是上面sql语句的结果:
Kyle 的时间是 4:36:00
。
怎样才能做到1600米最快的前10名event
,每个人只能上榜一次,而且是每个人最快的时间
编辑:我被要求以文本而不是图像的形式添加我的结果,所以它们是:
这是上述查询的结果:
fname lname event time meetName date
====== =========== ===== ======== ==================== ==========
Jeron Tucker 1600m 04:09:00 Coaches Meet 2015-02-12
Chris Hughes 1600m 04:27:00 Distance Chalenge 2015-02-10
Andrew Rummens 1600m 04:28:00 Simplot Games 2015-02-20
Kyle Rummens 1600m 04:36:00 Simplot Games 2015-02-19
Alex Southerland 1600m 04:52:00 Coaches Meet 2015-02-12
Canyon Schmerse 1600m 04:54:00 Arcadia Invitational 2015-05-12
Joseph Stone 1600m 05:08:00 Coaches Meet 2015-02-12
Teron Tucker 1600m 05:09:00 Harvard Track Meet 2015-02-19
Little Jacob 1600m 05:12:00 Arcadia Invitational 2015-05-12
Connor Kleinman 1600m 05:54:00 BYU Invitational 2015-04-03
在SELECT之后添加单词DISTINCT
SELECT DISTINCT * FROM Times WHERE event='1600m' GROUP BY fname, lname ORDER BY MIN(time) LIMIT 10
对每个游泳者使用 min(time)
来找出每个游泳者的最快时间:
SELECT fname, lname, min(time) as tune
FROM Times
WHERE event = '1600m'
GROUP BY fname, lname
ORDER BY time
LIMIT 10
也不需要按 event
订购,因为它总是 '1600m'
。
使用相关子查询:
SELECT t1.*
FROM Times t1
WHERE t1.event='1600m' AND t1.time = (SELECT MAX(time)
FROM Times t2
WHERE t2.event='1600m' AND
t1.fname = t2.fname AND
t1.lname = t2.fname)
ORDER BY t1.time
LIMIT 10;