统计有多少人第二次购买了相同的商品,有多少人第二次没有购买该商品(与第一次相比)
count how many people who buy the same item at second time and how many people who did not buy the second item at second time (compare to firsttime)
这里是 question:count 有多少人第二次购买了相同的商品,有多少人第二次没有购买相同的商品(与 buying_order = 1
比较)
我们有一个 table:
id
item
date
buying_order
1
1
19990101
1
1
1
19990102
2
2
1
19990102
1
2
2
19990101
2
2
3
19990103
1
更明确地说,如果 id 1
在 19990101
购买 item 1
,那是第一个购买的商品,所以 buying_order
是 1。我们关心的是知道如何许多人第一次购买特定商品,有多少人第二次购买该特定商品。
以下是我试过但行不通的逻辑
SELECT a.id FROM (SELECT id FROM table WHERE buying_order = 1) AS a
LEFT JOIN table AS b ON a.id = b.id
WHERE b.buying_order=2 and a.item = b.item
预期结果:
item
first_purchase_customer
second_purchase
second_buy_other_item_count
1
2
1
1
对于item 1
,在order 1
有两个第一次购买,只有一个客户在order 2
购买item 1
。
注意:顺序可以高于 2,这样 order = 3,4,..., but we only care the people who buy or not buy the same item at their second purchase.
这不是一个非常明确的问题。我还会提出问题,为什么你甚至有 buying_order 列,因为它似乎确实没有增加价值,你已经有每个用户和项目的行以及日期!
您可以简单地执行此操作来计算订单,再次完全忽略 buying_order 列
CREATE TABLE #MyTable (
UserId INT NOT NULL,
ItemId INT NOT NULL,
[Date] DATE NOT NULL,
BuyingOrder INT NOT NULL
);
INSERT INTO #MyTable(UserId, ItemId, [Date], BuyingOrder)
VALUES
(1, 1, '19990101', 1),
(1, 1, '19990102', 2),
(2, 1, '19990102', 1),
(2, 2, '19990101', 2),
(2, 3, '19990103', 1);
GO
SELECT UserId, ItemId, COUNT(*) NumberOfTimesBought
FROM #MyTable
GROUP BY UserId, ItemId
我也在想你可以使用下面的 ROW_NUMBER 解决方案,它只会给你购买不止一次的物品:
WITH T AS (
SELECT
UserId,
ItemId,
ROW_NUMBER() OVER(PARTITION BY UserId, ItemId ORDER BY [Date] DESC) AS RowNumber
FROM
#MyTable
)
SELECT UserId, ItemId
FROM T
WHERE RowNumber > 1
这里是 question:count 有多少人第二次购买了相同的商品,有多少人第二次没有购买相同的商品(与 buying_order = 1
比较)
我们有一个 table:
id | item | date | buying_order |
---|---|---|---|
1 | 1 | 19990101 | 1 |
1 | 1 | 19990102 | 2 |
2 | 1 | 19990102 | 1 |
2 | 2 | 19990101 | 2 |
2 | 3 | 19990103 | 1 |
更明确地说,如果 id 1
在 19990101
购买 item 1
,那是第一个购买的商品,所以 buying_order
是 1。我们关心的是知道如何许多人第一次购买特定商品,有多少人第二次购买该特定商品。
以下是我试过但行不通的逻辑
SELECT a.id FROM (SELECT id FROM table WHERE buying_order = 1) AS a
LEFT JOIN table AS b ON a.id = b.id
WHERE b.buying_order=2 and a.item = b.item
预期结果:
item | first_purchase_customer | second_purchase | second_buy_other_item_count |
---|---|---|---|
1 | 2 | 1 | 1 |
对于item 1
,在order 1
有两个第一次购买,只有一个客户在order 2
购买item 1
。
注意:顺序可以高于 2,这样 order = 3,4,..., but we only care the people who buy or not buy the same item at their second purchase.
这不是一个非常明确的问题。我还会提出问题,为什么你甚至有 buying_order 列,因为它似乎确实没有增加价值,你已经有每个用户和项目的行以及日期!
您可以简单地执行此操作来计算订单,再次完全忽略 buying_order 列
CREATE TABLE #MyTable (
UserId INT NOT NULL,
ItemId INT NOT NULL,
[Date] DATE NOT NULL,
BuyingOrder INT NOT NULL
);
INSERT INTO #MyTable(UserId, ItemId, [Date], BuyingOrder)
VALUES
(1, 1, '19990101', 1),
(1, 1, '19990102', 2),
(2, 1, '19990102', 1),
(2, 2, '19990101', 2),
(2, 3, '19990103', 1);
GO
SELECT UserId, ItemId, COUNT(*) NumberOfTimesBought
FROM #MyTable
GROUP BY UserId, ItemId
我也在想你可以使用下面的 ROW_NUMBER 解决方案,它只会给你购买不止一次的物品:
WITH T AS (
SELECT
UserId,
ItemId,
ROW_NUMBER() OVER(PARTITION BY UserId, ItemId ORDER BY [Date] DESC) AS RowNumber
FROM
#MyTable
)
SELECT UserId, ItemId
FROM T
WHERE RowNumber > 1