所以..这就是事情..我试图在Mysql上获得基于此表的报告。
表格包含这些列
statusvalue submitdate employeeNumber
2 2016-11-17 17:49:05 2
1 2016-11-17 17:49:11 4
3 2016-11-17 17:49:16 4
4 2016-11-17 17:50:58 2
5 2016-11-17 17:51:07 5
Status 0 = Pending
1= Started
2=inprogress
3= cancelled
4= terminated
5=unknown
现在我需要选择以下数据
date day pending started inprogress cancelled terminated total
2017-04-17 Monday 0 1 4 0 1 6
2017-04-16 Sunday 0 1 4 0 1 6
到目前为止,我可以获得日期和日期
select date(submitdate) as subdate, case weekday(date(submitdate))
WHEN 0 then 'PENDING'
WHEN 1 then 'STARTED'
WHEN 2 then 'INPROGRESS'
WHEN 3 then 'CANCELLED'
WHEN 4 then 'TERMINATED'
WHEN 5 then 'UNKNOWN' END as submitdate
from tb_status t1 where employeenumber=15 group by subdate order by subdate ;
然而不太确定如何根据状态进行计数..任何帮助将不胜感激。
答案 0 :(得分:1)
您只需要一个基本的透视查询。汇总提交日期,然后统计各种类型的状态。
SELECT
DATE(submitdate) AS subdate,
WEEKDAY(submitdate) AS day,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 0 THEN 1 END) AS PENDING,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 1 THEN 1 END) AS STARTED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 2 THEN 1 END) AS INPROGRESS,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 3 THEN 1 END) AS CANCELLED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 4 THEN 1 END) AS TERMINATED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 5 THEN 1 END) AS UNKNOWN
FROM tb_sattus t1
WHERE employeenumber = 15
GROUP BY DATE(submitdate)
ORDER BY subdate
请注意,您当前的查询不正确,因为您正在根据状态选择字符串文字。相反,在我的查询中,各种状态类型显示为列名,而您想要的计数则被选中。
答案 1 :(得分:0)
尝试一下:
SELECT
`submitDate` as `date`,
DATE_FORMAT(`submitDate`,'%a') as `day`,
SUM(if(`statusvalue` = 0,1,0)) as `pending`,
SUM(if(`statusvalue` = 1,1,0)) as `started`,
SUM(if(`statusvalue` = 2,1,0)) as `inprogress`,
SUM(if(`statusvalue` = 3,1,0)) as `cancelled`,
SUM(if(`statusvalue` = 4,1,0)) as `terminated`,
SUM(if(`statusvalue` = 5,1,0)) as `unknown`,
SUM(if(NOT `statusvalue` IN (0,1,2,3,4,5),1,0)) as `invalid`,
COUNT(`statusvalue`) as `total`
FROM `tbstatus`
GROUP BY `submitDate`
ORDER BY `submitDate`;