简体   繁体   English

SQL Server挑战性问题-寻找第一买家,第二/第三买家,第四或更多买家

[英]SQL Server Challenging questions - look for 1st buyer, 2nd/3rd buyers, 4th or more buyers

Happy Holidays, folks, 祝大家节日快乐

I am assigned to write a SQL query to show the number of buyers who purchase a particular product in a certain data range and in a location. 我被分配编写一个SQL查询,以显示在特定数据范围和位置中购买特定产品的购买者数量。
I need to find the following: 我需要找到以下内容:

  1. 1st time buyer ever. 有史以来第一次买家。
  2. 2nd/3rd time buyers. 第2/3次买家。
  3. 4th or more time buyers. 第4次或更多时间的买家。

I was able to get the first time ever buyers using the queries below ; 我能够使用以下查询首次获得买家; but unable to find the 2nd/3rd buyers, 4th or more buyers. 但找不到第二/第三买家,第四或更多买家。 Could someone please help. 有人可以帮忙。

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)

Please note these buyers are only from the date range mentioned above. 请注意,这些买家仅来自上述日期范围。 Suppose if aa buyer had three purchases prior to 12/1/2010 and he purchased again on 12/1/2010, he would be considered as a 4th timer buyer. 假设如果某位买家在2010年12月1日之前进行了三次购买,然后又在2010年1月1日再次购买,那么他将被视为第四定时器购买者。

I don't entirely understand what you want to do. 我不完全了解您想做什么。 If you could update the question with Table structure, a couple of sample rows and the expected output it would help. 如果您可以使用表结构,几个示例行和预期的输出来更新问题,它将有所帮助。

Anyway, here is an attempt :) 无论如何,这是一个尝试:)

The following query should give you a list of buyers that made four or less purchases. 以下查询应为您提供进行了四次或更少购买的购买者列表。 It isn't clear if you just wanted to count the purchases or if you need the actual purchase dates too, so I added the dates as well (for the first 4 purchases). 还不清楚您是否只想计算购买次数,或者是否也需要实际购买日期,所以我也添加了日期(对于前4次购买)。 If a buyer made only 1 purchase, the row would show NULL in purchase 2/3/4 columns. 如果买方仅进行了1次购买,则该行将在购买2/3/4列中显示NULL。

If you want to include a range of dates or filter by location, those conditions would go in the inner select. 如果要包括日期范围或按位置过滤,则这些条件将放在内部选择中。

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;

Let me know if it works. 让我知道它是否有效。

This query will provide the buyer_id and number of purchases in the date range: 此查询将提供日期范围内的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

If you use this as a nested query you can count the buyer_id values with a given number of purchases_in_period . 如果你使用这个作为一个嵌套查询可以算buyer_id与给定的数值purchases_in_period

I would normally approach this as a union of separate queries. 我通常将其作为单独查询的组合来处理。

      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.

相关问题 使用ROW_NUMBER()将第1个ARRAY与第2个,第3个,第4个等进行比较 - Using ROW_NUMBER () to Compare 1st ARRAY to 2nd, 3rd, 4th, etc 在一行中查找第 2、3、4、5 个非 NULL 值(SQL) - finding 2nd, 3rd, 4th, 5th not NULL values in a row (SQL) 从 SQL Server 表中的带连字符的字符串列中提取第 1 个第 2 个第 3 个值直到第 n 个值 - Extract 1st 2nd 3rd values till nth value from hyphenated string column in SQL Server table SQL-在列表中为第1次,第2次,第3次创建计数 - SQL - Creating counts for 1st, 2nd, 3rd occasion in a list 在SQL中,我需要生成一个排名(第1,第2,第3)列,并停留在“联系”上 - In SQL, I need to generate a ranking (1st, 2nd, 3rd) column, getting stuck on “ties” 查询第 1、第 2、第 3 名,使用第 2 和第 3 最低的平局,每次在一行 - query 1st, 2nd, 3rd place with tie break using 2nd and 3rd lowest with each time on one row 合并第一行和第二行,第三行和第四行,依此类推 - Merge 1st and Second Row, 3rd and 4th Row and so on 在Access中为每个人选择第1行,第2行和第3行 - Select 1st, 2nd, and 3rd rows for each person in Access 我如何 Select 不同列中的第一个、第二个和第三个值 - 访问女士 - How can I Select 1st, 2nd and 3rd values in different columns - Ms Access SQL第一个列表中的第二个 - SQL any of 1st list IN 2nd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM