SQL Assistance - Mailing list pgsql-novice

From Chris Campbell
Subject SQL Assistance
Date
Msg-id 453A24085F801842AEA8D0B6B269065D032BED41ACD1@HDMC.cds.local
Whole thread Raw
Responses Re: SQL Assistance  (Michael Swierczek <mike.swierczek@gmail.com>)
Re: SQL Assistance  (David Raznick <david.raznick@okfn.org>)
Re: SQL Assistance  (Jayadevan M <maymala.jayadevan@gmail.com>)
List pgsql-novice

Greetings,

 

I’m struggling with a bit of SQL here and am looking for ideas on how to resolve it. 

 

Given the following query:

 

Select ta.accountname, ta.appealname,

coalesce(CASE WHEN ta.fk_gifttype=1 THEN Sum(ta.appealgiftamount) ELSE 0::decimal END,0) as pledgetotal,

coalesce(CASE WHEN ta.fk_gifttype=3 THEN Sum(ta.appealgiftamount) ELSE 0::decimal END,0) as paymenttotal

FROM ds1.tranappeal ta

GROUP BY ta.accountname, ta.appealname, ta.fk_gifttype

ORDER BY accountname

 

What I want to end up with is one row per account name with the correct pledge and payment total. 

 

What I’m receiving in most cases is multiple lines per account name.  The reason is because I’m being forced to include the fk_gifttype field in the Group By.  Because there are other gifttype codes, those rows are being included with zero amounts. 

 

I also tried using a sub select but that didn’t work because “appealname” isn’t part of a scope.  As a result, I received the “total” pledge and payment regardless of appealname.

 

I’m wonder what other options I might have to get the desired results. 

 

Thank you,

 

Chris Campbell

Cascasde Data Solutions Inc.

ccampbell@cascadeds.com

 

pgsql-novice by date:

Previous
From: "Greg Sabino Mullane"
Date:
Subject: Re: Notification of minor release and/or Security Release
Next
From: Michael Swierczek
Date:
Subject: Re: SQL Assistance