The MySQL database table:
create table t (
visitor_id int(11),
activity_type varchar(10),
date date
);
The rows:
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');
I want to write a query to retrieve a data dump of users, activity type, and the order in which the activity occurred, that looks like the following:
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
So far I have written the following solution:
select visitor_id, activity_type, 'Position'
from t1
order by visitor_id, date
;
The hard part is the column Position. This should represent the position in the order of the rows for that visitor ID. Is there any way to determine Position?