From breydan@excite.com Thu Feb 23 18:06:10 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 705D79DC883 for ; Thu, 23 Feb 2006 14:06:10 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 36101-06 for ; Thu, 23 Feb 2006 14:06:09 -0400 (AST) X-Greylist: delayed 00:19:20.729476 by SQLgrey- Received: from excite.com (nn2.excitenetwork.com [207.159.120.56]) by postgresql.org (Postfix) with ESMTP id 23D199DC806 for ; Thu, 23 Feb 2006 14:06:07 -0400 (AST) Received: by xprdmxin.myway.com (Postfix, from userid 110) id 073502F616; Thu, 23 Feb 2006 12:46:44 -0500 (EST) To: pgsql-sql@postgresql.org Subject: Sum If Received: from [70.168.48.83] by xprdmailfe5.nwk.excite.com via HTTP; Thu, 23 Feb 2006 12:46:44 EST X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: ID = 5eafe5c87531a5d9e0ecaad7b286d21e Reply-To: breydan@excite.com From: "Daniel Hernandez" MIME-Version: 1.0 X-Sender: breydan@excite.com X-Mailer: PHP Content-Type: multipart/alternative; boundary="EXCITEBOUNDARY_000__391893a698969ab559cca31478aad1c7"; Content-Transfer-Encoding: 7bit Message-Id: <20060223174644.073502F616@xprdmxin.myway.com> Date: Thu, 23 Feb 2006 12:46:44 -0500 (EST) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=3.572 required=5 tests=[DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_POST=1.44, HTML_MESSAGE=0.001, RCVD_FAKE_HELO_DOTCOM=1.652] X-Spam-Score: 3.572 X-Spam-Level: *** X-Archive-Number: 200602/281 X-Sequence-Number: 24291 --EXCITEBOUNDARY_000__391893a698969ab559cca31478aad1c7 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi Guys,   I'm new on this group, and I have a question, is there a way to do a "sum if" (kind of)?What i want to do is the following.Select customers.custid, if (sales.itemname = 'candy', sum(sales.count)) as "Sales candies", if (sales.itemname = 'some', sum(sales.count)) as "Sales Some"from ...join ...where .....group by customers.custid ...Thanks in advanced, and best regards,Daniel Hernández.Tijuana, BC, México."More you learn, more you earn". _______________________________________________ Join Excite! - http://www.excite.com The most personalized portal on the Web! --EXCITEBOUNDARY_000__391893a698969ab559cca31478aad1c7 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi Guys,

   I'm new on this group, and I have a question, is there a way to do a "sum if" (kind of)?

What i want to do is the following.

Select customers.custid, if (sales.itemname = 'candy', sum(sales.count)) as "Sales candies", if (sales.itemname = 'some', sum(sales.count)) as "Sales Some"
from ...
join ...
where .....
group by customers.custid ...


Thanks in advanced, and best regards,


Daniel Hernández.
Tijuana, BC, México.
"More you learn, more you earn".


Join Excite! - http://www.excite.com
The most personalized portal on the Web!
--EXCITEBOUNDARY_000__391893a698969ab559cca31478aad1c7-- From jim@contactbda.com Thu Feb 23 18:09:43 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 4C1269DC99F for ; Thu, 23 Feb 2006 14:09:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 37361-05 for ; Thu, 23 Feb 2006 14:09:42 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from amanda.contactbda.com (ipn36372-f65123.cidr.lightship.net [216.204.66.227]) by postgresql.org (Postfix) with ESMTP id C9D889DC9EB for ; Thu, 23 Feb 2006 14:09:38 -0400 (AST) Received: from amanda.contactbda.com (amanda.contactbda.com [192.168.1.2]) by amanda.contactbda.com (8.12.11/8.12.11/Debian-3) with ESMTP id k1NI9Xe9029077; Thu, 23 Feb 2006 13:09:33 -0500 From: "Jim Buttafuoco" To: breydan@excite.com, pgsql-sql@postgresql.org Reply-To: jim@contactbda.com Subject: Re: Sum If Date: Thu, 23 Feb 2006 13:09:33 -0500 Message-Id: <20060223180822.M55494@contactbda.com> In-Reply-To: <20060223174644.073502F616@xprdmxin.myway.com> References: <20060223174644.073502F616@xprdmxin.myway.com> X-Mailer: Open WebMail 2.41 20040926 X-OriginatingIP: 192.168.1.1 (jim) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.129 required=5 tests=[AWL=0.129] X-Spam-Score: 0.129 X-Spam-Level: X-Archive-Number: 200602/282 X-Sequence-Number: 24292 try select ...,sum(case when sales.itemname = 'some' then sales.count else 0 end) as "Sales Candies" from your_table_here group by ... ---------- Original Message ----------- From: "Daniel Hernandez" To: pgsql-sql@postgresql.org Sent: Thu, 23 Feb 2006 12:46:44 -0500 (EST) Subject: [SQL] Sum If > Hi Guys, I'm new on this group, and I have a question, is there a way to do a "sum if" (kind of) > ?What i want to do is the following.Select customers.custid, if (sales.itemname = 'candy', sum(sales.count)) > as "Sales candies", if (sales.itemname = 'some', sum(sales.count)) as "Sales Some"from > ...join ...where .....group by customers.custid ...Thanks in advanced, and best regards,Daniel > Hernández.Tijuana, BC, México."More you learn, more you earn". > > _______________________________________________ > Join Excite! - http://www.excite.com > The most personalized portal on the Web! ------- End of Original Message ------- From breydan@excite.com Thu Feb 23 19:03:05 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 12AFB9DC9D5 for ; Thu, 23 Feb 2006 15:03:05 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49727-06 for ; Thu, 23 Feb 2006 15:03:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from excite.com (nn6.excitenetwork.com [207.159.120.60]) by postgresql.org (Postfix) with ESMTP id 095DB9DC801 for ; Thu, 23 Feb 2006 15:03:03 -0400 (AST) Received: by xprdmxin.myway.com (Postfix, from userid 110) id 5858C8A1E3; Thu, 23 Feb 2006 14:03:03 -0500 (EST) To: jim@contactbda.com, pgsql-sql@postgresql.org Subject: Re: Sum If Received: from [70.168.48.83] by xprdmailfe32.nwk.excite.com via HTTP; Thu, 23 Feb 2006 14:03:03 EST X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: ID = 5eafe5c87531a5d9e0ecaad7b286d21e Reply-To: breydan@excite.com From: "Daniel Hernandez" MIME-Version: 1.0 X-Sender: breydan@excite.com X-Mailer: PHP Content-Type: multipart/alternative; boundary="EXCITEBOUNDARY_000__aba80716577a5c999ba5f5ae5cf139d3"; Content-Transfer-Encoding: 7bit Message-Id: <20060223190303.5858C8A1E3@xprdmxin.myway.com> Date: Thu, 23 Feb 2006 14:03:03 -0500 (EST) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=3.212 required=5 tests=[AWL=-0.360, DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_POST=1.44, HTML_MESSAGE=0.001, RCVD_FAKE_HELO_DOTCOM=1.652] X-Spam-Score: 3.212 X-Spam-Level: *** X-Archive-Number: 200602/283 X-Sequence-Number: 24293 --EXCITEBOUNDARY_000__aba80716577a5c999ba5f5ae5cf139d3 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi Jim,   Thanks for the tip, It worked!!!!!Thanks a lot!!!Daniel Hernández.Tijuana, BC, México."More you learn, more you earn".try select ...,sum(case when sales.itemname = 'some' then sales.count else 0 end) as "Sales Candies"from your_table_heregroup by ... _______________________________________________ Join Excite! - http://www.excite.com The most personalized portal on the Web! --EXCITEBOUNDARY_000__aba80716577a5c999ba5f5ae5cf139d3 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi Jim,

   Thanks for the tip, It worked!!!!!

Thanks a lot!!!



Daniel Hernández.
Tijuana, BC, México.
"More you learn, more you earn".

try

select ...,sum(case when sales.itemname = 'some' then sales.count else 0 end) as "Sales Candies"
from your_table_here
group by ...


Join Excite! - http://www.excite.com
The most personalized portal on the Web!
--EXCITEBOUNDARY_000__aba80716577a5c999ba5f5ae5cf139d3--