Skip site navigation (1) Skip section navigation (2)

Re: SQL Query

From: Mark Wimer <mwimer(at)usgs(dot)gov>
To: Ashish Karalkar <ashish_postgre(at)yahoo(dot)co(dot)in>
Cc: ashish(dot)karalkar(at)netcore(dot)co(dot)in,pgsql-novice(at)postgresql(dot)org
Subject: Re: SQL Query
Date: 2007-12-13 17:23:39
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-generalpgsql-novice
See if this works; the max aggregate in the subquery could be just about 
any function, since it's only going to join to one row. 

select max(m1.output_date), m1.parent_id, max(m2.output_id) as 
from my_table M1 inner join (select output_date, output_id, parent_ID from 
my_table) as M2
        on m1.output_date = m2.output_date and m1.parent_ID = m2.parent_ID 

Where M1.output_date < now()
Group by m1.parent_id

-- Mark

Ashish Karalkar <ashish_postgre(at)yahoo(dot)co(dot)in> 
Sent by: pgsql-novice-owner(at)postgresql(dot)org
12/13/2007 04:47 AM

pgsql-novice(at)postgresql(dot)org, ashish(dot)karalkar(at)netcore(dot)co(dot)in


Hello List member,
I have a table which ha sfollowing structure

    output_id serial priimary key,
    parent_id int,
    output_date timesatmp


(parent_id int,
parent desc

the my_table contains data for previous as well as future output dates.
for single parent there may be multiple rows.

I want to retrieve all the output_id  which are having latest output_date 
but less than now() (no future output_dates) group by  parent_id 

Thanks in advance

With Regards

 Chat on a cool, new interface. No download required. Click here.

In response to

  • SQL Query at 2007-12-13 09:47:36 from Ashish Karalkar

pgsql-novice by date

Next:From: A. KretschmerDate: 2007-12-13 20:24:17
Subject: Re: remote user
Previous:From: Tom LaneDate: 2007-12-13 16:37:30
Subject: Re: Is now() atomic?

pgsql-general by date

Next:From: Reg Me PleaseDate: 2007-12-13 17:36:43
Subject: COPY speedup
Previous:From: Scott MarloweDate: 2007-12-13 17:03:18
Subject: Re: Need to find out which process is hitting hda

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group