同一列中的Postgres值分配

时间:2015-10-19 15:59:18

标签: postgresql

我正在运行工资单报告,但无法查询并返回我需要的结果。在报告中,有两列,表格如下:

Date   | Costs
2014   | 100000
2015   | 150000

但是,诀窍在于,有两种员工的成本会添加到“成本”列中。一种员工是正常的"他们的员工人数遵循" E00 ......"喜欢" E001,E002,E003等

第二种是经理。他们的员工数量类似于" E99 ...",所以" E990,E991等。

为了查询和返回“费用”列的数据,我目前收回了查询的100%员工费用。但是,我想查询并仅返回50%的E00类型员工以添加到“成本”列中,并且仅返回12.5%的E99类型员工以添加到“成本”列中。

通过这种方式,“成本”列将由添加50%的E00员工成本和增加12.5%的E99员工成本组成。

以下是我的查询,但我无法让它返回任何内容:

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");

1 个答案:

答案 0 :(得分:1)

为什么不这样呢?

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;