当我想从表it_Service_ticket中获取数据时。错误是
'having子句'中的未知列'it_service_ticket.xetr'
如何使用分组和有条件?请帮帮我
SELECT Assignedto,COUNT(Assignedto) as TC
,CONCAT(count(case when STATUS = 'CLOSE' then 1 else null end) * 100 / count(1), '%') as SC
,CONCAT(count(case when STATUS = 'PENDING' then 1 else null end) * 100 / count(1), '%') as PC
,SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 as WH ,(540-sum(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr)))/60 as VH,
COUNT(Feedback_Rate)/COUNT(Assignedto)*100 as Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 as AVG_Feedback
FROM `it_service_ticket`
INNER JOIN `it_problem`
ON `it_service_ticket`.`it_problem_id`=`it_problem`.`it_problem_id`
INNER JOIN `city_master`
ON `it_service_ticket`.cityid=`city_master`.city_id
GROUP BY Assignedto
HAVING `it_service_ticket`.`xetr` BETWEEN '2017-01-01 12:00:00 AM' AND '2017-03-31 12:00:00 PM'
;
答案 0 :(得分:0)
我认为你只想要where
,而不是having
:
SELECT Assignedto, COUNT(Assignedto) as TC,
CONCAT(AVG(STATUS = 'CLOSE') * 100, '%') as SC,
CONCAT(AVG(STATUS = 'PENDING') * 100, '%') as PC,
SUM(TIMESTAMPDIFF(MINUTE, Request_Date, Xetr))/60 as WH ,
(540-SUM(TIMESTAMPDIFF(MINUTE, Request_Date, Xetr)))/60 as VH,
COUNT(Feedback_Rate)/COUNT(Assignedto)*100 as Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 as AVG_Feedback
FROM it_service_ticket st INNER JOIN
it_problem` p
ON st.it_problem_id = p.it_problem_id INNER JOIN
`city_master` cm
ON st.cityid = cm.city_id
WHERE st.xetr BETWEEN '2017-01-01 00:00:00' AND '2017-03-31 12:00:00'
GROUP BY Assignedto;
通常,在过滤聚合函数时仅使用HAVING
。
注意:
format()
来获取特定数量的小数位。Feed_Percent
的计算是否正确。 COUNT()
计算非NULL
值的数量,因此它们的比率可能为1. 答案 1 :(得分:0)
您必须在“选择”字段中添加Xetr
。如果不使用此功能,则无法将having
条件与Xetr
一起使用
试试这个
SELECT Assignedto,COUNT(Assignedto) as TC
,CONCAT(count(case when STATUS = 'CLOSE' then 1 else null end) * 100 / count(1), '%') as SC
,CONCAT(count(case when STATUS = 'PENDING' then 1 else null end) * 100 / count(1), '%') as PC
,SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 as WH ,(540-sum(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr)))/60 as VH,
COUNT(Feedback_Rate)/COUNT(Assignedto)*100 as Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 as AVG_Feedback,Xetr
FROM `it_service_ticket`
INNER JOIN `it_problem`
ON `it_service_ticket`.`it_problem_id`=`it_problem`.`it_problem_id`
INNER JOIN `city_master`
ON `it_service_ticket`.cityid=`city_master`.city_id
GROUP BY Assignedto
HAVING `it_service_ticket`.`Xetr` BETWEEN '2017-01-01 12:00:00 AM' AND '2017-03-31 12:00:00 PM';
但是在My View中你必须在where子句中使用Xetr
,如下所述:
SELECT Assignedto,COUNT(Assignedto) as TC
,CONCAT(count(case when STATUS = 'CLOSE' then 1 else null end) * 100 / count(1), '%') as SC
,CONCAT(count(case when STATUS = 'PENDING' then 1 else null end) * 100 / count(1), '%') as PC
,SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 as WH ,(540-sum(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr)))/60 as VH,
COUNT(Feedback_Rate)/COUNT(Assignedto)*100 as Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 as AVG_Feedback,Xetr
FROM `it_service_ticket`
INNER JOIN `it_problem`
ON `it_service_ticket`.`it_problem_id`=`it_problem`.`it_problem_id`
INNER JOIN `city_master`
ON `it_service_ticket`.cityid=`city_master`.city_id
where `it_service_ticket`.`Xetr` BETWEEN '2017-01-01 12:00:00 AM' AND '2017-03-31 12:00:00 PM'
GROUP BY Assignedto;