Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > PostgreSQL > Help with a Query!!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-03, 00:57
veronicab veronicab is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
Help with a Query!!!

Hello!

I am stuck with a query, thank you very much for your help!!!

I have a table (let's call it booksborrow) like this one:

create table booksborrow (
user varchar(20),
date_borrow int4,
book varchar(10),
comments text );

In the date_borrow field I save the timestamp.

Is there a way to make one query to get which is the last book every user has borrowed ?

I would need in the result set, just one row per user, containing the information of the last book they have borrowed (the record with the highest date_borrow for each user).

I have tried many queries but I just cannot get this one right.

Thank you very much for your help again!!!

Regards,
Veronica
__________________
Veronica Bendersky - Buenos Aires - Argentina

Last edited by veronicab : 07-15-03 at 00:59.
Reply With Quote
  #2 (permalink)  
Old 07-15-03, 08:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
select user, date_borrow, book, comments
from booksborrow ZZ
where date_borrow =
( select max(date_borrow)
from booksborrow
where user = ZZ.user )

this is a correlated subquery

each row in the outer table, the table with the alias ZZ (the correlation variable), is compared to all the other rows in its group

this group consists of those rows which have the same user


if you think about it procedurally -- start with the first row in the table, get all the rows that have the same user, sort them by date, take the top 1, get the next row in the table, get all the rows that have the same user, sort them by date, take the top 1, and so on -- then your brain will turn to tapioca

instead, think of what you want

the last date for each user

the users must be grouped

correlation is one way to do that

(GROUP BY is another, but in this example the correlated subquery is a lot neater)

so don't think about it procedurally, think about how to describe the results you want, and let the database figure out how to do it

rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 07-15-03, 10:07
veronicab veronicab is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
Thank you very much!!!

Could you tell me where I can find more information about correlated sub querys?

Regards,
Veronica
__________________
Veronica Bendersky - Buenos Aires - Argentina
Reply With Quote
  #4 (permalink)  
Old 07-15-03, 19:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
this is a great article --
http://www.bcarter.com/sap29.htm

you can find more via google, there's lots out there


rudy
Reply With Quote
  #5 (permalink)  
Old 07-16-03, 03:51
karim2k karim2k is offline
Registered User
 
Join Date: Apr 2003
Location: Tunisia
Posts: 192
Thumbs up cool

great SQL enjoyement !!
__________________
Open up
Take a look to my Blog http://www.rundom.com/karim2k
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On