所以,我有这张桌子:
UNION ALL
我如何根据具有相同promo_id transac_id date
-------- ---------- ----
656265 213516 05/21/2016
656265 213520 05/21/2016
656265 213521 05/21/2016
656265 213530 05/22/2016
656265 213540 05/25/2016
895134 365124 06/01/2016
895134 365130 06/03/2016
895134 365135 06/04/2016
的{{1}}对transac_id
进行排名?
这就是我想看到的:
date
答案 0 :(得分:0)
模式:
create table t1
( id int not null,
thedate date not null
);
insert t1 (id,thedate) values (1,'2017-01-02'),(2,'2016-01-02'),(3,'2018-01-02');
查询:
SELECT id,thedate,@rn:=@rn+1 as rank
FROM t1
cross join (select @rn:=0) xParams
ORDER BY thedate;
+----+------------+------+
| id | thedate | rank |
+----+------------+------+
| 2 | 2016-01-02 | 1 |
| 1 | 2017-01-02 | 2 |
| 3 | 2018-01-02 | 3 |
+----+------------+------+
xParams
是派生表名。每个派生表都需要一个名称。由cross join
初始化变量(@rn)进行排名。