在Join条件中我想使用group by和having子句但是得到错误?如何使用group by和having子句

时间:2017-03-01 12:22:11

标签: mysql sql database database-design relational-database

当我想从表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'
              ;

2 个答案:

答案 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()来获取特定数量的小数位。
  • 如果没有必要,请不要使用反引号。它们只会使查询更难写和阅读。
  • 同样,表别名使查询更容易编写和阅读。
  • 使用24小时时间格式 - 更不容易出错。
  • 我怀疑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;