我尝试创建一个返回表格的查询,如下所示:
基本上我有一个用户可以输入案例的表格。每个案例都有以下字段(datedCreated,status)。
可能有三(3)个状态(新的,进度,关闭)
所以我想检索每一天的每个状态的数量。
每一行看起来都像这样:
我有一些看起来像这样的东西,但似乎无法让它发挥作用。
SELECT DAYNAME(dateAdded)AS Date, COUNT(status) AS Count, GROUP_CONCAT(CONCAT(status, ',', "54") SEPARATOR ',' ) AS Text FROM clients GROUP BY DATE(dateAdded) ORDER BY dateAdded LIMIT 7
我现在知道我有三个colomns,但我需要它在一列中。
提前感谢您的帮助。
答案 0 :(得分:0)
尝试:
SELECT concat(
DAYNAME(dateAdded),', '
sum(case when status = 'new' then 1 else 0 end),','
sum(case when status = 'progress' then 1 else 0 end),','
sum(case when status = 'closed' then 1 else 0 end)) as single_col
FROM clients
GROUP BY DATE(dateAdded)
ORDER BY dateAdded
答案 1 :(得分:0)
为什么不将小组连成结果?
SELECT
GROUP_CONCAT(t_date, t_new, t_progress, t_closed) AS 'Status Per Day'
FROM
( SELECT
DAYNAME(dateAdded)AS t_date,
SUM(status = 'new') t_new,
SUM(status = 'progress') t_progress,
SUM(status = 'closed') t_closed
FROM clients
GROUP BY DATE(dateAdded)
ORDER BY dateAdded LIMIT 7
) t
GROUP BY t_date