473,487 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

choosing a particular row from grouped rows

sks
I have a table which contains two strings and a date.

page_views
-----------------
sessionId
page
timestamp
This is added to each time someone views a page on our site. I want to
select the last page each user looked at, so I need to group on the
sessionId whilst getting it to select the one with the highest date
timestamp. Any help ?
Jul 20 '05 #1
4 1439
"sks" <sk******************@tekheadsdotcodotuk.co.uk> wrote in message
news:Td********************@eclipse.net.uk...
I have a table which contains two strings and a date.

page_views
-----------------
sessionId
page
timestamp
This is added to each time someone views a page on our site. I want to
select the last page each user looked at, so I need to group on the
sessionId whilst getting it to select the one with the highest date
timestamp. Any help ?


Something like...

select page from page_views where sessionID=<your value> order by timestemp
desc limit 1

if you're using MySQL
Jul 20 '05 #2
"sks" <sk******************@tekheadsdotcodotuk.co.uk> wrote in message
news:Td********************@eclipse.net.uk...
I have a table which contains two strings and a date.

page_views
-----------------
sessionId
page
timestamp
This is added to each time someone views a page on our site. I want to
select the last page each user looked at, so I need to group on the
sessionId whilst getting it to select the one with the highest date
timestamp. Any help ?


Oh, you may have wanted a list for all users. My misunderstanding.

select * from page_views a where timestamp = (select max(b.timestamp) from
page_views b where a.sessionId=b.sessionId group by sessionId)

may do what you want depending upon what database you're using. It may also
be a bit slow.

- Virgil
Jul 20 '05 #3
Virgil Green wrote:
select * from page_views a where timestamp = (select max(b.timestamp) from
page_views b where a.sessionId=b.sessionId group by sessionId)


A couple of nitpicks:

1. 'group by sessionId' is unnecessary, because we know that each
execution of the subquery is restricted to one sessionId.

2. Subqueries work only in MySQL 4.1 and later, and sks didn't say which
version of MySQL he/she is using. I don't know how one could do this
type of query without using a correlated subquery.
Sks may have to do this in multiple queries:

select max(timestamp) as maxtimestamp, sessionId
from page_views group by sessionId

then loop through that result set:
select * from page_views
where sessionId = ? and timestamp = ?

Bill K.
Jul 20 '05 #4
"Bill Karwin" <bi**@karwin.com> wrote in message
news:ck*********@enews2.newsguy.com...
Virgil Green wrote:
select * from page_views a where timestamp = (select max(b.timestamp) from page_views b where a.sessionId=b.sessionId group by sessionId)
A couple of nitpicks:

1. 'group by sessionId' is unnecessary, because we know that each
execution of the subquery is restricted to one sessionId.


Habit... when using columnar functions, I almost always include the 'group
by' since it is almost always being used. Rarely to I use a columnar
function other than count() expecting to return a single row.
2. Subqueries work only in MySQL 4.1 and later, and sks didn't say which
version of MySQL he/she is using. I don't know how one could do this
type of query without using a correlated subquery.
And so sks should learn to post his/her version. In fact, we are only
assuming MySQL is being used because of the group in which it was posted.
I've seen enough posts where someone was simply seeking SQL help and decided
this forum would be the place to get it.
Sks may have to do this in multiple queries:

select max(timestamp) as maxtimestamp, sessionId
from page_views group by sessionId

then loop through that result set:
select * from page_views
where sessionId = ? and timestamp = ?

Bill K.

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1226
by: John Smith | last post by:
How do I choose/find the function that is right for what I want to do. I have been using the phpManual which is one of the reasons I came to PHP. I would like to know how to search for a function...
4
2371
by: Jonas Hei | last post by:
I need to decided between Standard and Enterprise Edition (Cost is a criteria - but its secondary to performance - <!--and I am not paying for it myself-->) The server spec under consideration:...
15
1420
by: Ant | last post by:
Hi, This might seem like a strange question but I'm wondering how other developers go about choosing the appropriate Exception objects to use in their catch statements. Currently, I choose them...
1
1250
by: Olaf Rabbachin | last post by:
Hi folks, I have a table in an Access-DB that has a date-field (date + time). I'd like to use a DataSet + DataGrid to achieve a grouped/hierarchical display where the user would work his/her way...
6
1561
by: Ian Ribas | last post by:
Hello, This is probably a common problem, but I couldn't really find a direct answer in the archives (or maybe just couldn't find one that satisfied me ;-). I created an index specifically to...
0
1449
by: baaul | last post by:
I need to INSERT rows from #temp table into #final table with grp_code and bap_id combination. Thanks. Create Table #tmp ( sort_id int default 0, grp_Code int default 1,...
3
1268
by: arghhh | last post by:
Hi, If i have the following table ( this is an example...) Class Type A A112 A A2 A A108 A A218 A A328
1
1093
by: ericjb | last post by:
I have a table with many columns in it. Each row has an ORDER_ID, ORDER_LINE_ID, START_DATE, and END_DATE. The combination of said columns make the row unique. Currently, the row has a start date...
0
1047
by: nityaprashant | last post by:
Hello.. could u plz help me? i have template column inside grid view.. in that one link button called Remove this? when i click on that link button confirmation box like "Are u sure u want...
1
1176
by: suzacaru7 | last post by:
HI, I am doing a database for my beutition shop. I created a table called slimming, which i store the clients info (including ID card number), the date of the appointment, the type of therapy...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.