我有一个包含列的表
TicketID - ID of the ticket
AssignedTo - UserID of person to whom ticket is assigned
CreatedTime - Time when Ticket is received
HandleTime - Time when Ticket is picked up for handling
FinishTime - Time when Ticket is finished handling
我需要检索分组到AssignedTo ID的以下数据:
AssignedTo
拣配率在以下范围内(%和计数)
我提出了一个初始查询
SELECT
User,
sum(case when PickupTime <=1 then 1 else 0 end) as range1,
sum(case when PickupTime <=2 then 1 else 0 end) as range2,
...
FROM
(SELECT
((HandleTime - CreatedTime)/60000) as PickupTime,
((FinishTime - CreatedTime)/60000) as CompletedTime,
AssignedTo as User
FROM
TicketTable
)T
GROUP BY
User
在这里,我只能获得皮卡范围计数。我仍然需要皮卡范围百分比以及结算范围计数和百分比。我如何获得它们?
编辑:
让我们考虑一个样本数据集,只考虑两个范围&lt; = 1和&gt; 1,并在此处直接将时间视为分钟,而在原始表中将其存储为时间戳。
TicketID | AssignedTo | CreatedTime | HandleTime | FinishTime
1 001 2 3 3
2 001 4 6 8
3 002 1 2 3
在上表中,用户001被分配了总共2张票,而用户002被分配了总共1张票。 门票的PickupTime和CompletedTime是
TicketID | PickupTime | CompletedTime
1 1 1
2 2 4
3 1 2
因此,对于分配给他的两张门票中的用户-001,他在1分钟范围内选择了1张票,在1分钟范围内选择了1张票。所以1分钟范围内的票数百分比为50%且超过1分钟范围对于他来说是50%。同样适用于CompletedTime,也适用于User-002。 所以我想要的最终结果是。
AssignedTo | Pickup_range1_count | Pickup_range2_count | Pickup_range1_percentage |
001 1 1 0.5
002 1 0 1
Pickup_range2_percentage | Complete_range1_count | Complete_range2_count |
0.5 1 1
0 0 1
Complete_range1_percentage | Complete_range2_percentage
0.5 0.5
0 1
答案 0 :(得分:1)
根据你的例子,你已经差不多了。您所需要的只是个别金额与总金额的比率(或计算完毕的金额)。像
这样的东西SELECT AssignedTo,
sum(1) AllCount,
sum(CASE
WHEN HandleTime - CreatedTime <= 1
THEN 1
ELSE 0
END) Range1PickupCount,
sum(CASE
WHEN HandleTime - CreatedTime > 1
THEN 1
ELSE 0
END) Range2PickupCount,
...
sum(CASE
WHEN HandleTime - CreatedTime <= 1
THEN 1
ELSE 0
END) / sum(1) * 100 Range1PickupPercentage,
sum(CASE
WHEN HandleTime - CreatedTime > 1
THEN 1
ELSE 0
END) / sum(1) * 100 Range2PickupPercentage,
...
FROM Tickets
GROUP BY AssignedTo;
应该是一个有效的演示,你可以继续。
(免责声明:未进行任何测试,因为未提供DDL和DML。)