作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
SELECT CUST_ID, AVG(freq), AVG(amount) ,month from
(SELECT CUST_ID, DATE_FORMAT(CDATE, "%Y%m") as month,
COUNT(*) as freq, SUM(BILLS/count(*)) as amount FROM PROCESSED
where CDATE>= DATE(NOW() - INTERVAL 6 MONTH) GROUP BY CUST_ID, month having count(*) >=3
order by cust_id, month) T where CUST_ID != 2750 and CUST_ID != 1 group by CUST_ID
我了解 group by 子句不允许非聚合列,我需要月份作为列吗?
最佳答案
SELECT CUST_ID, AVG(freq), AVG(amount),MONTH from
(SELECT CUST_ID, DATE_FORMAT(CDATE, "%Y%m") as month,
COUNT(*) as freq, SUM(BILLS)/count(*) as amount FROM PROCESSED
where CDATE>= DATE(NOW() - INTERVAL 6 MONTH) GROUP BY CUST_ID, MONTH having count(*) >=3
order by cust_id, month) T where CUST_ID != 2750 and CUST_ID != 1 group by CUST_ID,MONTH
我得到了解决方案 - 问题是分组依据没有月份列,因此月份列不知道在聚合结果中附加到哪一行
关于MYSQL DB 错误 SELECT 列表的表达式 #2 不在 GROUP BY 子句中并且包含非聚合列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49396678/
我是一名优秀的程序员,十分优秀!