How do I make the following query to list the result in one row per company instead of 12(number of months) as I'm getting.
I'm trying to calculate the sum of salaries paid by company by month. I have 4 companies in my table. In my query below I have specified just for company whose ID is 1 to minimize the number of rows returned.
For company one (company name SDI] there were salaries paid for the months of July to November.
my goal is to get each sum in the corresponding month column so I should end up with one row/record per company.
(I hope I'm making myself clear).
These are the rusult of the query as of now:
CORR_EMPRESA NOMBRE_EMPRESA ENE FEB MAR ABR MAY JUN JUL AGO SEP OCT NOV DEC
1 SDI 0 0 0 0 0 0 0 0 0 0 7923.32 0
1 SDI 0 0 0 0 0 0 0 0 0 16002.97 0 0
1 SDI 0 0 0 0 0 0 0 0 15165.09 0 0 0
1 SDI 0 0 0 0 0 0 0 15920.65 0 0 0 0
1 SDI 0 0 0 0 0 0 15809.69 0 0 0 0 0
Can some one please tell me what am I doing wrong?
Thanks
EG
I'm trying to calculate the sum of salaries paid by company by month. I have 4 companies in my table. In my query below I have specified just for company whose ID is 1 to minimize the number of rows returned.
For company one (company name SDI] there were salaries paid for the months of July to November.
my goal is to get each sum in the corresponding month column so I should end up with one row/record per company.
(I hope I'm making myself clear).
SQL:
SELECT DISTINCT A.CORR_EMPRESA, B.NOMBRE_EMPRESA,
CASE WHEN A.MES_PERIODO = '1' THEN SUM(A.MONTO_NETO) ELSE 0 END AS ENE,
CASE WHEN A.MES_PERIODO = '2' THEN SUM(A.MONTO_NETO) ELSE 0 END AS FEB,
CASE WHEN A.MES_PERIODO = '3' THEN SUM(A.MONTO_NETO) ELSE 0 END AS MAR,
CASE WHEN A.MES_PERIODO = '4' THEN SUM(A.MONTO_NETO) ELSE 0 END AS ABR,
CASE WHEN A.MES_PERIODO = '5' THEN SUM(A.MONTO_NETO) ELSE 0 END AS MAY,
CASE WHEN A.MES_PERIODO = '6' THEN SUM(A.MONTO_NETO) ELSE 0 END AS JUN,
CASE WHEN A.MES_PERIODO = '7' THEN SUM(A.MONTO_NETO) ELSE 0 END AS JUL,
CASE WHEN A.MES_PERIODO = '8' THEN SUM(A.MONTO_NETO) ELSE 0 END AS AGO,
CASE WHEN A.MES_PERIODO = '9' THEN SUM(A.MONTO_NETO) ELSE 0 END AS SEP,
CASE WHEN A.MES_PERIODO = '10' THEN SUM(A.MONTO_NETO) ELSE 0 END AS OCT,
CASE WHEN A.MES_PERIODO = '11' THEN SUM(A.MONTO_NETO) ELSE 0 END AS NOV,
CASE WHEN A.MES_PERIODO = '12' THEN SUM(A.MONTO_NETO) ELSE 0 END AS DEC
FROM PLA_DOCUMENTO_DETA AS A
INNER JOIN GEN_EMPRESA AS B ON A.CORR_EMPRESA=B.CORR_EMPRESA
WHERE A.CORR_EMPRESA = 1
GROUP BY B.NOMBRE_EMPRESA, A.CORR_EMPRESA, A.MES_PERIODO
These are the rusult of the query as of now:
CORR_EMPRESA NOMBRE_EMPRESA ENE FEB MAR ABR MAY JUN JUL AGO SEP OCT NOV DEC
1 SDI 0 0 0 0 0 0 0 0 0 0 7923.32 0
1 SDI 0 0 0 0 0 0 0 0 0 16002.97 0 0
1 SDI 0 0 0 0 0 0 0 0 15165.09 0 0 0
1 SDI 0 0 0 0 0 0 0 15920.65 0 0 0 0
1 SDI 0 0 0 0 0 0 15809.69 0 0 0 0 0
Can some one please tell me what am I doing wrong?
Thanks
EG