查询循环数据并为每个唯一值提供 Table
Query to Loop Through Data and Provide Table for Each Unique Value
我正在尝试编写一个查询来循环遍历来自每种类型的特定 table 和 return 结果的数据。我已经弄清楚了一些逻辑我想无论如何我只知道它不会工作因为我遗漏了一些东西。
我正在使用的数据结构如下:
Customer ID Delivery Type
143 Delayed
123 Delivered
14 In Process
123 In Route
432 Delivered
456 Delayed
76 In Route
34 In Route
546 Delayed
324 Delivered
235 In Process
678 Delayed
234 In Route
123 In Route
321 Delayed
987 In Process
546 In Process
324 Delayed
235 In Route
678 In Process
234 Delayed
123 In Route
321 Delivered
987 In Process
我试图得到这样的结果:
Delivery Type Customer ID Count
Delayed 143 1
Delayed 234 1
Delayed 321 1
Delayed 324 1
Delayed 456 1
Delayed 546 1
Delayed 678 1
Delivery Type Customer ID Count
Delivered 123 1
Delivered 321 1
Delivered 324 1
Delivered 432 1
Delivery Type Customer ID Count
In Process 14 1
In Process 235 1
In Process 546 1
In Process 678 1
In Process 987 2
Delivery Type Customer ID Count
In Route 34 1
In Route 76 1
In Route 123 3
In Route 234 1
In Route 235 1
我从以下我知道行不通的逻辑开始:
DECLARE @i int
DECLARE @DeliveryCount int
DECLARE @Type int
@type = Select distinct DeliveryType from Deliveries
@DeliveryCount = select count(*) as 'Count' from @type
SET @i = 0
WHILE @DeliveryCount > @i
BEGIN
@DeliveryType = Select row @i from @type
@Top10 = SELECT TOP 10 count(*) as 'Count', CustomerID, DeliveryType FROM Deliveries
Group by CustomerID, DeliveryType
Where DeliveryType like @DeliveryType
Print @Top10
Set @i = @i + 1
END
你似乎想要 group by
和 order by
:
select delivery_type, customer_id, count(*)
from deliveries
group by delivery_type, customer_id
order by delivery_type, customer_id;
我不知道您为什么要尝试循环执行此操作。您应该只在 SQL.
中使用基于集合的逻辑
编辑:
根据您的评论,使用 window 函数:
select d.*
from (select delivery_type, customer_id,
count(*) as cnt,
row_number() over (partition by delivery_type order by count(*) desc) as seqnum
from deliveries
group by delivery_type, customer_id
) d
where seqnum <= 10
order by delivery_type, customer_id;
您可以使用聚合和 window 函数按交付类型获取前 10 位客户:
select *
from (
select
DeliveryType,
CustomerID,
count(*),
rank() over(partition by DeliveryType order by count(*) desc) rn
from Deliveries
group by DeliveryType, CustomerID
) t
where rn <= 10
order by DeliveryType, CustomerID, rn;
或者,您也可以使用 top 1 with ties
- 这消除了对子查询的需要,但不允许您对结果进行排序:
select top (1) with ties
DeliveryType,
CustomerID,
count(*)
from Deliveries
group by DeliveryType, CustomerID
order by (rank() over(partition by DeliveryType order by count(*) desc) - 1) / 10
我正在尝试编写一个查询来循环遍历来自每种类型的特定 table 和 return 结果的数据。我已经弄清楚了一些逻辑我想无论如何我只知道它不会工作因为我遗漏了一些东西。
我正在使用的数据结构如下:
Customer ID Delivery Type
143 Delayed
123 Delivered
14 In Process
123 In Route
432 Delivered
456 Delayed
76 In Route
34 In Route
546 Delayed
324 Delivered
235 In Process
678 Delayed
234 In Route
123 In Route
321 Delayed
987 In Process
546 In Process
324 Delayed
235 In Route
678 In Process
234 Delayed
123 In Route
321 Delivered
987 In Process
我试图得到这样的结果:
Delivery Type Customer ID Count
Delayed 143 1
Delayed 234 1
Delayed 321 1
Delayed 324 1
Delayed 456 1
Delayed 546 1
Delayed 678 1
Delivery Type Customer ID Count
Delivered 123 1
Delivered 321 1
Delivered 324 1
Delivered 432 1
Delivery Type Customer ID Count
In Process 14 1
In Process 235 1
In Process 546 1
In Process 678 1
In Process 987 2
Delivery Type Customer ID Count
In Route 34 1
In Route 76 1
In Route 123 3
In Route 234 1
In Route 235 1
我从以下我知道行不通的逻辑开始:
DECLARE @i int
DECLARE @DeliveryCount int
DECLARE @Type int
@type = Select distinct DeliveryType from Deliveries
@DeliveryCount = select count(*) as 'Count' from @type
SET @i = 0
WHILE @DeliveryCount > @i
BEGIN
@DeliveryType = Select row @i from @type
@Top10 = SELECT TOP 10 count(*) as 'Count', CustomerID, DeliveryType FROM Deliveries
Group by CustomerID, DeliveryType
Where DeliveryType like @DeliveryType
Print @Top10
Set @i = @i + 1
END
你似乎想要 group by
和 order by
:
select delivery_type, customer_id, count(*)
from deliveries
group by delivery_type, customer_id
order by delivery_type, customer_id;
我不知道您为什么要尝试循环执行此操作。您应该只在 SQL.
中使用基于集合的逻辑编辑:
根据您的评论,使用 window 函数:
select d.*
from (select delivery_type, customer_id,
count(*) as cnt,
row_number() over (partition by delivery_type order by count(*) desc) as seqnum
from deliveries
group by delivery_type, customer_id
) d
where seqnum <= 10
order by delivery_type, customer_id;
您可以使用聚合和 window 函数按交付类型获取前 10 位客户:
select *
from (
select
DeliveryType,
CustomerID,
count(*),
rank() over(partition by DeliveryType order by count(*) desc) rn
from Deliveries
group by DeliveryType, CustomerID
) t
where rn <= 10
order by DeliveryType, CustomerID, rn;
或者,您也可以使用 top 1 with ties
- 这消除了对子查询的需要,但不允许您对结果进行排序:
select top (1) with ties
DeliveryType,
CustomerID,
count(*)
from Deliveries
group by DeliveryType, CustomerID
order by (rank() over(partition by DeliveryType order by count(*) desc) - 1) / 10