Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 7170D1337BFC for ; Thu, 21 Apr 2011 20:30:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 34713-04 for ; Thu, 21 Apr 2011 23:30:32 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 6E07E1337BF5 for ; Thu, 21 Apr 2011 20:30:27 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QD3KL-0001jH-JH for pgsql-sql@postgresql.org; Fri, 22 Apr 2011 01:30:25 +0200 Received: from ip-118-90-133-108.xdsl.xnet.co.nz ([118.90.133.108]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 22 Apr 2011 01:30:25 +0200 Received: from jasen by ip-118-90-133-108.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 22 Apr 2011 01:30:25 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: help on select Date: 21 Apr 2011 23:15:26 GMT Organization: Dis (not Dat) Organisation Lines: 60 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-133-108.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/35 X-Sequence-Number: 35282 On 2011-04-20, Saulo Venâncio wrote: > --bcaec52e65e9b2f22304a15f3840 > Content-Type: text/plain; charset=ISO-8859-1 > Content-Transfer-Encoding: quoted-printable > > Hi guys, > I need your help. > I have a table called medidas, in this table i have some ocurrences that ha= > s > id_medida(primary key) id_ponto (sec_key) and also datetime field as > timestamp. > i would like to know from a set of idpontos, e.g. 10,11,23,24.... how can i > get the most recent date that is common to all?? > for example, if idponto das date 2011-02-03 but none of others have this > date in the db i dont want this. i want one common for all.. > thanks. the trick seems to be to GROUP BY datetime and to use a HAVING clause to reject the unwanted groups using count(distinct()) to ensure coverage of the list. -- a table create temp table medidas(id_medida serial,id_ponto integer,datetime timestamp); -- some test data. insert into medidas (id_ponto,datetime) select floor(random()*30+1),('today'::timestamp + floor(generate_series(0,100000)/10)*'1s'::interval); -- the query: -- note you need to paste the list of number in two different places -- in the query, postgres only counts the length once. select datetime from medidas where id_ponto in (10,11,23,24,27) group by datetime having count(distinct(id_ponto)) = array_length( array[10,11,23,24,27],1) order by datetime desc limit 1; -- confirmation select * from medidas where datetime = ( select datetime from medidas where id_ponto in (10,11,23,24,27) group by datetime having count(distinct(id_ponto)) = array_length(array[10,11,23,24,27],1) order by datetime desc limit 1 ) order by id_ponto; what's this for? Are you looking at keno results to see how recently your pick would have won? -- ⚂⚃ 100% natural