我想查询最高点的排名,如果点相同,检查完成的时间是最短的。我的桌子是
+-------------------------------------------------+
| | id | user_id | point | finishtime | week | |
+-------------------------------------------------+
| | 1 | G1 | 1560 | 55 | 1 | |
| | 2 | G1 | 1560 | 43 | 1 | |
| | 3 | G1 | 1530 | 55 | 1 | |
| | 4 | G2 | 1760 | 45 | 1 | |
| | 5 | G3 | 1760 | 46 | 1 | |
| | 6 | G3 | 1330 | 25 | 2 | |
| | 7 | G4 | 360 | 65 | 1 | |
| | 8 | G2 | 1760 | 50 | 1 | |
预期结果是
+-------------------------------------------------+
| | id | user_id | point | finishtime | week | |
+-------------------------------------------------+
| | 4 | G2 | 1760 | 45 | 1 | |
| | 5 | G3 | 1760 | 46 | 1 | |
| | 2 | G1 | 1560 | 43 | 1 | |
| | 7 | G4 | 360 | 65 | 1 | |
| | 6 | G3 | 1330 | 25 | 2 | |
我尝试选择max作为该点,但它不会花费最短的完成时间。
我需要唯一的user_id结果组,按最高点获得订单,最短的完成时间。
我需要使用if else语句吗?
答案 0 :(得分:1)
这是一个可能的答案:
SELECT t1.* FROM t t1
JOIN (
SELECT t1.user_id, t1.week, t1.point, min(t1.finishtime) ft FROM t t1
LEFT JOIN t t2
ON t1.week = t2.week AND t1.user_id = t2.user_id AND t1.point < t2.point
WHERE t2.point IS NULL
GROUP BY t1.user_id, t1.week, t1.point
) t2 ON t1.user_id = t2.user_id AND t1.week = t2.week AND
t1.point = t2.point AND t1.finishtime = t2.ft
ORDER BY t1.week, t1.point DESC, t1.finishtime
<强>结果:
| ID | USER_ID | POINT | FINISHTIME | WEEK |
|----|---------|-------|------------|------|
| 4 | G2 | 1760 | 45 | 1 |
| 5 | G3 | 1760 | 46 | 1 |
| 2 | G1 | 1560 | 43 | 1 |
| 7 | G4 | 360 | 65 | 1 |
| 6 | G3 | 1330 | 25 | 2 |
小提琴here。
这基本上是每组最大的双重问题,因为你需要首先获得最高点的那些,然后获得最小完成时间的那些。
另一种解决方案是通过...来获取双组,但这将涉及第三个嵌套级别并试图避免它,所以选择了左连接解决方案。
答案 1 :(得分:0)
以下是使用JOINS
SELECT
DISTINCT
t1.user_id,
max(t2.point),
min(t2.finishtime),
min(t1.week)
FROM
t t1
JOIN t t2 ON ( t1.user_id = t2.user_id )
GROUP BY
t1.user_id
HAVING
MAX( t1.point ) = MAX( t2.point )
AND MIN( t1.finishtime ) = MIN( t2.finishtime )
ORDER BY
t1.user_id
结果:
| USER_ID | MAX(T2.POINT) | MIN(T2.FINISHTIME) | MIN(T1.WEEK) |
|---------|---------------|--------------------|--------------|
| G1 | 1560 | 43 | 1 |
| G2 | 1760 | 45 | 1 |
| G3 | 1760 | 25 | 1 |
| G4 | 360 | 65 | 1 |
感谢Mosty的SQLFIDDLE
答案 2 :(得分:0)
SELECT id, user_id, point, finishtime, week
FROM ( SELECT id, user_id, point, finishtime, week
FROM table_name
ORDER BY point DESC, finishtime ASC) AS h
GROUP BY user_id, week
ORDER BY week ASC
这可能是使用未记录的行为执行此操作的最短方法之一。请阅读以下内容。