Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 8F7D09F9756 for ; Mon, 8 Oct 2007 08:33:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 17705-03 for ; Mon, 8 Oct 2007 08:32:42 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.175]) by postgresql.org (Postfix) with ESMTP id B230B9F9753 for ; Mon, 8 Oct 2007 08:32:57 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id j3so816542ugf for ; Mon, 08 Oct 2007 04:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:subject:cc:in-reply-to:mime-version:content-type:references; bh=jXJMCl7iRBZVq4NMX5KbQb7KtdOOlIM2nZ8y20h62wg=; b=A+1g6XB+C3FjZtNxTdKSZQjwCOgcjwaJFPD6IP7H0T2MMeYVGbSUMMeHy64B6QFihhyzcMV1GPxMXrc/Z/Dw6gyDAB6AEHmhaYJZkjwgn5fZGCRAuc3QzQQ8IRnJdjlUlJlzZRqqUWaqe8GTSpHqHfxa336kYT91Jk4DENi1z/M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:subject:cc:in-reply-to:mime-version:content-type:references; b=VHW1t+DZNch3JEAgdhBb4RExnexRUQxUqBQJwxVWovylwegpGzF5Id5OZCLPgM72Bp/l/w+ulttN9XwM+pET9bfj5nrHriYfvIcWe8i23/KkDsdAUzO6pMYPXReBhrQM+er3Ka3Sn/czuHLz7xEeilTEwfi8LycRg0hK95n4IEM= Received: by 10.78.184.2 with SMTP id h2mr9961169huf.1191843175090; Mon, 08 Oct 2007 04:32:55 -0700 (PDT) Received: by 10.78.146.6 with HTTP; Mon, 8 Oct 2007 04:32:55 -0700 (PDT) Message-ID: Date: Mon, 8 Oct 2007 12:32:55 +0100 From: "Peter Childs" Subject: Re: Get different sums from the same table in one query Cc: pgsql-sql@postgresql.org In-Reply-To: <20071008105422.GC15336@a-kretschmer.de> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_41619_6069601.1191843175088" References: <470A0B20.40203@gmail.com> <20071008105422.GC15336@a-kretschmer.de> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/39 X-Sequence-Number: 29411 ------=_Part_41619_6069601.1191843175088 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 08/10/2007, A. Kretschmer wrote: > > am Mon, dem 08.10.2007, um 12:49:04 +0200 mailte Dani Casta=F1os folgend= es: > > Hi all! > > > > I want to put this three queries in only one... Take a look that the > > only thing that changes is the message_type_id. Any suggestions?? > > Yes, no problem, You can use UNION (ALL) for such. Not sure thats what he wants at all. Anyway Union all is going to produce a very slow way of doing this needing to go back to the data 3 times where as this only goes once. SELECT TO_CHAR( statistics_date, 'DD/MM/YYYY') AS date_in, sum(case when message_type_id in (4,5) then total_num_messages else 0 end) as fourorfive, sum(case when message_type_id in (6) then total_num_messages else 0 end) as six, sum(case when message_type_id in (21) then total_num_messages else 0 end) a= s twentyone FROM statistics_daily WHERE statistics_date =3D CURRENT_DATE AND telecom_operator_id <> 0 AND telephone_number IN ( $numbers ) GROUP BY date_in You may have to play to get exactly what you want but here worse case your only doing one scan of the table not three... You may want to use views to simplify things. Peter Childs Andreas > -- > Andreas Kretschmer > Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) > GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net > > ---------------------------(end of broadcast)--------------------------- > TIP 7: You can help support the PostgreSQL project by donating at > > http://www.postgresql.org/about/donate > ------=_Part_41619_6069601.1191843175088 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline

On 08/10/2007, A. Kretschmer <andreas.kretschmer@schollglas.com> wrote: am  Mon, dem 08.10.2007, um 12:49:04 +0200 mailte Dani Casta=F1os= folgendes:
> Hi all!
>
> I want to put this three querie= s in only one... Take a look that the
> only thing that changes is th= e message_type_id. Any suggestions??

Yes, no problem, You can use UNION (ALL) for such.

Not sure thats what he wants at all. Anyway Union all is going to = produce a very slow way of doing this needing to go back to the data 3 time= s where as this only goes once.


 SELECT TO_CHAR( statistics_date, 'DD/MM/YYYY') AS= date_in,
sum(case when message_type_id in (4,5) then total_num_messages= else 0 end) as fourorfive,
sum(case when message_type_id in (6) then to= tal_num_messages else 0 end) as six,
sum(case when message_type_id in (21) then total_num_messages else 0 en= d) as twentyone
        FROM statistics_daily
&= nbsp;       WHERE statistics_date =3D CURRENT_DATE
 = ;       AND telecom_operator_id <> 0
        AND telephone_number IN ( $numbers )
 = ;       GROUP BY date_in

You may have to play to ge= t exactly what you want but here worse case your only doing one scan of the= table not three... You may want to use views to simplify things.

Peter Childs


Andreas
--
Andreas Kretschmer
Kontakt:  H= eynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

-------= --------------------(end of broadcast)---------------------------
TIP 7:= You can help support the PostgreSQL project by donating at

           &= nbsp;    http://www.postgresql.org/about/donate

------=_Part_41619_6069601.1191843175088--