Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id AF3E62E2DA2 for ; Thu, 13 Dec 2007 10:19:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 56484-08 for ; Thu, 13 Dec 2007 10:19:03 -0400 (AST) Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.182]) by postgresql.org (Postfix) with ESMTP id 7016C2E026A for ; Thu, 13 Dec 2007 10:19:03 -0400 (AST) Received: by wa-out-1112.google.com with SMTP id m38so1171120waf.5 for ; Thu, 13 Dec 2007 06:19:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=hqJ6E3FDu4cdGT8L4CUAVV4vWGHHiODpsiBSwSliqSo=; b=rtUGkPShZqhMjgCmGJzjBXp82dPEgmOAFp2CeZl5aLZdL5U5wlFv6T7IOsP+WUWPvMIt9fRL5/3OadmWR5swsGLRWlt9YK81qvqQAamtrRwUlCxvp9gOeCUNry+mMF7SC9kx1qS7dCWpmqff8aBFKeqP0EQmZ4I7ByES4cvh6OM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=ejgQ/k/lzpmwx50AtvtrrSinGZgDYzqy7IZn0PNfScLFScnEGXNTwO3i9bDgUGKjVxDk3HHq1EtUP/XjouhDaAjxqE2FKKekAZd4tQzyEBFiFPT0iQaXGCRyyvkEU6MpYaPGFRlv/SGRDr0nrLFfLfOP1P7UoQ5klUHO0owzvHs= Received: by 10.114.146.1 with SMTP id t1mr972356wad.20.1197555540336; Thu, 13 Dec 2007 06:19:00 -0800 (PST) Received: by 10.114.56.19 with HTTP; Thu, 13 Dec 2007 06:19:00 -0800 (PST) Message-ID: Date: Thu, 13 Dec 2007 19:49:00 +0530 From: "Ranbeer Makin" To: trinaths@intoto.com Subject: Re: SQL Query Cc: "Ashish Karalkar" , pggeneral In-Reply-To: <20071213130244.090cb587@1mc110.intotoind.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_35021_1292513.1197555540329" References: <401674.75312.qm@web94303.mail.in2.yahoo.com> <20071213130244.090cb587@1mc110.intotoind.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/900 X-Sequence-Number: 125643 ------=_Part_35021_1292513.1197555540329 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello All: Ashish wants latest output_date but less than now() >want to retrieve all the output_id which are having latest >output_date but less than now() (no future output_dates) The query written by Tirnath will return count of all output ids given parent id where output_date is less than now, it won't return rows for latest output_date. I propose the following solution: SELECT o.output_id FROM my_table o, parent_table p WHERE o.parent_id = p.parent_id AND o.output_date < now() AND NOT EXISTS ( SELECT out.output_date FROM my_table out WHERE out.parent_id = o.parent_id AND out.output_date < now () AND o.output_date < out.output_date ); The idea is get first maximum date which is less than now(). Assuming now() as the maximum date, the problem reduces to finding second maximum date. The nested query means: the date in context (outer query date) shouldn't be less than any date which is less than now(). This will give you the latest output date grouped by parent_id albeit there's no group clause used. :-) Let me know if there's any mistake. Cheers, Ranbeer Makin On Dec 13, 2007 6:32 PM, Trinath Somanchi wrote: > Hi , > > Try this out > > SELECT count(o.id) > FROM output_table o , parent_table p > WHERE o.pid=p.pid AND o_date < now() > GROUP BY p.pid ; > > > On Thu, 13 Dec 2007 10:00:56 +0000 (GMT) > Ashish Karalkar wrote: > > > Hello List member, > > I have a table which ha sfollowing structure > > > > my_table: > > ( > > output_id serial priimary key, > > parent_id int, > > output_date timesatmp > > ) > > > > parent_table: > > > > (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 > > Ashish... > > > > > > --------------------------------- > > Forgot the famous last words? Access your message archive online. > > Click here. > > -- > Trinath Somanchi , > (trinaths@intoto.com), > > > ******************************************************************************** > This email message (including any attachments) is for the sole use of the > intended recipient(s) > and may contain confidential, proprietary and privileged information. Any > unauthorized review, > use, disclosure or distribution is prohibited. If you are not the intended > recipient, > please immediately notify the sender by reply email and destroy all copies > of the original message. > Thank you. > > Intoto Inc. > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend > ------=_Part_35021_1292513.1197555540329 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello All:

Ashish wants latest output_date but less than now()

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

The query written by Tirnath will return count of all output ids given parent id where output_date is less than now, it won't return rows for latest output_date.

I propose the following solution:

SELECT o.output_id
FROM my_table o, parent_table p
WHERE o.parent_id = p.parent_id
AND o.output_date < now()
AND NOT EXISTS
(
 SELECT out.output_date
 FROM my_table out
 WHERE out.parent_id = o.parent_id
 AND out.output_date < now ()
 AND o.output_date < out.output_date
);

The idea is get first maximum date which is less than now(). Assuming now() as the maximum date, the problem reduces to finding second maximum date.
The nested query means: the date in context (outer query date) shouldn't be less than any date which is less than now().

This will give you the latest output date grouped by parent_id albeit there's no group clause used. :-)

Let me know if there's any mistake.
Cheers,
Ranbeer Makin




On Dec 13, 2007 6:32 PM, Trinath Somanchi <trinaths@intoto.com > wrote:
Hi ,

Try this out

SELECT count( o.id)
FROM   output_table o , parent_table p
WHERE  o.pid=p.pid AND o_date < now()
GROUP BY p.pid ;


On Thu, 13 Dec 2007 10:00:56 +0000 (GMT)
Ashish Karalkar < ashish_postgre@yahoo.co.in> wrote:

> Hello List member,
> I have a table which ha sfollowing structure
>
> my_table:
> (
>     output_id serial priimary key,
>     parent_id int,
>     output_date timesatmp
> )
>
> parent_table:
>
> (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
> Ashish...
>
>
> ---------------------------------
>  Forgot the famous last words? Access your message archive online.
> Click here.

--
Trinath Somanchi ,
(trinaths@intoto.com),

********************************************************************************
This email message (including any attachments) is for the sole use of the intended recipient(s)
and may contain confidential, proprietary and privileged information. Any unauthorized review,
use, disclosure or distribution is prohibited. If you are not the intended recipient,
please immediately notify the sender by reply email and destroy all copies of the original message.
Thank you.

Intoto Inc.


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

------=_Part_35021_1292513.1197555540329--