X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7E4408B9D4D for ; Tue, 22 Feb 2005 20:15:05 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 93498-10 for ; Tue, 22 Feb 2005 20:15:00 +0000 (GMT) Received: from web1-glo.biscit.net (web1-glo.biscit.net [82.195.96.53]) by svr1.postgresql.org (Postfix) with ESMTP id 3DCBA8B9D48 for ; Tue, 22 Feb 2005 20:14:59 +0000 (GMT) Received: from bolek.coachhouse (host62-7-158-109.webport.bt.net [62.7.158.109]) by web1-glo.biscit.net (8.10.2/8.10.2) with ESMTP id j1MKEsP20980 for ; Tue, 22 Feb 2005 20:14:54 GMT Received: (Exim 3.36) #1 (Debian)) protocol: esmtp id 1D3gQL-0004Uh-00 ; Tue, 22 Feb 2005 20:14:25 +0000 Message-ID: <421B92A0.3050506@numerixtechnology.de> Date: Tue, 22 Feb 2005 20:14:24 +0000 From: T E Schmitz Reply-To: mailreg@numerixtechnology.de User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: aggregate / group by question Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.153 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200502/252 X-Sequence-Number: 20363 Hello, I must apologize for not coming up with a more descriptive subject line. I am struggling with the following query and I am not even sure whether what I want to achieve is possible at all: The problem in real-world terms: The DB stores TRANSAKTIONS - which are either sales or refunds: each TRANSAKTION has n ITEMS related to it, which contain their RETAIL_PRICE and DISCOUNT. At the end of day, a total is run up, which should show the sum of refunds, sales and discounts. Tables: TRANSAKTION ----------- KIND ('R' or 'S' for refund or sale) TRANSAKTION_PK PAYMENT_METHOD (cheque, cash, CC) ITEM ---- TRANSAKTION_FK ITEM_PK RETAIL_PRICE DISCOUNT Desired result set: PAYMENT_METHOD | category | SUBTOTAL ------------------------------------ Cash | sales | 103,55 Cash | discounts| -0,53 Cash | refunds | -20,99 CC | sales | 203,55 CC | discounts| -5,53 CC | refunds | -25,99 where sales amount is the sum of RETAIL_PRICE discount amount is the sum of DISCOUNT refunds is the sum of (RETAIL_PRICE-DISCOUNT) I've had a stab at it but my sales amount is short of the RETAIL_PRICEs of all discounted ITEMs: select PAYMENT_METHOD, case when KIND='R' then 'R' when KIND='S' and DISCOUNT is not null then 'D' when KIND='S' and DISCOUNT is null then 'S' end as CATEGORY, sum(case when KIND=2 then -(RETAIL_PRICE-coalesce(DISCOUNT,0)) when KIND=1 and DISCOUNT is not null then -DISCOUNT when KIND=1 and DISCOUNT is null then RETAIL_PRICE end) as SUBTOTAL, from ITEM inner join TRANSAKTION on TRANSAKTION_PK =TRANSAKTION_FK where ... group by PAYMENT_METHOD,CATEGORY order by PAYMENT_METHOD,CATEGORY -- Regards/Gruß, Tarlika Elisabeth Schmitz