More than one DISTINCT on SUM
Posted in 2003
I need to be able to use more than one DISTINCT with this SQL query to have
the columns added properly. If I run it just like I have written below,
the column T2.gljrs_amount, T3.gljrs_amount, and the "Total" expression are
all larger numbers than they should be. If I change the SELECT to
SUM (DISTINCT T2.gljrs_amount) then that column is added correctly, but
T3.gljrs_amount and the expression are still incorrect. I need to be able
to use the DISTINCT with each SUM aggregate, except T1.gljrs_amount as
this is already being added correctly. The Informix documentation says
DISTINCT can't be used more than once and I confirmed this because it gives
a syntax error if I do. Is there any way around this or could this
SELECT be written differently to give the proper results? There are
7 account numbers in the BETWEEN for T1.gljrs_account and 2 account
in the BETWEEN for T2.gljrs_account. I'm using SE 7.23.
SELECT T1.gljrs_prft_ctr, SUM (T1.gljrs_amount), SUM (T2.gljrs_amount),
SUM (T3.gljrs_amount), SUM (T1.gljrs_amount + T2.gljrs_amount) Total
FROM gl_jrnl_rs T1, gl_jrnl_rs T2, gl_jrnl_rs T3
WHERE T1.gljrs_shl = T2.gljrs_shl
AND T2.gljrs_shl = T3.gljrs_shl
AND T1.gljrs_date = T2.gljrs_date
AND T2.gljrs_date = T3.gljrs_date
AND T1.gljrs_date BETWEEN MDY(03,01,2003) and MDY(03,31,2003)
AND T1.gljrs_account BETWEEN 5049 and 5058
AND T2.gljrs_account BETWEEN 5000 and 5040
AND T3.gljrs_account = 5600
GROUP BY 1
ORDER BY 1
Here is the table
gl_jrnl_rs
gljrs_account
gljrs_amount
gljrs_prft_ctr
gljrs_date
gljrs_shl