【发布时间】:2016-11-14 00:00:24
【问题描述】:
select D.[Date], E.emp_name, E.emp_jde, count(C.[agent_no]) calls, count(S.[EMPJDENUM]) sales
from
(select cast([start_date] as date) dte, [agent_no]
from call_table
where [skill_name] like '%5700 sales l%'
and [Agent_Time] != '0'
) C
full outer join
(select [AC#DTE_dt], [EMPJDENUM]
from sales_table
where [ICGCD2] in ('LAWN', 'HORT')
and [CHANNEL]= 'INQ'
and [ITMQTY]>3
) S on c.dte=s.[AC#DTE_dt]
right join
(select [Date]
from Date_table
) D on c.dte=d.[Date] or s.[AC#DTE_dt]=d.[Date]
right join
(select [emp_name], [emp_jde], [agent_no]
from Employee_table
) E on C.[agent_no]=E.agent_no and S.[EMPJDENUM]=E.emp_jde
group by D.[Date], E.emp_name, E.emp_jde
日期表 -
注意:并非所有日期都有电话和销售。
附加表 -
需要完成的事情——
1) 通过加入 call 表(在 agent_no 上)和 sales(在 JDE 上)表来加入和聚合员工的 call 和 sales
2) 由于并非所有日期都包括电话和销售 - 使用日期维度表来确保所有日期都被表示
想要的结果应该是这样的 -
我写的查询执行了 - 花了很长时间我最终取消了查询。
任何帮助将不胜感激。
【问题讨论】:
-
每个表中大约有多少行?
-
每年调用表大约有 800k 数据,sales 表大约有 400k 数据。
-
能否提供估计的执行计划请brentozar.com/pastetheplan
标签: sql-server join subquery aggregate