将计算的COUNT列从一个视图添加到另一视图

时间:2018-10-12 22:11:52

标签: mysql join count formula average

我有一个视图,该视图引用带有供应商联系信息的供应商表和一个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)这一行添加到已经对其他所有代码都有效但没有成功的代码中。我在做什么错呢?我只是将结果看起来像这样。

供应商,平均等级,评价的次数,电话号码等...

1 个答案:

答案 0 :(得分:1)

在加入之前进行汇总(即使用子查询或“派生表”)

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

请注意,MySQL有一种非常不幸的方式,允许group by子句仅包含select子句中的少数非聚合列。这不是一个好习惯,它可能导致“意外结果”。 MySQL慢慢地在改善这一领域,但是您应该对这个问题here's a start保持警惕。研究 only_full_group_by

  • 请注意,我不建议您在最终代码中使用select v.*, vr.*,列出所需的列,我这样做只是为了将注意力集中在子查询上
  • 但是我确实建议您在连接后的所有列前加上相关的表前缀。