计算 Pivot 中的总日差 SQL
Count total day difference in a Pivot SQL
我在两个 table 中有数据,日期分别为 PAL 日期和 HUB 日期。
我希望在具有 6 列的 table 中显示天数差异:
Client
1day
2days
3days
4days
5days
客户端将显示分组的客户端,1天将显示1的datediff的数量。
2 Days 将显示 2,然后是 3,然后是 4,然后是 5 的 datediff 的计数。
PAL Table:
Consignment_Number
eventdatetime
T00A1A0001000001
2021-09-27 12:17:00.000
T00A1A0001000002
2021-09-27 12:27:00.000
T00A1A0001000003
2021-09-28 13:37:00.000
T00A1A0001000004
2021-09-28 13:47:00.000
T00A1A0001000005
2021-09-29 13:57:00.000
T00A1A0001000006
2021-09-39 14:07:00.000
T00A1A0001000007
2021-09-30 14:17:00.000
T00A1A0001000008
2021-09-30 14:27:00.000
T00A1A0001000009
2021-09-30 14:37:00.000
集线器Table:
Consignment_Number
eventdatetime
T00A1A0001000001
2021-09-30 18:39:00.000
T00A1A0001000002
2021-10-01 18:40:00.000
T00A1A0001000003
2021-10-01 18:46:00.000
T00A1A0001000004
2021-10-02 18:46:00.000
T00A1A0001000005
2021-10-02 18:46:00.000
T00A1A0001000006
2021-10-03 18:46:00.000
T00A1A0001000007
2021-10-03 18:46:00.000
T00A1A0001000008
2021-10-04 18:46:00.000
T00A1A0001000009
2021-10-05 18:46:00.000
最终它看起来像这样:
Clients
1day
2days
3days
4days
5days
Client1
5
2
3
1
0
Client2
2
2
2
1
0
Client3
4
2
5
1
0
select
client
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 1 then 1 else 0 end) 1day
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 2 then 1 else 0 end) 2days
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 3 then 1 else 0 end) 3days
, ...
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 1 then 1 else 0 end) * 100.0 / count(*) 1dayperc
, ...
from PAL p
join HUB h
on h.Consignment_Number = p.Consignment_Number
group by client
我在两个 table 中有数据,日期分别为 PAL 日期和 HUB 日期。 我希望在具有 6 列的 table 中显示天数差异:
Client | 1day | 2days | 3days | 4days | 5days |
---|
客户端将显示分组的客户端,1天将显示1的datediff的数量。 2 Days 将显示 2,然后是 3,然后是 4,然后是 5 的 datediff 的计数。
PAL Table:
Consignment_Number | eventdatetime |
---|---|
T00A1A0001000001 | 2021-09-27 12:17:00.000 |
T00A1A0001000002 | 2021-09-27 12:27:00.000 |
T00A1A0001000003 | 2021-09-28 13:37:00.000 |
T00A1A0001000004 | 2021-09-28 13:47:00.000 |
T00A1A0001000005 | 2021-09-29 13:57:00.000 |
T00A1A0001000006 | 2021-09-39 14:07:00.000 |
T00A1A0001000007 | 2021-09-30 14:17:00.000 |
T00A1A0001000008 | 2021-09-30 14:27:00.000 |
T00A1A0001000009 | 2021-09-30 14:37:00.000 |
集线器Table:
Consignment_Number | eventdatetime |
---|---|
T00A1A0001000001 | 2021-09-30 18:39:00.000 |
T00A1A0001000002 | 2021-10-01 18:40:00.000 |
T00A1A0001000003 | 2021-10-01 18:46:00.000 |
T00A1A0001000004 | 2021-10-02 18:46:00.000 |
T00A1A0001000005 | 2021-10-02 18:46:00.000 |
T00A1A0001000006 | 2021-10-03 18:46:00.000 |
T00A1A0001000007 | 2021-10-03 18:46:00.000 |
T00A1A0001000008 | 2021-10-04 18:46:00.000 |
T00A1A0001000009 | 2021-10-05 18:46:00.000 |
最终它看起来像这样:
Clients | 1day | 2days | 3days | 4days | 5days |
---|---|---|---|---|---|
Client1 | 5 | 2 | 3 | 1 | 0 |
Client2 | 2 | 2 | 2 | 1 | 0 |
Client3 | 4 | 2 | 5 | 1 | 0 |
select
client
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 1 then 1 else 0 end) 1day
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 2 then 1 else 0 end) 2days
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 3 then 1 else 0 end) 3days
, ...
, sum(case when abs(datediff(day, Consignment_Number,Consignment_Number)) = 1 then 1 else 0 end) * 100.0 / count(*) 1dayperc
, ...
from PAL p
join HUB h
on h.Consignment_Number = p.Consignment_Number
group by client