SQL:计算出现的事件并对列进行主查询分组

时间:2017-08-22 11:58:06

标签: mysql sql group-by subquery

作为一名CRM用户,我很想知道他卖掉了每件商品多少次,但是我已经开始讨论这件事了。

Normaly我通过一些子查询获得总数,甚至总收入。问题是这个用户有多个“独特”的项目具有相同的名称,但不同的单位价格。他喜欢这些分组,并将金额添加到打印的行中。

SQL当前有效,但是如果我添加GROUP BY,它将按预期显示唯一的名称而不会正确计算总和。

    SELECT 
        `app_separate_costs`.*, 
        (SELECT 
            SUM(`a`.`amount`) AS count 
            FROM (`app_invoice_rows` AS `a`) 
            WHERE `a`.`separate_cost_id` = `app_separate_costs`.`id`
        ) AS count, 
        (SELECT 
            SUM(`b`.`amount`)*(`b`.`unit_price`) AS total 
            FROM (`app_invoice_rows` AS `b`) 
            WHERE `b`.`separate_cost_id` = `app_separate_costs`.`id`
        ) AS total 
    FROM (`app_separate_costs`) 
    WHERE `app_separate_costs`.`is_text` = 0 
        AND `app_separate_costs`.`is_header` = 0 
        AND `app_separate_costs`.`name` != '-' 
    ##GROUP BY `app_separate_costs`.`name` --> this will cause incorrect sum
    ORDER BY `count` DESC 

有人可以帮我这个吗?

示例数据

表app_invoice_rows

+------------------+--------------------------------------+--------+------------+
| separate_cost_id |                 name                 | amount | unit_price |
+------------------+--------------------------------------+--------+------------+
|                1 | Domein registratie                   |   4.00 |      16.00 |
|                1 | Domein registratie                   |   4.00 |      16.00 |
|               55 | Domein registratie                   |   2.00 |      20.00 |
|                1 | Domein registratie                   |   2.00 |      16.00 |
|                2 | BO - Randapperatuur                  |   2.00 |     123.14 |
|                6 | Additional IP Subnets                |   3.00 |       8.06 |
|               99 | 2BY - Desktop                        |   1.00 |     809.00 |
|             1030 | Uit te voeren werken op verplaatsing |   2.00 |      65.00 |
|             1031 | Verplaatsing                         |   1.00 |      45.00 |
+------------------+--------------------------------------+--------+------------+

表app_separate_costs将id,name,unit_price作为字段(以及其他不相关的列)

期望的结果

+-------------------------------------------+--------+------------+
|                   name                    |  count |    total   |
+-------------------------------------------+--------+------------+
| Domein registratie                        | 13.00  | 200.00     |
| BO - Randapperatuur                       | 2.00   | 246.28     |
| Additional IP Subnets                     | 3.00   | 24.18      |
| 2BY - Desktop                             | 1.00   | 809.00     |
| Uit te voeren werken op verplaatsing      | 2.00   | 130.00     |
| Verplaatsing                              | 1.00   | 45.00      |
+-------------------------------------------+--------+------------+

1 个答案:

答案 0 :(得分:2)

SELECT 
    `app_separate_costs`.*, 
    SUM(`a`.`amount`) AS count, 
    SUM((`a`.`amount`)*(`a`.`unit_price`)) AS total 
FROM (`app_separate_costs`) 
     INNER JOIN (`app_invoice_rows` AS `a`) ON `a`.`separate_cost_id` = `app_separate_costs`.`id`
WHERE `app_separate_costs`.`is_text` = 0 
    AND `app_separate_costs`.`is_header` = 0 
    AND `app_separate_costs`.`name` != '-' 
GROUP BY `app_separate_costs`.`name` 
ORDER BY `count` DESC

猜猜应该是这样的。加入表一次而不是使用子查询。按名称分组后,为每个名称提供汇总计算。