如何在每列B中获得最频繁重复的列A.

时间:2016-06-30 08:44:55

标签: sql

我希望得到每个客户大部分订购的前2项。我可以从销售表中获取以下数据

 ---------------------------
 |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

3 个答案:

答案 0 :(得分:2)

这可能会有效。如果帐户的订单数量相同,它将返回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

答案 1 :(得分:0)

伙计,你刚刚对分区和顺序犯了错误。你想得到每个客户的前2项。因此您需要按客户进行分区,并且项目中有#num,因此您需要按项目排序。

通过:

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

答案 2 :(得分:-1)

我设法获得了一个可行的解决方案,但它使用了非常糟糕的做法,并且在现实生活中的数据库上会有糟糕的表现(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