SQL Server挑战性问题-寻找第一位买家、第二位/第三位买家、第四位或更多买家
节日快乐,各位 我被指派编写一个SQL查询,以显示在特定数据范围和位置购买特定产品的买家数量。 我需要找到以下内容: 第一次买主。 第二次/第三次买家。 第四次或更多次买家。 我第一次通过下面的查询找到了买家 ; 但无法找到第二/第三位买家、第四位或更多买家。有人能帮忙吗SQL Server挑战性问题-寻找第一位买家、第二位/第三位买家、第四位或更多买家,sql,Sql,节日快乐,各位 我被指派编写一个SQL查询,以显示在特定数据范围和位置购买特定产品的买家数量。 我需要找到以下内容: 第一次买主。 第二次/第三次买家。 第四次或更多次买家。 我第一次通过下面的查询找到了买家 ; 但无法找到第二/第三位买家、第四位或更多买家。有人能帮忙吗 DECLARE @from_dt datetime DECLARE @Day_End_dt datetime SET @from_dt = '10/01/2010' SET @Day_End_d
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年12月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;
让我知道它是否有效。此查询将提供买方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
如果将其用作嵌套查询,则可以在给定的购买次数下计算买家id值。我通常将其作为单独查询的联合
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
您能提供您正在使用的DBMS以及示例输入/输出以增加清晰度吗?我使用的是MS SQL server 2000/2005/2008。谢谢,这是第一次买房吗?还是该日期范围内的第一次买家?。此外,您说您能够获得第一次买家,但在您的查询中,您正在对买家进行计数,而不是获得该数据范围内的买家、首次买家。抱歉搞混了,我在数买主。感谢我使用MS SQL server 2000/2005/2008。-真正地不,真的吗?嗨,Ronnis,我用输入输出样本编辑了我的问题。我现在正在测试您的查询。非常感谢汉克斯,罗尼斯。行数部分非常有用。@joe,我不明白第二/第三买家如何=7?您的样本数据中有5个不同的买家。1人购买产品2次id 9231374,1人购买产品3次id 8061088。我也不明白第四位或更多买家如何=3?只有2位买家购买了id 2288101和id 2510454的产品4次或4次以上。