Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query not returning desired results when calculating monthly sum of salaries paid

Status
Not open for further replies.

egstatus

Programmer
Apr 14, 2005
143
US
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).

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

 
Hi egstatus,
You are doing nothing wrong - you have the core of the sql correct. There is probably a far more elegant way to do this, but you could try something like this (totally untested)

SELECT X.CORR_EMPRESA
, X.NOMBRE_EMPRESA
, MAX(X.ENE) AS ENE
, MAX(X.FEB) AS FEB
, MAX(X.MAR) AS MAR
, MAX(X.ABR) AS ABR
, MAX(X.MAY) AS MAY
, MAX(X.JUN) AS JUN
, MAX(X.JUL) AS JUL
, MAX(X.AGO) AS AGO
, MAX(X.SEP) AS SEP
, MAX(X.OCT) AS OCT
, MAX(X.NOV) AS NOV
, MAX(X.DEC) AS DEC
FROM (SELECT 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 ) X
GROUP BY X.NOMBRE_EMPRESA, X.CORR_EMPRESA, X.MES_PERIODO

 
egstatus, try:

Code:
SELECT 
    A.CORR_EMPRESA, 
    B.NOMBRE_EMPRESA, 
    SUM(CASE WHEN A.MES_PERIODO = '1' THEN A.MONTO_NETO ELSE 0 END) AS ENE,
    SUM(CASE WHEN A.MES_PERIODO = '2' THEN A.MONTO_NETO ELSE 0 END) AS FEB,
    SUM(CASE WHEN A.MES_PERIODO = '3' THEN A.MONTO_NETO ELSE 0 END) AS MAR,
    SUM(CASE WHEN A.MES_PERIODO = '4' THEN A.MONTO_NETO ELSE 0 END) AS ABR,
    SUM(CASE WHEN A.MES_PERIODO = '5' THEN A.MONTO_NETO ELSE 0 END) AS MAY,
    SUM(CASE WHEN A.MES_PERIODO = '6' THEN A.MONTO_NETO ELSE 0 END) AS JUN,
    SUM(CASE WHEN A.MES_PERIODO = '7' THEN A.MONTO_NETO ELSE 0 END) AS JUL,
    SUM(CASE WHEN A.MES_PERIODO = '8' THEN A.MONTO_NETO ELSE 0 END) AS AGO,
    SUM(CASE WHEN A.MES_PERIODO = '9' THEN A.MONTO_NETO ELSE 0 END) AS SEP,
    SUM(CASE WHEN A.MES_PERIODO = '10' THEN A.MONTO_NETO ELSE 0 END) AS OCT,
    SUM(CASE WHEN A.MES_PERIODO = '11' THEN A.MONTO_NETO ELSE 0 END) AS NOV,
    SUM(CASE WHEN A.MES_PERIODO = '12' THEN 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

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
Thank you both notadba and imex, both solutions worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top