Sql 如何获得每列B中重复次数最多的列A
我想得到每个客户订购最多的两件商品。我可以从销售表中获得以下数据Sql 如何获得每列B中重复次数最多的列A,sql,Sql,我想得到每个客户订购最多的两件商品。我可以从销售表中获得以下数据 --------------------------- |OrderAccount| Item | |ABC | Shoes #1 | |ABC | Shoes #2 | |ABC | Shoes #2 | |ABC | Shoes #1 | |ABC | Shoes #4 | |RDD
---------------------------
|OrderAccount| Item |
|ABC | Shoes #1 |
|ABC | Shoes #2 |
|ABC | Shoes #2 |
|ABC | Shoes #1 |
|ABC | Shoes #4 |
|RDD | Shoes #1 |
|RDD | Shoes #2 |
|RDD | Shoes #1 |
|RDD | Shoes #6 |
|RDD | Shoes #1 |
----------------------------
我怎样才能得到数据呢?
这不起作用:
SELECT so.Item,
so.OrderAccount
FROM (
SELECT so.Item,
so.OrderAccount,
row_number() OVER(Partition BY so.Item ORDER BY so.OrderAccount desc) as repeated
FROM SalesOrders so
WHERE so.Item IS NOT NULL
) AS so
WHERE so.repeated <= 2
ORDER BY so.OrderAccount
我设法找到了一个可行的解决方案,但它使用了非常糟糕的做法,并且在实际数据库中性能很差OrderAccount重命名为customer,Item重命名为Item:
-- get the top-product per customer
SELECT customer, item, MAX(cnt)
FROM (
-- get all customer-item-pairs with the associated count
SELECT customer, item, COUNT(item) AS cnt FROM tbl GROUP BY customer,item
) GROUP BY customer
UNION -- combine that with the second-top-product per customer
-- get the top-product per customer, but stripped of the first part of the result (so the second-top-product)
SELECT customer, item, MAX(cnt)
FROM (
-- get all customer-item-pairs with the associated count
SELECT customer, item, COUNT(item) AS cnt FROM tbl GROUP BY customer,item
EXCEPT --except for the customer-item-pairs which are already top-products
--this is the same as get the top-product per customer
SELECT customer, item, MAX(cnt)
FROM (
SELECT customer, item, COUNT(item) AS cnt FROM tbl GROUP BY customer,item
) GROUP BY customer
) GROUP BY customer
这可能会起作用。如果帐户下的订单数相等,它将返回2行以上
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
哥们,你刚才犯了一个关于分区和排序的错误。您希望从每位客户那里获得前2项。所以您需要按客户进行分区,并且项目中有num,所以您需要按项目进行排序 通过:
当前的结果是什么?想要的结果是什么?哪个DBMS?MS SQL Server?在此查询中,订单在何处计数,以确定每个客户最常订购的项目?这是否有效?我同意下划线d,不计算发生次数。如果帐户下的订单数相等,则返回的行数将超过2行。-这是一个有益的副作用!因为op要求排名前二的Rowsorry,所以我只是想说清楚,但是如果你不得不公开承认你的答案真的很糟糕,很糟糕,那可能就不值得发布了
SELECT so.Item,
so.OrderAccount
FROM (
SELECT so.Item,
so.OrderAccount,
row_number() OVER(Partition BY so.OrderAccount ORDER BY so.Item desc) as repeated
FROM SalesOrders so
WHERE so.Item IS NOT NULL
) AS so
WHERE so.repeated <= 2
ORDER BY so.OrderAccount