简体   繁体   中英

Group sum, group sum total in one query

want to return following result set - group sum in one column, total sum in next.

I tried to do it with subquery, but it doesn't work.

SELECT x.y, x.m, x.mcount mo, sum( x.mcount ) total
FROM (
    SELECT r.year y, r.month m, sum( r.count ) mcount
    FROM `report` r
    WHERE r.year >= '2013'
    AND r.day <=10
    GROUP BY r.year, r.month
) AS x
GROUP BY x.y, x.m

What I want to as result is something like this:

+----+-+--+-----+
|y   |m|mo|total|
+----+-+--+-----+
|2013|1|10|45   |
+----+-+--+-----+
|2013|2|15|45   |
+----+-+--+-----+
|2013|3|20|45   |
+----+-+--+-----+

Is it possible? Thanks.

here's a simple example i wrote on one on my tables, just rename the table/cols

select year(k.date), month(k.date), sum(k.impression), a.total 
from kpi_funnel k
where date between '2012-01-01' and '2013-05-05'
join (select sum(impression) as total from kpi_funnel where date between '2012-01-01' and '2013-05-05') a
group by 1,2

edit: your case:

 SELECT r.year y, r.month m, sum( r.count ) mcount, total.total
    FROM `report` r
    join
    (SELECT sum( r.count ) as total FROM `report` r WHERE r.year >= '2013' AND r.day <=10) as total
    WHERE r.year >= '2013'
    AND r.day <=10
    GROUP BY r.year, r.month

这就是您要寻找的

SELECT DISTINCT year y,  month m, sum( count ) OVER(PARTITION by month), sum(count) OVER(PARTITION BY year)     FROM report WHERE year >= 2013

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.

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