我的数据是员工级别,我每天汇总,然后加入一个名单以获取名称和经理名称等。有没有办法然后聚合这些数据(与经理名称)并加入它回到原始数据?
直到这一点正常。我每个日期为每个system_id创建一行。我使用映射表加入系统ID以获取Employee ID。然后我将其加入名册表以获取一些员工信息(包括他们的主管姓名)。
SELECT *
FROM
(SELECT DISTINCT (system_id)
FROM schema.fact_table
WHERE report_date >= '2017-09-01') AS t1
CROSS JOIN
(SELECT report_date :: DATE
FROM generate_series('2017-09-01' :: DATE , '2017-09-10' :: DATE , INTERVAL '1 day') report_date ) AS t2
LEFT JOIN
(SELECT *
FROM schema.dimension_table_1
WHERE system_id IS NOT NULL
AND expiration_date > '2017-09-01') AS t3
ON t1.system_id = t3.system_id
AND t2.report_date >= t3.effective_date
AND t2.report_date <= t3.expiration_date
LEFT JOIN
(SELECT *
FROM schema.dimension_table_2
WHERE expiration_date > '2017-09-01') AS t4
ON t3.employee_id = t4.employee_id
AND report_date >= t4.effective_date
AND report_date <= t4.expiration_date
LEFT JOIN
(SELECT report_date AS report_date,
system_id AS system_id,
sum(stuff_count) AS stuff_count,
FROM schema.fact_table_1
WHERE report_date >= '2017-09-01'
GROUP BY report_date, avaya_id, source_database ) AS t5
ON t2.report_date = t5.report_date
AND t1.system_id = t5.system_id
然后,我想添加一些列,这些列具有员工团队的汇总日常绩效。
LEFT JOIN
(SELECT report_date,
supervisor_employee_id,
sum(stuff_count) AS supervisor_stuff_count,
WHERE report_date = '2017-09-01'
GROUP BY report_date, supervisor_employee_id) AS t7
ON supervisor_employee_id = supervisor_employee_id
AND report_date = report_date
我怎样才能做到这一点?我需要以某种方式为整个上部区域(使用管理员名称的每日聚合员工级别数据)提供别名,然后在日期+ supervisor_id_column上聚合该别名。我无法让它正常工作。
答案 0 :(得分:1)
我需要以某种方式给整个上半部分别名(每天 使用主管名称汇总员工级别数据)
使用 WITH (Common Table Expressions),您可以通过引用给查询的别名来“重复使用”查询,例如
WITH myCTE as (
....
)
select *
from myCTE
可以根据需要在CTE中或之后执行聚合。