我希望得到每个客户大部分订购的前2项。我可以从销售表中获取以下数据
-----------------------------------------
|OrderAccount| Item | OrderDate |
|ABC | Shoes #1 | 10.01.2016 |
|ABC | Shoes #2 | 10.01.2016 |
|ABC | Shoes #2 | 13.02.2016 |
|ABC | Shoes #1 | 12.12.2016 |
|ABC | Shoes #4 | 09.01.2016 |
|RDD | Shoes #1 | 01.21.2016 |
|RDD | Shoes #2 | 01.22.2016 |
|RDD | Shoes #1 | 03.01.2016 |
|RDD | Shoes #6 | 03.11.2016 |
|RDD | Shoes #1 | 03.21.2016 |
-----------------------------------------
我使用下面提到的in earlier post查询。
SELECT b.OrderAccount, b.Item
FROM(
SELECT *, RANK() OVER(PARTITION BY a.OrderAccount ORDER BY a.count_item DESC) AS RowRank
FROM(
SELECT so.OrderAccount, so.Item, count(item) count_item
FROM SalesOrders so
GROUP BY so.OrderAccount, so.Item
) a
) b
WHERE b.RowRank <= 2
我希望从数据中获得更多信息:此项目将出现在下一个销售订单中的可能性是什么(即该项目为此客户显示的销售订单的百分比) 我可以从查询中获取此信息吗?
答案 0 :(得分:0)
根据您问题中的数据,每一行似乎都是一个单独的订单。所以,我认为你可以做到:
SELECT so.OrderAccount, so.Item,
(count_item * 1.0 / num_orders) as proportion_of_orders
FROM (SELECT so.OrderAccount, so.Item, count(item) as count_item,
SUM(COUNT(*)) OVER (PARTITION BY so.OrderAccount) as num_orders,
RANK() OVER (PARTITION BY so.OrderAccount ORDER BY COUNT(*) DESC) as rnk
FROM SalesOrders so
GROUP BY so.OrderAccount, so.Item
) so
WHERE rnk <= 2;
答案 1 :(得分:0)
我就这样做了:
SELECT a.OrderAccount, a.Item, a.Occurrence, b.SalesOrdersPerAccount,
CAST(CAST(a.Occurrence AS decimal)/CAST(b.SalesOrdersPerAccount AS decimal)*100 AS decimal(16,2)) AS Percentage
FROM
(
SELECT so.OrderAccount, so.Item, COUNT(*) AS Occurrence FROM SalesOrders so
WHERE so.Item IS NOT NULL
GROUP BY so.OrderAccount, so.Item
) a
LEFT OUTER JOIN
(
SELECT so.OrderAccount, COUNT(*) AS SalesOrdersPerAccount FROM SalesOrders so
WHERE so.Item IS NOT NULL
GROUP so.OrderAccount
) b ON a.OrderAccount = b.OrderAccount
WHERE b.SalesOrdersPerAccount IS NOT NULL
ORDER BY a.OrderAccount, Percentage DESC