想要返回以下结果集 - 一列中的组和,下一个总和。
我尝试用子查询来做,但它不起作用。
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
我想要的结果是这样的:
+----+-+--+-----+
|y |m|mo|total|
+----+-+--+-----+
|2013|1|10|45 |
+----+-+--+-----+
|2013|2|15|45 |
+----+-+--+-----+
|2013|3|20|45 |
+----+-+--+-----+
有可能吗?感谢。
答案 0 :(得分:1)
这是我在我的桌子上写的一个简单的例子,只是重命名表/ 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
编辑:你的案子:
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
答案 1 :(得分:0)
以下是您要找的内容
SELECT DISTINCT year y, month m, sum( count ) OVER(PARTITION by month), sum(count) OVER(PARTITION BY year) FROM report WHERE year >= 2013