SQL-创建具有不同数量的另一列的列

时间:2018-07-08 12:20:52

标签: sql sql-server count subquery distinct

我试图在SQl表中添加3列。

第1列:总计_小时_总体

我希望此列汇总每个sequence_ID的总小时数

第2列:总计代码

我希望此列是“客户”列中不同值的计数。

第3列:客户总数

我希望此列是“客户”列中“ Job_Type”列等于“客户”的不同值的计数

下面是我当前拥有的表的摘录。有超过800个唯一的sequence_ID,每个ID具有多个行。我希望上面计算的列显示每一行的值。

Sequence_ID |Date_European  |Hours  |Month  |Day    |Year   |Day_of_Week    |Client_Number  |Client         |Job_No |Job_Type
1001        |01/09/2017     |7.3    |9      |1      |2017   |Friday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE
1001        |04/09/2017     |7.3    |9      |4      |2017   |Monday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE
1001        |09/08/2017     |2      |8      |9      |2017   |Wednesday      |1              |Admin          |A1     |Non-Billable
1001        |24/08/2017     |1.3    |8      |24     |2017   |Thursday       |2              |Client1        |A2     |Client
1001        |28/08/2017     |2.3    |8      |28     |2017   |Monday         |2              |Client1        |A2     |Client
1001        |16/08/2017     |0.5    |8      |16     |2017   |Wednesday      |3              |Client2        |A3     |Client
1001        |16/08/2017     |1      |8      |16     |2017   |Wednesday      |2              |Client1        |A2     |Client
1001        |18/08/2017     |3      |8      |18     |2017   |Friday         |3              |Client2        |A3     |Client
1001        |22/08/2017     |0.7    |8      |22     |2017   |Tuesday        |4              |Client3        |A4     |Client
1001        |16/08/2017     |7.3    |8      |16     |2017   |Wednesday      |5              |Client4        |A5     |Client
1001        |18/08/2017     |1.3    |8      |18     |2017   |Friday         |5              |Client4        |A5     |Client
1001        |21/08/2017     |1      |8      |21     |2017   |Monday         |5              |Client4        |A5     |Client
1001        |12/09/2017     |0.6    |9      |12     |2017   |Tuesday        |5              |Client4        |A5     |Client
1002        |01/09/2017     |7.3    |9      |1      |2017   |Friday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE
1002        |04/09/2017     |7.3    |9      |4      |2017   |Monday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE
1002        |09/08/2017     |2      |8      |9      |2017   |Wednesday      |1              |Admin          |A1     |Non-Billable
1002        |24/08/2017     |1.3    |8      |24     |2017   |Thursday       |4              |Client3        |A4     |Client
1002        |28/08/2017     |2.3    |8      |28     |2017   |Monday         |5              |Client4        |A5     |Client

下面是我希望桌子的外观。

Sequence_ID |Date_European  |Hours  |Month  |Day    |Year   |Day_of_Week    |Client_Number  |Client         |Job_No |Job_Type       |Total_Hours_Overall    |Total_No_Codes |Total Clients
1001        |01/09/2017     |7.3    |9      |1      |2017   |Friday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE   |35.6                   |6              |4
1001        |04/09/2017     |7.3    |9      |4      |2017   |Monday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE   |35.6                   |6              |4
1001        |09/08/2017     |2      |8      |9      |2017   |Wednesday      |1              |Admin          |A1     |Non-Billable   |35.6                   |6              |4
1001        |24/08/2017     |1.3    |8      |24     |2017   |Thursday       |2              |Client1        |A2     |Client         |35.6                   |6              |4
1001        |28/08/2017     |2.3    |8      |28     |2017   |Monday         |2              |Client1        |A2     |Client         |35.6                   |6              |4
1001        |16/08/2017     |0.5    |8      |16     |2017   |Wednesday      |3              |Client2        |A3     |Client         |35.6                   |6              |4
1001        |16/08/2017     |1      |8      |16     |2017   |Wednesday      |2              |Client1        |A2     |Client         |35.6                   |6              |4
1001        |18/08/2017     |3      |8      |18     |2017   |Friday         |3              |Client2        |A3     |Client         |35.6                   |6              |4
1001        |22/08/2017     |0.7    |8      |22     |2017   |Tuesday        |4              |Client3        |A4     |Client         |35.6                   |6              |4
1001        |16/08/2017     |7.3    |8      |16     |2017   |Wednesday      |5              |Client4        |A5     |Client         |35.6                   |6              |4
1001        |18/08/2017     |1.3    |8      |18     |2017   |Friday         |5              |Client4        |A5     |Client         |35.6                   |6              |4
1001        |21/08/2017     |1      |8      |21     |2017   |Monday         |5              |Client4        |A5     |Client         |35.6                   |6              |4
1001        |12/09/2017     |0.6    |9      |12     |2017   |Tuesday        |5              |Client4        |A5     |Client         |35.6                   |6              |4
1002        |01/09/2017     |7.3    |9      |1      |2017   |Friday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE   |20.2                   |2              |2
1002        |04/09/2017     |7.3    |9      |4      |2017   |Monday         |0              |ANNUAL LEAVE   |0      |ANNUAL LEAVE   |20.2                   |2              |2
1002        |09/08/2017     |2      |8      |9      |2017   |Wednesday      |1              |Admin          |A1     |Non-Billable   |20.2                   |2              |2
1002        |24/08/2017     |1.3    |8      |24     |2017   |Thursday       |4              |Client3        |A4     |Client         |20.2                   |2              |2
1002        |28/08/2017     |2.3    |8      |28     |2017   |Monday         |5              |Client4        |A5     |Client         |20.2                   |2              |2

我已经尝试了(但失败了)很多方法。我认为必须使用子查询,但是我似乎无法正确获取格式。

另一个难以解决的问题是count函数,因为我知道这需要一个group by子句,但现在我想将所有行保留在表中。

我认为我对整个事情都考虑不周,因此任何帮助将不胜感激。 预先感谢

1 个答案:

答案 0 :(得分:1)

您可以使用窗口功能:

select t.*, sum(hours) over (partition by sequence_id) as sum_hours,
       max(codes_seqnum) over (partition by sequence_id) as num_codes,
       max(clients_seqnum) over (partition by sequence_id) as num_clients
from (select t.*,
             dense_rank() over (partition by sequence_id order by client) as codes_seqnum,
             dense_rank() over (partition by sequence_id, job_type order by (case when job_type = 'Client' then client end)) as client_seqnum
      from t
     ) t;

COUNT(DISTINCT)实际上对于SQL Server中的窗口函数有些棘手。上面的方法基本有效,但是如果没有客户端,它将不会返回0。在结果中仅JOIN可能会更好:

select t.*, tt.sum_hours, tt.num_codes, tt.num_clients
from t join
     (select sequence_id, sum(hours) as sum_hours,
             count(distinct client) as num_codes,
             count(distinct case when job_type = 'Client' then client end) as num_clients
      from t
      group by sequence_id
     ) tt
     on tt.sequence_id = t.sequence_id;