mysql - 将计算的 COUNT 列从一个视图添加到另一个视图
问题描述
我有一个视图,它引用了一个带有供应商联系信息的供应商表和一个 vendor_ratings 表来显示每个供应商的平均评级。该视图在提取供应商信息和评级平均值时有效,但我有另一个视图可以告诉我每个供应商被评级的次数。这个视图工作正常,但我无法让它在我的加入视图中工作。
这是适用于供应商信息和平均评级的视图代码:
SELECT
vendors.ID,
vendors.Vendor,
ROUND(((AVG(`Cost_Rating`) + AVG(`Documentation_Rating`) + AVG(`Safety_Rating`) + AVG(`Equipment_Rating`) + AVG(`Performance_Rating`) + AVG(`Promptness_Rating`) + AVG(`Communication_Rating`))/7.0), 2) AS `Overall Rating`,
vendors.`Phone #`,
vendors.`Fax #`,
vendors.Website,
vendors.`Physical Address`,
vendors.`P.O. Box`,
vendors.City,
vendors.`State`,
vendors.Zip,
vendors.`Region Serving`,
vendors.Note,
vendors.OnVendorList,
vendors.`Search Words`,
ROUND(AVG(`Communication_Rating`), 2) AS `Average Communication Rating`,
ROUND(AVG(`Promptness_Rating`), 2) AS `Average Promptness Rating`,
ROUND(AVG(`Performance_Rating`), 2) AS `Average Performance Rating`,
ROUND(AVG(`Equipment_Rating`), 2) AS `Average Equipment Rating`,
ROUND(AVG(`Safety_Rating`), 2) AS `Average Safety Rating`,
ROUND(AVG(`Documentation_Rating`), 2) AS `Average Documentation Rating`,
ROUND(AVG(`Cost_Rating`), 2) AS `Average Cost Rating`
FROM vendors
LEFT OUTER JOIN `vendor ratings` ON vendors.Vendor = `vendor ratings`.Vendor
GROUP BY vendor
这是我另一个视图的代码,它显示了每个供应商被评分的次数:
SELECT
Vendor,
COUNT(Vendor) AS `COUNT(Vendor)`
FROM `vendor ratings`
GROUP BY Vendor
ORDER BY Vendor
我尝试将以下行添加: COUNT(vendor As COUNT(Vendor)
, 到已经适用于其他所有内容但没有成功的代码中。我做错了什么?我只是结果看起来像这样......
供应商、平均评级、评级次数、电话号码等......
解决方案
Do the summarization before you join (i.e. use a subquery or "derived table")
SELECT
v.*
, vr.*
FROM
FROM vendors v
LEFT JOIN (
SELECT
vendors.Vendor
, COUNT(*) AS `COUNT(Vendor) `
, ROUND(((AVG(`Cost_Rating`)
+ AVG(`Documentation_Rating`)
+ AVG(`Safety_Rating`)
+ AVG(`Equipment_Rating`)
+ AVG(`Performance_Rating`)
+ AVG(`Promptness_Rating`)
+ AVG(`Communication_Rating`)) / 7.0), 2) AS `Overall Rating`
, ROUND(AVG(`Communication_Rating`), 2) AS `Average Communication Rating`
, ROUND(AVG(`Promptness_Rating`), 2) AS `Average Promptness Rating`
, ROUND(AVG(`Performance_Rating`), 2) AS `Average Performance Rating`
, ROUND(AVG(`Equipment_Rating`), 2) AS `Average Equipment Rating`
, ROUND(AVG(`Safety_Rating`), 2) AS `Average Safety Rating`
, ROUND(AVG(`Documentation_Rating`), 2) AS `Average Documentation Rating`
, ROUND(AVG(`Cost_Rating`), 2) AS `Average Cost Rating`
FROM `vendor ratings`
GROUP BY vendors.Vendor
) vr ON v.Vendor = vr.Vendor
Please note that MySQL has a very unfortunate way of allowing group by
clauses to have only a few of the non-aggregating columns that exist in the select clause. This is NOT good practice and it can lead to "unexpected results". Slowly MySQL is improving this area but you should be alert to this problem here's a start. Research only_full_group_by
- please note I do NOT recommend using
select v.*, vr.*
in your final code, list out the columns you need, I have done this only to focus attention on the subquery - but I DO recommend that you prefix ALL columns after the join by the relevant table prefix.
推荐阅读
- ruby - 在我的 MacOS 10.15.7 上升级 ruby 2.6 时遇到问题
- android-studio - jCenter & Bintray 正在关闭。怎么办?如果使用了第三方库,如何解决?
- cobol - 更新:如何修复 Cobol 中的排序和发布?
- django - Django 翻译:get_language
- java - 如何将单个 Java 文件转换为 MVC 设计?
- javascript - 禁用“关闭”按钮,直到模态视图 PDF 向下滚动
- amazon-web-services - terraform 不断强制使用默认参数对容器定义进行新资源/强制替换
- flutter - 如何在 Dart 中正确声明类的 List 类型属性?
- c# - 如何知道子进程的标准输出行需要覆盖前一行?
- java - Android:当两个触发器之间的时间很短时,使用来自服务的相同值触发活动(片段)中的回调