如何在SQL中编写计算列

问题描述 投票:0回答:1

这是我的数据库的示例:

我正在尝试编写一个返回以下内容的查询:

worker_id,
avg salary for 2020 (basic_salary+bonus),
basic_salary for 2021,
ever been in level C (yes/no),
average basic_salary \>6000 (yes/no),
how many salaries the employee had until today.

这是我写的脚本:

// select worker_id, AVG(CASE WHEN year(month)=2020
                THEN BASIC_SALARY+bonus
                ELSE NULL END) AS avg_2020,
avg(case when year(month)=2021 
then BASIC_SALARY
else null end) as avg_2021,
case when t1.level='C' then 'YES' 
else 'NO'
end as 'Level C',
CASE WHEN AVG(BASIC_SALARY) >6000 
            THEN 'yes'
            ELSE 'no' 
       END AS 'above_6000',
count(BASIC_SALARY) 'HowManySalaries'
from [workers_table] t1
join data t2
on t1.level=t2.level
group by worker_id,t1.level
order by WORKER_ID
//

和结果:

worker_id   avg_2020    avg_2021    Level C above_6000  HowManySalaries
2011-11-11  NULL    3500    NO  no  4
2011-11-11  NULL    5000    NO  no  2
2011-11-11  NULL    4000    YES yes 3
2011-11-12  8666    4500    NO  yes 4
2011-11-12  9666    NULL    NO  yes 3
2011-11-12  7000    6000    YES no  2
2011-11-12  9000    NULL    NO  no  1
2011-11-13  NULL    10000   NO  yes 3
2011-11-13  9000    4500    NO  no  3
2011-11-13  10000   18000   YES yes 5
2011-11-13  12500   6750    NO  yes 5
2011-11-14  NULL    3600    NO  no  1
2011-11-14  NULL    7200    NO  yes 2
2011-11-14  NULL    4800    YES no  1

也许有更好的方法来编写脚本?我需要为每个worker_id获取1行。

sql aggregate-functions window-functions
1个回答
0
投票

这是我的解决方案。我只是简单地检查了结果/值,所以它可能不是 100% 正确,但看起来是正确的。使用前请仔细检查其正确性。 :)

select 
    worker_id,
    AVG(CASE WHEN year(w.month)=2020 THEN w.BASIC_SALARY+b.bonus ELSE NULL END) AS avg_2020,
    AVG(CASE WHEN year(w.month)=2021 THEN w.BASIC_SALARY ELSE NULL END) AS avg_2021,
    CASE WHEN MAX(CASE WHEN w.level='C' THEN 1 ELSE 0 END)=1 THEN 'YES' ELSE 'NO' END AS 'Level C',
    CASE WHEN AVG(w.BASIC_SALARY) > 6000 THEN 'yes' ELSE 'no' END AS 'above_6000',
    COUNT(w.BASIC_SALARY) 'HowManySalaries'
FROM workers_table w
JOIN data b on b.level=w.level
GROUP BY w.worker_id

在您的查询中,您按工作人员 ID 和奖金级别进行分组。因此,您的行最终会包含这两个值之间的所有可能组合。将其限制为仅worker_id 会导致每个worker 一行。 尽管这会导致您的“C 级”列无法正常工作,因为基本的

CASE WHEN
不会关注为特定worker_id 给出的所有行。使用
CASE WHEN
聚合扩展
MAX
可以解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.