[英]I need a query that will get the sum(column) with 4 different where conditions which includes group by condition
Here's my query: 这是我的查询:
SELECT
date,
(SELECT sum(sessions) as org FROM datatwo WHERE medium='Organic Search' GROUP by date),
(SELECT sum(sessions) as soc FROM datatwo WHERE medium='Social' GROUP BY date),
(SELECT sum(sessions) as dir FROM datatwo WHERE medium='Direct' group by date),
(SELECT sum(sessions) as ref FROM datatwo WHERE medium='Referral')
FROM datatwo
GROUP BY date
I am trying to combine 4 queries in a single query but I am getting this error, I am using mysql DB. 我试图在一个查询中组合4个查询,但出现此错误,我正在使用mysql DB。
1242 - Subquery returns more than 1 row 1242-子查询返回多于1行
When you use the result of a query as a column, it must return exactly one row and one column. 当您将查询结果用作列时,它必须恰好返回一行和一列。 Expressions like this: 像这样的表达式:
(SELECT sum(sessions) as org FROM datatwo WHERE medium='Organic Search' GROUP by date)
will return one row for every distinct date
value. 将为每个不同的date
值返回一行。
I suspect you want this: 我怀疑你想要这个:
SELECT
date,
sum(case when medium='Organic Search' then sessions end) as org,
sum(case when medium='Social' then sessions end) as soc,
sum(case when medium='Direct' then sessions end) as dir,
sum(case when medium='Referral' then sessions end) as ref
FROM datatwo
GROUP BY date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.