Is the query I need even possible?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a table that looks like this:

page_id        visitor_id            timestamp
...
1                    3                       20030101060522
5                    8                       20030101060512
2                    6                       20030101060634
7                    3                       20030101060651
3                    5                       20030101060612  --> Earliest
record with this visitor id
8                    5                       20030101060738
21                  5                       20030101060759
1                    5                       20030101060810
7                    5                       20030101060827 --> Last record
with this visitor id
5                    10                     20030101060944
1                    6                       20030101061002
... etc


What I want to select is the PAGE_ID of the EARLIEST record and the PAGE_ID
of the LAST record, for each distinct visitor_id.  I cannot figure out how
to do this without multiple, very slow sql calls.

Currently I'm getting it like this:

SELECT DISTINCT(visitor_id) FROM (table)

Then going through each row with:

SELECT page_id FROM (table) WHERE visitor_id = '$visitor_id'  ORDER BY
date_time ASC LIMIT 1

to get the first and then:

SELECT page_id FROM (table) WHERE visitor_id = '$visitor_id'  ORDER BY
date_time DESC LIMIT 1

to get the last.

There must be an easier way I'm not seeing.  This, on the scale of thousands
of visitors, takes forever.  Is there any other way to do it?

TIA,

Mike





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux