Mysql 行值作为具有计数 (*) 的列
Mysql row values as columns with count(*)
我有一个具有以下结构的 table,我正在尝试将行值用作列,并将它们的值用作出现次数。
| date |order|status|
-----+-----+----------------
| 2018-05-22| 1| closed|
| 2018-05-22| 2| closed|
| 2018-05-22| 3| closed|
| 2018-05-22| 4| open |
| 2018-05-22| 4| open |
输出:
| date |closed|open|
-----+-----+----------------
| 2018-05-22| 3 | 2|
当我使用以下查询获取计数 (*) 值时出现错误..."invalid use of group function"
select date,
max(case when `status` ='closed' then count(*) end) closed,
max(case when `status` = 'open' then count(*) end) open
from orders where date ='2018-05-22' group by date,status
感谢帮助修复它...
下面的怎么样?
select
date,
sum(case when `status` ='closed' then 1 else 0 end) closed,
sum(case when `status` = 'open' then 1 else 0 end) open
from orders where date ='2018-05-22' group by date
我有一个具有以下结构的 table,我正在尝试将行值用作列,并将它们的值用作出现次数。
| date |order|status|
-----+-----+----------------
| 2018-05-22| 1| closed|
| 2018-05-22| 2| closed|
| 2018-05-22| 3| closed|
| 2018-05-22| 4| open |
| 2018-05-22| 4| open |
输出:
| date |closed|open|
-----+-----+----------------
| 2018-05-22| 3 | 2|
当我使用以下查询获取计数 (*) 值时出现错误..."invalid use of group function"
select date,
max(case when `status` ='closed' then count(*) end) closed,
max(case when `status` = 'open' then count(*) end) open
from orders where date ='2018-05-22' group by date,status
感谢帮助修复它...
下面的怎么样?
select
date,
sum(case when `status` ='closed' then 1 else 0 end) closed,
sum(case when `status` = 'open' then 1 else 0 end) open
from orders where date ='2018-05-22' group by date