2011-11-03 18 views
14

tengo la siguiente tabla:MYSQL consulta de selección con 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  | 

necesito el siguiente resultado:

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

sé que tengo que usar algún tipo de SUM() en mi consulta, pero no sé cómo diferir entre esos múltiples valores únicos en el source_id (algo así como foreach, idk).

¿Cómo puedo obtener un resultado que muestre las estadísticas de todos los únicos source_ids utilizando solo una consulta?

Respuesta

19

Prueba esto:

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

¿No debería querer agrupar por source_id? De lo contrario, ¡lo intentaré! – DonCroce

+0

Deberías 'GROUP BY source_Id'. También creo que quiere 'SELECT source_id, SUM (hecho clic) + SUM (visto) como Total' –

+0

@DonCroce: sí, solo un error tipográfico. Editado mi publicación :) – Marco

3

Aquí está su muestra de datos cargados en una tabla llamada campaña:

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; 

Aquí es lo que contiene

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) 

Ahora, aquí hay una Buena consulta que necesita sumar y sumar por campaña + gran total

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; 

Aquí está la salida:

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) 

Ahora que se visten con la función 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; 

Aquí es que la producción

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) 

darle una oportunidad !! !

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