下面是我桌子的复制品。如下所示,该行按ID和日期排序。
id | date | points
1 Jan 1, 2014 110
2 Feb 12, 2014 20
3 Mar 23, 2014 40
4 Apr 10, 2014 80
5 May 20, 2014 30
6 June 3, 2014 60
7 Jan 15, 2014 10
8 Mar 5, 2014 70
9 Feb 8, 2014 50
10 June 10, 2014 5
我的目标是按50到10之间的点(从最高到最低)订购行,并且到目前为止也是最短的。我怎样才能做到这一点?
必须包含所有行作为最终结果
答案 0 :(得分:0)
类似的东西:
SELECT *, CASE WHEN (points >= 10 AND points <= 50) THEN 1 ELSE 0 END AS order_points
FROM points
ORDER BY
order_points DESC,
CASE
WHEN order_points = 1 THEN points
END DESC, DATEDIFF( date, NOW() )
答案 1 :(得分:0)
有点猜测:
select a_date,points
from t
order by case when points between 10 and 50 then -1 else 1 end * points
, abs(DATEDIFF( a_date, now()));
对于您的数据的子集:
+------------+--------+
| a_date | points |
+------------+--------+
| 2014-03-23 | 40 |
| 2014-02-12 | 20 |
| 2014-06-10 | 5 |
| 2014-04-10 | 80 |
| 2014-01-01 | 110 |
+------------+--------+
答案 2 :(得分:0)
SELECT *
FROM MyTable
order by CASE WHEN Points BETWEEN 10 AND 50 THEN 0 ELSE 1 END, TheDate desc;
ID TheDate Points
-- --------- ------
5 20-MAY-14 30
3 23-MAR-14 40
2 12-FEB-14 20
9 08-FEB-14 50
7 15-JAN-14 10
10 10-JUN-14 5
6 03-JUN-14 60
4 10-APR-14 80
8 05-MAR-14 70
1 01-JAN-14 110