Help me with this tricky join

From: Jay <josip(dot)2000(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Help me with this tricky join
Date: 2010-03-19 16:33:57
Message-ID: f0cd2b61-2540-4a74-890b-c6464a8895c6@15g2000yqi.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I'm somewhat new to SQL so I need a bit of help with this problem. So
I have 2 tables: "selection" and "master", both have two columns in
each: "user_id" and "date".

The "selection" contains one row for each "user_id" and depicts _one_
"date" value for each user.
The "master" contains all "date" changes for each "user_id". I.e.,
there are many dates for each "user_id". It is a history of previous
occurrences.

Now, I want to add a 3rd column to the "selection" table that is the
"date" value from one step back for each "user_id". I.e., if the
"master" contains:

User1 20010101
User1 20000101
User1 19990101
User1 19970101

for User1, and the "selection" is

User1 19990101

I want this to become:

User1 20000101 19990101

How do I do this? A simple join wont do it since it is dependent on
what value "date" is for each user..
Moreover, I know for a fact that the "date" in "selection" is the
second largest value -> I want to add the 3rd largest value from
"master" for each user.

BR,
Jay

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jay 2010-03-19 16:36:21 Re: Help me with this tricky join
Previous Message Leonardo F 2010-03-19 16:18:05 Re: Order of Daily VACUUM, CLUSTER, REINDEX