[英]Using ROW_NUMBER () to Compare 1st ARRAY to 2nd, 3rd, 4th, etc
[英]SQL Server Challenging questions - look for 1st buyer, 2nd/3rd buyers, 4th or more buyers
祝大家节日快乐
我被分配编写一个SQL查询,以显示在特定数据范围和位置中购买特定产品的购买者数量。
我需要找到以下内容:
我能够使用以下查询首次获得买家; 但找不到第二/第三买家,第四或更多买家。 有人可以帮忙。
DECLARE @from_dt datetime
DECLARE @Day_End_dt datetime
SET @from_dt = '10/01/2010'
SET @Day_End_dt = '12/29/2010'
select count(B.buyer_id) as California_1st_time_buyer
from
--LIST OF ALL BUYERS AND FIRST PURCHASED DATE THRU END OF DATE RANGE (10/1/98 - 12/28/10)
(select distinct buyer_id, min(purchased_date_time) as First_timer
from product_details
where purchased_date <@Day_End_dt
group by buyer_id
)A,
--LIST OF CALIFORNIA BUYERS AND FIRST PURCHASED DATE IN DATE RANGE (12/1/10 - 12/28/10)
(select distinct buyer_id, min(purchased_date_time) as First_timer_ca_in_date_range
from product_details
where purchased_date >=@from_d
and purchased_date <@Day_End_dt
and location = 'CA'
group by buyer_id
)B
where A.buyer_id = B.buyer_id
and A.First_timer = B.First_timer_ca_in_date_range
Input sample:
purchased_date_time purchased_date buyer_id
12/7/2010 12:30:00 PM 12/7/2010 5627242
--------------------------------------------------------------------
12/9/2010 4:30:00 PM 12/9/2010 9231374
12/19/2010 11:30:00 AM 12/19/2010 9231374
--------------------------------------------------------------------
12/9/2010 12:10:00 AM 12/9/2010 8061088
12/15/2010 5:00:00 PM 12/15/2010 8061088
12/21/2010 6:00:00 PM 12/21/2010 8061088
--------------------------------------------------------------------
12/1/2010 12:30:00 PM 12/1/2010 2288101
12/12/2010 6:30:00 PM 12/12/2010 2288101
12/27/2010 7:30:00 PM 12/27/2010 2288101
12/28/2010 6:30:00 PM 12/28/2010 2288101
--------------------------------------------------------------------
12/9/2010 10:45:00 AM 12/9/2010 2510454
12/16/2010 9:45:00 PM 12/16/2010 2510454
12/19/2010 4:19:00 AM 12/19/2010 2510454
12/22/2010 7:05:00 AM 12/22/2010 2510454
12/29/2010 2:30:00 AM 12/29/2010 2510454
--------------------------------------------------------------------
Output sample:
1st buyers =5 --count buyer_id who purchased the product first time ever in date range (12/1/10 - 12/28/10)
2nd/3rd buyers =7 --count buyer_id who purchased the product 2nd/3rd time in date range (12/1/10 - 12/28/10)
4th or more buyers =3 --count buyer_id who purchased the product 4th time or more in date range (12/1/10 - 12/28/10)
请注意,这些买家仅来自上述日期范围。 假设如果某位买家在2010年12月1日之前进行了三次购买,然后又在2010年1月1日再次购买,那么他将被视为第四定时器购买者。
我不完全了解您想做什么。 如果您可以使用表结构,几个示例行和预期的输出来更新问题,它将有所帮助。
无论如何,这是一个尝试:)
以下查询应为您提供进行了四次或更少购买的购买者列表。 还不清楚您是否只想计算购买次数,或者是否也需要实际购买日期,所以我也添加了日期(对于前4次购买)。 如果买方仅进行了1次购买,则该行将在购买2/3/4列中显示NULL。
如果要包括日期范围或按位置过滤,则这些条件将放在内部选择中。
select buyer_id
,count(*) as purchases
,max(case when purchase_no = 1 then purchased_date end) as purchase_1
,max(case when purchase_no = 2 then purchased_date end) as purchase_2
,max(case when purchase_no = 3 then purchased_date end) as purchase_3
,max(case when purchase_no = 4 then purchased_date end) as purchase_4
from (select buyer_id
,purchased_date
,row_number() over(partition by buyer_id
order by purchased_date) as purchase_no
from product_details
)
where purchase_no <= 4
group by buyer_id;
让我知道它是否有效。
此查询将提供日期范围内的Buyer_id和购买次数:
DECLARE @from_dt datetime
DECLARE @Day_End_dt datetime
SET @from_dt = '10/01/2010'
SET @Day_End_dt = '12/29/2010'
SELECT [buyer_id],COUNT([buyer_id]) AS purchases_in_period
FROM [product_details]
WHERE purchased_date_time between @from_dt and @Day_end_dt
-- OPTIONAL LOCATION FILTER
-- AND [location] = 'CA'
GROUP BY [buyer_id]
GO
如果你使用这个作为一个嵌套查询可以算buyer_id
与给定的数值purchases_in_period
。
我通常将其作为单独查询的组合来处理。
select buyer from T, 1 as FirstTimeBuyer, 0 as SecondTimeBuyer, 0 as thirdtimebuyer
where date is in the specified range
group by buyer having count(t.id) = 1
UNION
select buyer from T, 0 as FirstTimeBuyer, 1 as SecondTimeBuyer, 0 as thirdtimebuyer
where date is in the specified range
group by buyer having count(t.id) = 2
UNION
select buyer from T, 0 as FirstTimeBuyer, 0 as SecondTimeBuyer, 1 as thirdtimebuyer
where date is in the specified range
group by buyer having count(t.id) = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.