Thread: Sum If
Hi Guys,<br /><br />�� I'm new on this group, and I have a question, is there a way to do a "sum if" (kind of)?<br /><br/>What i want to do is the following.<br /><br />Select customers.custid, if (sales.itemname = 'candy', sum(sales.count))as "Sales candies", if (sales.itemname = 'some', sum(sales.count)) as "Sales Some"<br />from ...<br />join...<br />where .....<br />group by customers.custid ...<br /><br /><br />Thanks in advanced, and best regards,<br /><br/><br />Daniel Hern�ndez.<br />Tijuana, BC, M�xico.<br />"More you learn, more you earn".<p><hr /><font face="geneva"size="2"><b>Join Excite! - <a href="http://www.excite.com" target="_blank">http://www.excite.com</a></b><br/>The most personalized portal on the Web!</font>
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" <breydan@excite.com> 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 -------
Hi Jim,<br /><br />�� Thanks for the tip, It worked!!!!!<br /><br />Thanks a lot!!!<br /><br /><br /><br />Daniel Hern�ndez.<br/>Tijuana, BC, M�xico.<br />"More you learn, more you earn".<br /><br /><blockquote style="border-left: 2pxsolid orange; padding-left: 7px; margin-left: 7px;">try <br /><br />select ...,sum(case when sales.itemname = 'some' thensales.count else 0 end) as "Sales Candies"<br />from your_table_here<br />group by ...<br /></blockquote><p><hr /><fontface="geneva" size="2"><b>Join Excite! - <a href="http://www.excite.com" target="_blank">http://www.excite.com</a></b><br/>The most personalized portal on the Web!</font>