按CTE /设置运算符排序

时间:2018-11-29 09:14:31

标签: sql sql-server

因此,我的业务需求是每天吸引前30名客户(有关系),并且每个客户每周只能被选择一次。因此,我进行了三个查询,每个查询都相同(很好),但是我在理论上有疑问。

1)第一种方法是,当我有两个CTE并在表达式中从这两个组中选择TOP 30时,这是设置操作(EXCEPT)的一部分,但是-据我所知-我们不应该使用order在设定操作中,对吗?顺便说一句,顺便说一句

WITH A  
AS (select dbo.customerentities.customer_id, first_name, last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions 
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 2) and convert(date, 
getdate() - 1) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null), 

B  
AS (select dbo.customerentities.customer_id, first_name, last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 1) and convert(date, 
getdate()) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null)

select *from B where customer_id in
(select top 30 with ties customer_id from B order by sale desc
except
select top 30 with ties customer_id from A order by sale desc)
order by sale desc

2)第二个几乎相同,但是我决定选择CTE级别的前30名,但是再一次-CTE是关系,对吗?所以我们不应该在关系中使用order by吗?否则,它会起作用。

WITH A  
AS (select top 30 with ties dbo.customerentities.customer_id, first_name, 
last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions 
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 2) and convert(date, 
getdate() - 1) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null
order by sale desc), 

B  
AS (select top 30 with ties dbo.customerentities.customer_id, first_name, 
last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 1) and convert(date, 
getdate()) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null
order by sale desc)

select *from B where customer_id in
(select customer_id from B 
except
select customer_id from A )
order by sale desc

3)第三种方法是最差的一种,但我对此毫无疑问。

select A.customer_id, A.first_name, A.last_name, A.tel, A.sum_sale
from (select top 30 with ties dbo.customerentities.customer_id, first_name, 
last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 1) and convert(date, 
getdate()) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null
order by sale desc) as A
left join (select top 30 with ties dbo.customerentities.customer_id, 
first_name, 
last_name, tel, 
sale.sum_sale 
from dbo.CustomerSubscriptions ---check for client's subscriptions 
left join dbo.CustomerEntities on dbo.CustomerSubscriptions.customer_id = 
dbo.CustomerEntities.customer_id 
left join (select customer_id, sum (sale) as sum_sale, from dbo.OrderTable 
where sale_date between CONVERT(DATE, getdate() - 2) and convert(date, 
getdate() - 1) group by customer_id) as sale on CustomerEntities.customer_id 
= sale.customer_id
where not sale.sum_sale is null
order by sale desc) as B on A.customer_id = B.customer_id
where B.customer_id is null --- to exclude customers which already received 
--- bonus day earlier
order by sale desc

所以我的问题是-我应该使用第三种方法,还是可以通过CTE /设置操作轻松地使用顶级订单?

0 个答案:

没有答案