【问题标题】:Count Distinct Duplicates Within Groups in MySql在 MySql 中计算组内的不同重复项
【发布时间】:2013-04-26 05:16:51
【问题描述】:

我有一个名为 main_table 的 SQL 表:

编号 |产品编号 |购买ID |购买者 ID ---+------------+-------------+------------- 1 | 1 | 1 | 1 2 | 1 | 2 | 1 3 | 1 | 3 | 1 4 | 1 | 4 | 2 5 | 1 | 5 | 2 6 | 1 | 6 | 3 7 | 2 | 1 | 1 8 | 2 | 4 | 2 9 | 2 | 5 | 2 10 | 2 | 7 | 2 11 | 2 | 8 | 2 12 | 2 | 6 | 3 13 | 2 | 9 | 3 14 | 2 | 10 | 3 15 | 2 | 11 | 3 16 | 2 | 12 | 4 17 | 2 | 13 | 4

我需要按product_id分组,找到四样东西:

  • # 购买
  • # 购买者
  • # 重复购买
  • #重复购买者

所以前3个比较简单……

SELECT FROM `main_table`
  product_id,
  COUNT(DISTINCT `purchase_id`) AS `purchases`,
  COUNT(DISTINCT `purchaser_id`) AS `purchasers`,
  (COUNT(DISTINCT `purchase_id`) - COUNT(DISTINCT `purchaser_id`)) AS `repeat_purchases`,
  (??????) AS `repeat_purchasers`
GROUP BY product_id
ORDER BY product_id ASC

?????? 是什么才能得到下表:

product_id | purchases | purchasers | repeat_purchases | repeat_purchasers
-----------+-----------+------------+------------------+------------------
         1 |         6 |          3 |                3 |                2 
         2 |        11 |          4 |                7 |                3 

【问题讨论】:

  • repeat_purchasers 中的 2 和 3 是如何设置的?逻辑?
  • 逻辑在问题的定义中:(如果我有逻辑,我就不会问这个问题......
  • @shadowice222- 按逻辑我的意思是你为什么写 2 & 3 而不是 4 和 5?
  • 因为那是重复购买者的数量
  • 好的。而且您需要查看Markdown help 来轻松格式化帖子,就像我在这里所做的那样:)

标签: mysql sql group-by distinct having


【解决方案1】:
SELECT  a.product_id,
        COUNT(DISTINCT a.purchase_id) AS purchases,
        COUNT(DISTINCT a.purchaser_id) AS purchasers,
        (COUNT(DISTINCT a.purchase_id) - COUNT(DISTINCT a.purchaser_id)) AS repeat_purchases,
        COALESCE(c.totalCount,0) AS repeat_purchasers
FROM    main_table a
        LEFT JOIN
        (
            SELECT  product_id, COUNT(totalCOunt) totalCount
            FROM    
                    (
                        SELECT  product_id, purchaser_id, COUNT(*) totalCOunt
                        FROM    main_table
                        GROUP   BY product_id, purchaser_id
                        HAVING  COUNT(*) > 1
                    ) s
            GROUP   BY product_id
        ) c ON  a.product_id = c.product_id
GROUP   BY product_id

输出

╔════════════╦═══════════╦════════════╦══════════════════╦═══════════════════╗
║ PRODUCT_ID ║ PURCHASES ║ PURCHASERS ║ REPEAT_PURCHASES ║ REPEAT_PURCHASERS ║
╠════════════╬═══════════╬════════════╬══════════════════╬═══════════════════╣
║          1 ║         6 ║          3 ║                3 ║                 2 ║
║          2 ║        11 ║          4 ║                7 ║                 3 ║
╚════════════╩═══════════╩════════════╩══════════════════╩═══════════════════╝

【讨论】:

  • 您使用哪种工具来格式化这样的表格(输出)?
【解决方案2】:

我会这样做:

select a.product_id, count(*) as purchases, count(distinct(a.purchaser_id)) as 
purchasers, count(*) - count(distinct(a.purchaser_id)) as repeat_purchases, 
b.repeat_purchasers from main_table a, 
(select x.product_id, count(*) as repeat_purchasers from 
   (select y.product_id, y.purchaser_id from main_table y 
    group by y.purchaser_id, y.product_id having y.count > 1) x 
 group by x.product_id) b group by
a.product_id,b.repeat_purchasers,b.product_id having 
a.product_id = b.product_id`

这与 John 的基本相同,但没有 JOIN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2015-01-12
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多