我有一个关于clausule SUM
的查询。我需要根据列的总和。
这可能吗?
有没有人让我看一个例子?
我的代码如下:
SELECT
PL_MAT AS 'Código',
RA_NOME AS 'Nome',
RJ_DESC AS 'Cargo',
SUM(PL_HORAS) AS 'Atrasos'
--SUM(PL_HORAS) AS 'Hora Extra' -- I need SUM this
FROM SPL020
INNER JOIN SP9020 ON PL_PD = P9_CODIGO AND SP9020.D_E_L_E_T_ <> '*' AND SPL020.D_E_L_E_T_ <> '*'
INNER JOIN SRA020 ON RA_MAT = PL_MAT AND SRA020.D_E_L_E_T_ <> '*' AND RA_FILIAL = PL_FILIAL
INNER JOIN SRJ020 ON RJ_FUNCAO = RA_CODFUNC
WHERE PL_DATA BETWEEN '20120616' AND '20120715'
AND P9_CODIGO IN ('011','421','013','409')
--AND P9_CODIGO IN ('408','007','420','009','015','016','018') -- I need sum this
AND RA_CODFUNC IN ('0408','1204','1233')--AUX COLETA C,MOTORISTA D,MOTORISTA CARRETEIRO
AND RA_SITFOLH NOT IN ('D','A')
AND PL_FILIAL = '05'
GROUP BY PL_MAT,RA_NOME,RA_ADMISSA,RA_CODFUNC,RJ_DESC,P9_DESC, PL_HORAS, P9_CODIGO
ORDER BY RA_NOME
答案 0 :(得分:3)
这样做:
sum(case when p9_codigo in ('408','007','420','009','015','016','018') then pl_horas else 0 end) as 'Hora Extra'
如果你把AND P9_CODIGO IN('011','421','013','409')放在where子句中,那么上面的那些将不会被过滤。
所以你需要以不同的方式删除它们。
我想你需要它:
sum(case when p9_codigo in ('011','421','013','409') then PL_HORAS else 0 end) as 'Atrasos',
sum(case when p9_codigo in ('408','007','420','009','015','016','018') then pl_horas else 0 end) as 'Hora Extra'
并从where子句中删除过滤器。
答案 1 :(得分:0)
知道了! 谢谢大家
SELECT
DISTINCT PL_MAT AS 'Código',
RA_NOME AS 'Nome',
RJ_DESC AS 'Cargo',
SUM(CASE WHEN P9_CODIGO IN ('082','080','030','409') THEN PL_HORAS ELSE 0 END) AS 'Atrasos',
SUM(CASE WHEN P9_CODIGO IN ('408','007','420','009','015','016','018') THEN PL_HORAS ELSE 0 END) AS 'Hora extra'
--SUM(PL_HORAS) AS 'Hora Extra' -- I need SUM this
FROM SPL020
INNER JOIN SP9020 ON PL_PD = P9_CODIGO AND SP9020.D_E_L_E_T_ <> '*' AND SPL020.D_E_L_E_T_ <> '*'
INNER JOIN SRA020 ON RA_MAT = PL_MAT AND SRA020.D_E_L_E_T_ <> '*' AND RA_FILIAL = PL_FILIAL
INNER JOIN SRJ020 ON RJ_FUNCAO = RA_CODFUNC
WHERE PL_DATA BETWEEN '20120616' AND '20120715'
--AND P9_CODIGO IN ('011','421','013','409')
--AND P9_CODIGO IN ('408','007','420','009','015','016','018') -- I need sum this
AND RA_CODFUNC IN ('0408','1204','1233')--AUX COLETA C,MOTORISTA D,MOTORISTA CARRETEIRO
AND RA_SITFOLH NOT IN ('D','A')
AND PL_FILIAL = '05'
AND RA_MAT = '000007'
GROUP BY PL_MAT, RA_NOME, RJ_DESC
答案 2 :(得分:-1)
podéspromparesto:
选择nome,cargo,sum(Hora_Extra)作为Hora_Extra 从( 选择 PL_MAT AS'Codigo', RA_NOME AS'Nome', RJ_DESC AS'Cargo', SUM(PL_HORAS)AS'Atrasos',
P9_CODIGO为'Codigo'
FROM SPL020
INNER JOIN SP9020 ON PL_PD = P9_CODIGO
AND SP9020.D_E_L_E_T_&lt;&gt; '*'
AND SPL020.D_E_L_E_T_&lt;&gt; '*'
INNER JOIN SRA020 ON RA_MAT = PL_MAT AND SRA020.D_E_L_E_T_&lt;&gt; '*'
AND RA_FILIAL = PL_FILIAL
INNER JOIN SRJ020 ON RJ_FUNCAO = RA_CODFUNC
在'20120616'和'20120715'之间的PL_DATA
AND P9_CODIGO IN('011','421','013','409')
AND RA_CODFUNC IN('0408','1204','1233')
AND RA_SITFOLH NOT IN('D','A')
AND PL_FILIAL ='05'
GROUP BY PL_MAT,RA_NOME,RA_ADMISSA,RA_CODFUNC,RJ_DESC,P9_DESC,PL_HORAS,P9_CODIGO
)作为kk
Codigo in('408','007','420','009','015','016','018') 由nome命令
Unos提示: 没有使用tildes(C“ó”digo) 没有使用espacios en los campos(Horas Extras)
Saludos