SQL Query生成输出表

时间:2017-07-31 09:23:56

标签: mysql sql reporting-services plsql dynamic-list

我有两个表,一个是员工注册,其中提供了员工的数据(姓名,ID,电话,州等),另一个是员工或前景表收集的数据(每当他去收集数据时)在字段中包含他访问的个人的信息)。

现在,任务是输出表格 - Desired Output Table

这是所需的表格,我想从我的查询中获取。

CG-DL-GJ等是不同的状态。

现在,我可以通过映射两个表来获取整体数据,并通过在两个表中使用员工ID来获取潜在客户表的总和。

我使用以下Query来获取相同的内容,

select
    sum(case when r.state = 'CG' then 1 else 0 end) as "CG",
    sum(case when r.state = 'DL' then 1 else 0 end) as "DL",
    sum(case when r.state = 'GJ' then 1 else 0 end) as "GJ",
    sum(case when r.state = 'MH' then 1 else 0 end) as "MH",
    sum(case when r.state = 'MP' then 1 else 0 end) as "MP",
    sum(case when r.state = 'PJB' then 1 else 0 end) as "PJB",
    sum(case when r.state = 'RAJ' then 1 else 0 end) as "RAJ",
    sum(case when r.state = 'HR' then 1 else 0 end) as "HR",
    sum(case when r.state in ('RAJ', 'PJB', 'MP' , 'MH', 'GJ', 'DL', 'CG', 'HR') then 1 else 0 end) as "Grand Total"
    from
    _au_all_prospects p,
    _au_all_registrations r
    where p.employee_id = r.employee_id

我收到了这个输出:

CG  DL  GJ  MH  MP  PJB RAJ HR  Grand Total
0   0   0   1   0   0   0   24  25

但不知道,如何进行聚合,例如输出表中定义的类别,如0-30%,30-50%,50-100%,> 100%等收集的数据的百分比等。对于每个员工,他需要收集的门槛数据点是每天10点。所以,如果他每周收集60分,那么他将获得100分。

我需要建立一个类似的表而不是一个确切的表,我需要建议,上面是我的查询,我需要更改,提前谢谢。

0 个答案:

没有答案