Here I ma trying to do
SELECT b.sponso , b.source_id , Count(Distinct b.source_id) As nb_part
FROM buzzes b
WHERE b.type = 3 AND b.sponso_id != 0
GROUP BY b.source_id , b.sponso
And the output :
+------------+--------------+--------+
| Sponso | source_id | nb_part |
+------------+--------------+--------+
| A | 954711 | 1 |
| A | 587741 | 1 |
| B | 321447 | 1 |
| B | 350 | 1 |
+------------+--------------+--------+
And what I am expected to have
+------------+--------------+--------+
| Sponso | source_id | nb_part |
+------------+--------------+--------+
| A | 954711 | 2 |
| B | 321447 | 2 |
+------------+--------------+--------+
I wanted to count the sum of nb_part and group by Sponso but I am stuck with my query
It seems you want the maximum source_id
and the number of different source_id
per sponso
. "Per sponso" translates to GROUP BY sponso
in SQL.
SELECT sponso, MAX(source_id) AS source_id, COUNT(DISTINCT source_id) AS nb_part
FROM buzzes
WHERE type = 3 AND sponso_id != 0
GROUP BY sponso
ORDER BY sponso;
Or maybe you want to aggregate over your query:
SELECT sponso, MAX(source_id) AS source_id, SUM(nb_part) AS nb_part
FROM
(
SELECT b.sponso , b.source_id , Count(Distinct b.source_id) As nb_part
FROM buzzes b
WHERE b.type = 3 AND b.sponso_id != 0
GROUP BY b.source_id , b.sponso
)
GROUP BY sponso
ORDER BY sponso;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.