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 30BE9D1B269 for ; Tue, 20 Jul 2004 12:27:27 -0300 (ADT) 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 57308-08 for ; Tue, 20 Jul 2004 15:27:18 +0000 (GMT) Received: from misav07.sasknet.sk.ca (misav03.sasknet.sk.ca [142.165.20.164]) by svr1.postgresql.org (Postfix) with ESMTP id 71597D1B1FD for ; Tue, 20 Jul 2004 12:27:15 -0300 (ADT) Received: from thyme.sasktel.net ([142.165.20.198]) by misav03 with InterScan Messaging Security Suite; Tue, 20 Jul 2004 09:27:40 -0600 Received: from borderveygqj37 (hssx-estv-30-169.sasknet.sk.ca [142.165.30.169]) by thyme.sasktel.net (SaskTel eMessaging Service) with ESMTPA id <0I1500JDRPL960@thyme.sasktel.net> for pgsql-sql@postgresql.org; Tue, 20 Jul 2004 09:27:13 -0600 (CST) Date: Tue, 20 Jul 2004 09:27:14 -0600 From: Caleb Simonyi-Gindele Subject: SELECT query/subquery problem To: pgsql-sql@postgresql.org Message-id: <000001c46e6e$08cbd310$c201a8c0@borderveygqj37> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 X-Mailer: Microsoft Outlook, Build 10.0.4510 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Importance: Normal X-Priority: 3 (Normal) X-MSMail-priority: Normal X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200407/129 X-Sequence-Number: 18381 I'm still trying to isolate issues with my SELECT query. I have a table in my veterinary software which stores my sales transactions. The pertinent columns are dat_staff_code (stores the doctor who gets credit for the sale), sys_tran_number (stores a unique transaction #), cli_credit_adj_trans_no (stores the sys_tran_number that a credit is put against). My problem is that while dat_staff_code is populated for sales, it is not populated when a credit against a sale is issued. So I end up with data as follows: |dat_staff |sys_tran |cli_credit |cli_tran| |_code |_number |_adj_tran_no |_amount | |----------|---------|-------------|--------| |mm |91112 | |50.00 | | |95402 |91112 |-50.00 | What I want to end up with is net sales (sales - credits) GROUP BY dat_staff_code. Where I'm stuck is I can't link the credits to a doctor for the life of me. I use a subquery to get amt where cli_credit_adj_tran_no matches sys_tran_number but I can't associate it with the doctor of the original transaction. This is my query: SELECT (SELECT SUM(cli_tran_amount) FROM vetpmardet WHERE cli_credit_adj_trans_no IN (SELECT sys_tran_number from vetpmardet WHERE cli_tran_trans_date BETWEEN '$pro_week_start_date_yyyymmdd' AND '$pro_week_end_date_yyyymmdd')) + SUM(cli_tran_amount) AS amount FROM vetpmardet WHERE cli_tran_trans_date BETWEEN '$pro_week_start_date_yyyymmdd' AND '$pro_week_end_date_yyyymmdd' AND TRIM(dat_staff_code) LIKE 'mm' Should I be switching to a union query or something? TIA, Caleb