- Sep 10, 2001
- 11,711
- 8
- 81
Suppose you have the following situation,
a table named 'activity' with three columns as follows:
PAGE_ID - NUMBER
USER_ID - NUMBER
LAST_ACTIVE_DT - DATE/TIME
a web site which has server side code which inserts/updates a row in above table each time they visit or revisit a particular page.
now, you want a report where you get the the last active page the user was on.
how do you do this in one select query?
I cannot do:
select user_id, max(last_active_dt) from activity group by user_id
because while the resulting date would be correct I have no idea what page it was associated with
then I cannot add page_id into the group by expression because then it would separate the results by page but I only want the one last page a user visited and the date of that visit.
Example: data as follows: (bah, attach code is teh suck)
PAGE_ID | USER_ID | LAST_ACTIVE_DT |
--------+---------+----------------|
1 | 1 | 7/10/2008 |
2 | 1 | 8/15/2008 |
3 | 1 | 7/25/2008 |
2 | 2 | 9/15/2008 |
4 | 2 | 8/10/2008 |
--------+---------+----------------+
And the result of query would be:
2, 1, 8/15/2008
2, 2, 9/15/2008
a table named 'activity' with three columns as follows:
PAGE_ID - NUMBER
USER_ID - NUMBER
LAST_ACTIVE_DT - DATE/TIME
a web site which has server side code which inserts/updates a row in above table each time they visit or revisit a particular page.
now, you want a report where you get the the last active page the user was on.
how do you do this in one select query?
I cannot do:
select user_id, max(last_active_dt) from activity group by user_id
because while the resulting date would be correct I have no idea what page it was associated with
then I cannot add page_id into the group by expression because then it would separate the results by page but I only want the one last page a user visited and the date of that visit.
Example: data as follows: (bah, attach code is teh suck)
PAGE_ID | USER_ID | LAST_ACTIVE_DT |
--------+---------+----------------|
1 | 1 | 7/10/2008 |
2 | 1 | 8/15/2008 |
3 | 1 | 7/25/2008 |
2 | 2 | 9/15/2008 |
4 | 2 | 8/10/2008 |
--------+---------+----------------+
And the result of query would be:
2, 1, 8/15/2008
2, 2, 9/15/2008
