我正在运行工资单报表,但查询和返回所需结果时遇到问题。在报表中,有两列,表如下所示:
Date | Costs
2014 | 100000
2015 | 150000
然而,诀窍在于,有两种员工的成本被添加到成本栏中。一种员工是“正常”的,他们的员工号遵循“E00…”模式,如“E001、E002、E003等”。
第二类是经理。他们的员工号是“E99…”,所以是“E990,E991等”。
为了查询和返回Costs列的数据,我现在可以为我的查询返回100%的员工成本。但是,我想查询并且只返回50%的E00类型的雇员用于添加到成本列,并且只返回12.5%的E99类型的雇员用于添加到成本列。
这样,成本栏将由E00员工成本的50%和E99员工成本的12.5%组成。
以下是我的查询,但我无法让它返回任何内容:
select
to_char("Date", 'YYYY') as "Date",
case
when "Type" in ('E001', 'E002', 'E003', 'E004') then 0.5 * sum ("Amount") filter (where "Type" in ('E001', 'E002', 'E003', 'E004'))
when "Type" like 'E9%' then 0.125 * sum("Amount") filter (where "Type" like 'E9%')
end as "Costs"
from "Transactions"
group by 1
order by min("Date");
最佳答案
为什么不这样呢?
SELECT mydate,
SUM(mysum)
FROM (
(SELECT to_char("Date", 'YYYY') AS mydate,
0.5 * SUM("Amount") AS mysum
FROM "Transactions"
WHERE TYPE LIKE 'E00%'
GROUP BY 1)
UNION ALL
(SELECT to_char("Date", 'YYYY') AS mydate,
0.125 * SUM("Amount")
FROM "Transactions"
WHERE TYPE LIKE 'E9%'
GROUP BY 1)) s1
GROUP BY mydate
ORDER BY mydate;
关于postgresql - 同一列中的Postgres值分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33218942/