1

我对 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
4

3 回答 3

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 子句中删除过滤器。

于 2013-03-14T14:52:25.520 回答
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
于 2013-03-14T15:47:10.573 回答
-1

podés probar esto:

选择名称,货物,总和(Hora_Extra)作为 Hora_Extra(选择 PL_MAT AS 'Codigo',RA_NOME 作为'Nome',RJ_DESC 作为'货物',SUM(PL_HORAS)作为'Atrasos',

P9_CODIGO 为 'Codigo'

从 SPL020

内部连接 ​​SP9020 ON PL_PD = P9_CODIGO

和 SP9020.D_E_L_E_T_ <> '*'

AND SPL020.D_E_L_E_T_ <> '*'

在 RA_MAT = PL_MAT 和 SRA020.D_E_L_E_T_ <> '*' 上内部连接 ​​SRA020

和 RA_FILIAL = PL_FILIAL

内连接 SRJ020 ON RJ_FUNCAO = RA_CODFUNC

WHERE PL_DATA BETWEEN '20120616' 和 '20120715'

AND P9_CODIGO IN ('011','421','013','409')

AND RA_CODFUNC IN ('0408','120​​4','1233')

AND RA_SITFOLH 不在 ('D','A')

AND PL_FILIAL = '05'

按 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') 按名称排序

Unos 提示:不使用波浪号 (C"ó"digo) 不使用 espacios en los campos (Horas Extras)

萨卢多斯

于 2013-03-14T15:03:28.210 回答