MySQL数据库表:
create table t (
visitor_id int(11),
activity_type varchar(10),
date date
);
行:
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-1-1');
insert into t (visitor_id, activity_type, date) values (1, 'event', '2012-1-2');
insert into t (visitor_id, activity_type, date) values (2, 'hit', '2012-1-2');
insert into t (visitor_id, activity_type, date) values (2, 'event', '2012-3-5');
insert into t (visitor_id, activity_type, date) values (2, 'hit', '2012-3-2');
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-3-5');
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-2-1');
我想编写一个查询来检索用户、活动类型和活动发生顺序的数据转储,如下所示:
visitor_id, activity_type, Position
1, hit, 1
1, event, 2
1, hit, 3
1, hit, 4
2, hit, 1
2, hit, 2
2, event, 3
到目前为止,我已经编写了以下解决方案:
select visitor_id, activity_type, 'Position'
from t1
order by visitor_id, date
;
困难的部分是列位置。这应该代表该访客 ID 的行顺序中的位置。有什么方法可以确定位置吗?