说我有一张这样的桌子
| id | user_id | event_id | created_at |
|----|---------|----------|------------|
| 1 | 5 | 10 | 2015-01-01 |
| 2 | 6 | 7 | 2015-01-02 |
| 3 | 3 | 8 | 2015-01-01 |
| 4 | 5 | 9 | 2015-01-04 |
| 5 | 5 | 10 | 2015-01-02 |
| 6 | 6 | 1 | 2015-01-01 |
我希望能够为每个用户生成一个事件计数器。所以我的结果是:
| counter | user_id | event_id | created_at |
|---------|---------|----------|------------|
| 1 | 5 | 10 | 2015-01-01 |
| 1 | 6 | 7 | 2015-01-02 |
| 1 | 3 | 8 | 2015-01-01 |
| 2 | 5 | 9 | 2015-01-04 |
| 3 | 5 | 10 | 2015-01-02 |
| 2 | 6 | 1 | 2015-01-01 |
答案 0 :(得分:2)
一个想法是自己加入表,group by
复制其他RDBMS中可用的row_number() over..
函数。
选中此Rextester Demo并查看第二个查询,了解inner join
在这种情况下的工作原理。
select t1.user_id,
t1.event_id,
t1.created_at,
count(*) as counter
from your_table t1
inner join your_table t2
on t1.user_id=t2.user_id
and t1.id>=t2.id
group by t1.user_id,
t1.event_id,
t1.created_at
order by t1.user_id,t1.event_id;
输出:
+---------+----------+------------+---------+
| user_id | event_id | created_at | counter |
+---------+----------+------------+---------+
| 3 | 8 | 01-01-2015 | 1 |
| 5 | 10 | 01-01-2015 | 1 |
| 5 | 10 | 02-01-2015 | 3 |
| 5 | 9 | 04-01-2015 | 2 |
| 6 | 1 | 01-01-2015 | 2 |
| 6 | 7 | 02-01-2015 | 1 |
+---------+----------+------------+---------+
答案 1 :(得分:1)
尝试以下方法:
select counter,
xx.user_id,
xx.event_id,
xx.created_at
from xx
join (select a.id,
a.user_id,
count(*) as counter
from xx as a
join xx as b
on a.user_id=b.user_id
and b.id<=a.id
group by 1,2) as counts
on xx.id=counts.id
使用联接为每个ID生成行,并在其下方为该用户提供所有其他较低的ID并对其进行计数。
答案 2 :(得分:1)
试试这个:
子查询将有助于解决这个问题。
select (select count(*) from user_event iue where iue.user_id == oue.user_id) as counter,
oue.user_id,
oue.event_id,
oue.created_at
from user_event oue
答案 3 :(得分:1)
您可以尝试将变量用作表,将其与源表交叉连接,并在用户ID更改时重置。
SELECT @counter := CASE
WHEN @user = user_id THEN @counter + 1
ELSE 1
END AS counter,
@user := user_id AS user_id,
event_id,
created_at
FROM your_table m,
(SELECT @counter := 0,
@user := '') AS t
ORDER BY user_id;
我创建了一个演示here