Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D4F70133616B for ; Fri, 11 Feb 2011 16:47:58 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 39589-08 for ; Fri, 11 Feb 2011 20:47:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exprod6og115.obsmtp.com (exprod6og115.obsmtp.com [64.18.1.35]) by mail.postgresql.org (Postfix) with SMTP id 3385C1337B90 for ; Fri, 11 Feb 2011 16:47:49 -0400 (AST) Received: from source ([66.152.196.38]) (using TLSv1) by exprod6ob115.postini.com ([64.18.5.12]) with SMTP ID DSNKTVWgdYywfjUhSN2V5UyVUeeI7swqcWNo@postini.com; Fri, 11 Feb 2011 12:47:50 PST Received: from EXCHMAIL.bzzagent.lan ([172.16.92.56]) by EXCHCAS.bzzagent.lan ([192.168.1.22]) with mapi; Fri, 11 Feb 2011 15:47:48 -0500 From: Aaron Burnett To: "pgsql-sql@postgresql.org" Date: Fri, 11 Feb 2011 15:47:45 -0500 Subject: Re: Drawing a blank on some SQL Thread-Topic: [SQL] Drawing a blank on some SQL Thread-Index: AcvKJ41NhRj8X4JRQWasd24zgjHNPAABWHYa Message-ID: In-Reply-To: Accept-Language: en-US Content-Language: en X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: multipart/alternative; boundary="_000_C97AEE81233E8aburnettbzzagentcom_" MIME-Version: 1.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-4.199 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_MED=-2.3 X-Spam-Level: X-Archive-Number: 201102/57 X-Sequence-Number: 35133 --_000_C97AEE81233E8aburnettbzzagentcom_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thank you all very much for your help. The suggestion from Osvaldo below was the best for my situation (not having= any soret of xref table to join)... Best Regards, Aaron On 2/11/11 1:09 PM, "Osvaldo Kussama" wrote: 2011/2/11, Aaron Burnett : > > Hi, > > I'm just drawing a blank entirely today and would appreciate some help on > this. > > The long and short; there are 12 distinct activities that need to be quer= ied > on a weekly basis: > > SELECT count(activity_id), activity_id > FROM foo_activity > WHERE created >=3D '01/01/2011' and created < '01/08/2011' > GROUP BY 2 > ORDER BY 2; > > It gives me this answer, which is correct: > > count | activity_id > -------+--------------------- > 1502 | 1 > 11 | 2 > 2 | 3 > 815 | 4 > 4331 | 7 > 30 | 9 > 1950 | 10 > 7 | 11 > 67 | 12 > > But what I need to see is if there are no activities for the particular > activity_id that week, that it lists the count as 0 and lists the > activity_id associated like this: > > count | activity_id > -------+--------------------- > 1502 | 1 > 11 | 2 > 2 | 3 > 815 | 4 > 0 | 5 > 0 | 6 > 4331 | 7 > 0 | 8 > 30 | 9 > 1950 | 10 > 7 | 11 > 67 | 12 > > Thanking you in advance for any help on this. The caffiene seems to be no= t > working well today. > Try: SELECT sum(case when created >=3D '2011-01-01' and created < '2011-01-08' then 1 else 0 end), activity_id FROM foo_activity GROUP BY 2 ORDER BY 2; Osvaldo --_000_C97AEE81233E8aburnettbzzagentcom_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Re: [SQL] Drawing a blank on some SQL

Thank you all very much for your help.

The suggestion from Osvaldo below was the best for my situation (not having= any soret of xref table to join)...

Best Regards,

Aaron


On 2/11/11 1:09 PM, "Osvaldo Kussama" <osvaldo.kussama@gmail.com> wrote:

2011/2/11, Aaron Burnett <aburnett@bzzagent.com>:
>
> Hi,
>
> I'm just drawing a blank entirely today and would appreciate some help= on
> this.
>
> The long and short; there are 12 distinct activities that need to be q= ueried
> on a weekly basis:
>
> SELECT count(activity_id), activity_id
> FROM foo_activity
> WHERE created >=3D '01/01/2011' and created < '01/08/2011'
> GROUP BY 2
> ORDER BY 2;
>
> It gives me this answer, which is correct:
>
>  count | activity_id
> -------+---------------------
>   1502 |         &nb= sp;         1
>     11 |        = ;           2
>      2 |       =             3 >    815 |        &nbs= p;          4
>   4331 |         &nb= sp;         7
>     30 |        = ;           9
>   1950 |         &nb= sp;        10
>      7 |       =            11
>     67 |        = ;          12
>
> But what I need to see is if there are no activities for the particula= r
> activity_id that week, that it lists the count as 0 and lists the
> activity_id associated like this:
>
>  count | activity_id
> -------+---------------------
>   1502 |         &nb= sp;         1
>     11 |        = ;           2
>      2 |       =             3 >    815 |        &nbs= p;          4
>      0 |       =             5 >      0 |       =             6 >   4331 |         &nb= sp;         7
>      0 |       =             8 >     30 |        = ;           9
>   1950 |         &nb= sp;        10
>      7 |       =            11
>     67 |        = ;          12
>
> Thanking you in advance for any help on this. The caffiene seems to be= not
> working well today.
>


Try:
SELECT sum(case when created >=3D '2011-01-01' and created <
'2011-01-08' then 1 else 0 end), activity_id
FROM foo_activity
GROUP BY 2
ORDER BY 2;

Osvaldo

--_000_C97AEE81233E8aburnettbzzagentcom_--