mysql - 如何通过 SELECT 子查询聚合进行分组?
问题描述
第一次来这里,希望得到帮助。(MySQL)我尝试在 SELECT 语句中使用子查询,但是当我 GROUP BY 时,子查询的单个聚合值输出只会为表中的所有行生成一个相同的值。这意味着它们没有分组,对吧?我离做到这一点有多近?谢谢
SELECT
c.name, ca.name, DATE_FORMAT(sp.created,'%Y%m') AS yr_month,
ss.signup_source, count(sp.seller_profile_id) AS No_seller_profiles,
(SELECT SUM(seller_invoice.gbp_value)/100
FROM seller_invoice JOIN seller_profile
ON seller_invoice.seller_profile_id = seller_profile.seller_profile_id
WHERE seller_invoice.created BETWEEN seller_profile.created AND ADDDATE(seller_profile.created, INTERVAL 30 DAY)),
(SELECT count(project_response.project_response_id)
FROM project_response JOIN seller_profile
ON project_response.seller_profile_id = seller_profile.seller_profile_id
WHERE project_response.created BETWEEN seller_profile.created AND ADDDATE(seller_profile.created, INTERVAL 30 DAY) AND project_response.is_visible_to_seller = 1)
FROM seller_profile AS sp
JOIN country AS c ON sp.country_id = c.country_id
JOIN seller_category AS sc ON sp.seller_profile_id = sc.seller_profile_id
JOIN category AS ca ON sc.category_id = ca.category_id
JOIN seller_signup_source AS ss ON sp.seller_profile_id = ss.seller_profile_id
WHERE sp.created BETWEEN '2018-11-01' AND '2018-12-31'
GROUP BY 1,2,3,4;
解决方案
推荐阅读
- reactjs - 更新 twilio flex 网络聊天配置
- git - 在 Git 中获取当前签出的完整参考名称
- angular - Angular:没有重载匹配此调用错误
- ssis - BIML 错误:无法解析对属性“属性”中的 OdbcSource12.SQLCommand 的引用。'' 是无效的
- html - 是否可以为一张特定图像使用物理像素而不是逻辑像素?
- sas - 将 SAS 代码转换为另一种语言,以变成独立的可分发 exe
- java - Spark SQL - java.lang.UnsupportedOperationException:转换列时为empty.init
- javascript - 客户端可以“等待”来自服务器的流吗?
- javascript - Firefox 不使用 getAllResponseHeaders 显示内容长度标头
- python - 有没有一种简单的方法可以在 vim 中反转 json 的漂亮打印?