繁体   English   中英

使用SUM()的MYSQL选择查询

[英]MYSQL Select Query with SUM()

我有下表:

| campaign_id | source_id | clicked | viewed |
----------------------------------------------
| abc         | xxx       | 0       | 0      |  
| abc         | xxx       | 1       | 0      |
| abc         | xxx       | 1       | 1      | 
| abc         | yyy       | 0       | 0      |    
| abc         | yyy       | 1       | 0      |    
| abc         | yyy       | 1       | 1      |    
| abc         | yyy       | 0       | 0      |

我需要以下输出:

xxx > Total: 3 // Clicked: 2 // Viewed 1
yyy > Total: 4 // Clicked: 2 // Viewed 1

我知道我必须在查询中使用某种SUM(),但我不知道如何区分source_id中的那些多个唯一值(类似于foreach,idk)。

如何通过仅使用一个查询来获得显示来自所有唯一source_id的统计信息的输出?

试试这个:

SELECT source_id, (SUM(clicked)+SUM(viewed)) AS Total
FROM your_table
GROUP BY source_id

以下是您将示例数据加载到名为campaign的表中:

CREATE TABLE campaign
(
    campaign_id VARCHAR(10),
    source_id VARCHAR(10),
    clicked int,
    viewed int
);
INSERT INTO campaign VALUES
('abc','xxx',0,0),
('abc','xxx',1,0),
('abc','xxx',1,1),
('abc','yyy',0,0),
('abc','yyy',1,0),
('abc','yyy',1,1),
('abc','yyy',0,0);
SELECT * FROM campaign;

这是它包含的内容

mysql> DROP TABLE IF EXISTS campaign;
CREATE TABLE campaign
(
    campaign_id VARCHAR(10),
    source_id VARCHAR(10),
    clicked int,
    viewed int
);
INSERT INTO campaign VALUES
('abc','xxx',0,0),
('abc','xxx',1,0),
('abc','xxx',1,1),
('abc','yyy',0,0),
('abc','yyy',1,0),
('abc','yyy',1,1),
('abc','yyy',0,0);
SELECT * FROM campaign;
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE campaign
    -> (
    ->     campaign_id VARCHAR(10),
    ->     source_id VARCHAR(10),
    ->     clicked int,
    ->     viewed int
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO campaign VALUES
    -> ('abc','xxx',0,0),
    -> ('abc','xxx',1,0),
    -> ('abc','xxx',1,1),
    -> ('abc','yyy',0,0),
    -> ('abc','yyy',1,0),
    -> ('abc','yyy',1,1),
    -> ('abc','yyy',0,0);
Query OK, 7 rows affected (0.07 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM campaign;
+-------------+-----------+---------+--------+
| campaign_id | source_id | clicked | viewed |
+-------------+-----------+---------+--------+
| abc         | xxx       |       0 |      0 |
| abc         | xxx       |       1 |      0 |
| abc         | xxx       |       1 |      1 |
| abc         | yyy       |       0 |      0 |
| abc         | yyy       |       1 |      0 |
| abc         | yyy       |       1 |      1 |
| abc         | yyy       |       0 |      0 |
+-------------+-----------+---------+--------+
7 rows in set (0.00 sec)

现在,这是一个很好的查询,你需要总计和总和的活动+总计

SELECT
    campaign_id,
    source_id,
    count(source_id) total,
    SUM(clicked) sum_clicked,
    SUM(viewed) sum_viewed
FROM campaign
GROUP BY campaign_id,source_id
WITH ROLLUP;

这是输出:

mysql> SELECT
    ->     campaign_id,
    ->     source_id,
    ->     count(source_id) total,
    ->     SUM(clicked) sum_clicked,
    ->     SUM(viewed) sum_viewed
    -> FROM campaign
    -> GROUP BY campaign_id,source_id
    -> WITH ROLLUP;
+-------------+-----------+-------+-------------+------------+
| campaign_id | source_id | total | sum_clicked | sum_viewed |
+-------------+-----------+-------+-------------+------------+
| abc         | xxx       |     3 |           2 |          1 |
| abc         | yyy       |     4 |           2 |          1 |
| abc         | NULL      |     7 |           4 |          2 |
| NULL        | NULL      |     7 |           4 |          2 |
+-------------+-----------+-------+-------------+------------+
4 rows in set (0.00 sec)

现在用CONCAT功能装扮它

SELECT
CONCAT(
    'Campaign ',campaign_id,
    ' Source ',source_id,
    ' > Total: ',
    total,
    ' // Clicked: ',
    sum_clicked
    ,' // Viewed: ',
    sum_viewed) "Campaign Report"
FROM
(SELECT
    campaign_id,
    source_id,
    count(source_id) total,
    SUM(clicked) sum_clicked,
    SUM(viewed) sum_viewed
FROM campaign
GROUP BY
campaign_id,source_id) A;

这是输出

mysql> SELECT
    -> CONCAT(
    ->     'Campaign ',campaign_id,
    ->     ' Source ',source_id,
    ->     ' > Total: ',
    ->     total,
    ->     ' // Clicked: ',
    ->     sum_clicked
    ->     ,' // Viewed: ',
    ->     sum_viewed) "Campaign Report"
    -> FROM
    -> (SELECT
    ->     campaign_id,
    ->     source_id,
    ->     count(source_id) total,
    ->     SUM(clicked) sum_clicked,
    ->     SUM(viewed) sum_viewed
    -> FROM campaign
    -> GROUP BY
    -> campaign_id,source_id) A;
+---------------------------------------------------------------+
| Campaign Report                                               |
+---------------------------------------------------------------+
| Campaign abc Source xxx > Total: 3 // Clicked: 2 // Viewed: 1 |
| Campaign abc Source yyy > Total: 4 // Clicked: 2 // Viewed: 1 |
+---------------------------------------------------------------+
2 rows in set (0.00 sec)

试试看 !!!

SELECT source_id, SUM(clicked + viewed) AS 'Total'
FROM your_table
GROUP BY source_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM