SQL SUM,GROUP BY问题

时间:2018-03-17 19:43:10

标签: sql group-by sum

为什么这对我不起作用?我试图选择每个结算单位(OJ)中超过2名员工执行的工作。

select W.NAME as NAME,
    W.OJ as OJ,
    W.JOB as JOB,
    W.MONTHLY_PAY as MONTHLY_PAY ,
    SUM(W.OJ) AS "SUM"
 from WORKER W
 WHERE W.OJ > 2
 GROUP BY W.IME, W.POSAO, W.MJESECNA_PLACA

4 个答案:

答案 0 :(得分:0)

这就是你需要的:

select W.NAME as NAME,
    W.OJ as OJ,
    W.JOB as JOB,
    W.MONTHLY_PAY as MONTHLY_PAY ,
    SUM(W.OJ) AS "SUM"
 from WORKER W
having count( W.OJ)  > 2
 GROUP By NAME,
    W.OJ  ,
    W.JOB  ,
    W.MONTHLY_PAY  

答案 1 :(得分:0)

我想你想要:

select W.OJ, count(*) as num_workers
from WORKER W
group by W.O
having count(*) > 2;

答案 2 :(得分:0)

SELECT job
FROM
(
SELECT 
    w.job, w.oj, count(*) workerCount
FROM worker w
GROUP BY w.job, w.oj) t
GROUP BY job
HAVING min(workerCount) > 2

答案 3 :(得分:0)

SELECT子句中的所有字段(聚合字段除外)都应存在于GROUP BY子句中。

尝试以下:

select W.IME, W.POSAO, W.MJESECNA_PLACA
    SUM(W.OJ) AS "SUM"
 from WORKER W
 WHERE W.OJ > 2
 GROUP BY W.IME, W.POSAO, W.MJESECNA_PLACA