【问题标题】:使用聚合函数 MYSQL 查找百分比
【发布时间】:2022-01-12 06:41:42
【问题描述】:

所以我有 3 张桌子 propertiesproperty_plotsorders。每个属性将有许多地块,每个地块的大小都可以是 no_sqyds。 现在用户可以购买数量为no_sqyds的地块,我存储在orders表中。

properties

property_plots

orders

所以我的问题是如何找到特定地块的购买百分比?另外,我怎样才能找到使用相同的整个物业的购买百分比?

到目前为止,我尝试使用这个粗略查询在绘图级别找到百分比

((SELECT sum(o.no_sqyds) FROM orders as o 
WHERE o.plot_id = pp.id)*100)/pp.no_sqyds FROM property_plots as pp

基于表 orders 表,我可以获得每个图的百分比,但我也在通过结合以下数据来查看属性级别。 (我必须得到所有情节百分比的平均值才能在属性级别找到?)

plot_id 1 = 100% purchase

plot_id 2 = 66.67% purchase

plot_id 3 = 50%

示例数据库 - https://pastebin.com/RYJwwRqJ

【问题讨论】:

  • 将您的表作为 CREATE TABLE 脚本提供。提供一些示例数据作为 INSERT INTO 脚本和该数据所需的输出。
  • @Akina 我已经更新了这个问题。请看一下还添加了基于表中虚拟数据的预期结果。谢谢
  • 屏幕截图没有意义 - 它们不允许复制 SQL 代码并重现您的结构和数据。
  • 好的,我已经在问题中添加了示例数据库链接。
  • 下次提供您的示例数据作为在线小提琴(就像我回答中的第一个代码块)。

标签: mysql sql aggregate-functions relation


【解决方案1】:

加入它们并将它们分组并计算。

SELECT 
  property_id
, prop.name AS property_name
, (SUM(order_no_sqyds)/SUM(plot_no_sqyds))*100 AS percentage
, SUM(plot_no_sqyds) AS plot_no_sqyds
, SUM(order_no_sqyds) AS order_no_sqyds
, COUNT(plot_id) AS total_plots
, SUM(total_orders) AS total_orders
FROM
(
    SELECT 
      plot.property_id
    , plot.id AS plot_id
    , plot.no_sqyds AS plot_no_sqyds
    , SUM(ordr.no_sqyds) AS order_no_sqyds
    , COUNT(DISTINCT ordr.id) AS total_orders
    FROM property_plots AS plot
    LEFT JOIN orders AS ordr
      ON ordr.plot_id = plot.id
     AND ordr.property_id = plot.property_id
    GROUP BY 
      plot.property_id
    , plot.id
    , plot.no_sqyds
) q
INNER JOIN properties AS prop
   ON prop.id = q.property_id
GROUP BY property_id, prop.name
ORDER BY property_id
property_id property_name percentage plot_no_sqyds order_no_sqyds total_plots total_orders
1 Lake View Park 66.6667 225 150 3 4

dbfiddle here

上的演示

另一种计算方法是加入订单聚合。

SELECT 
  plot.property_id
, prop.name AS property_name
, (SUM(ordr.no_sqyds)/SUM(plot.no_sqyds))*100 AS percentage
, SUM(plot.no_sqyds) AS plot_no_sqyds
, SUM(ordr.no_sqyds) AS order_no_sqyds
, COUNT(DISTINCT plot.id) AS total_plots
, SUM(total_orders) AS total_orders
FROM property_plots AS plot
INNER JOIN properties AS prop
   ON prop.id = plot.property_id
LEFT JOIN (
   SELECT plot_id, property_id
   , SUM(no_sqyds) AS no_sqyds
   , COUNT(DISTINCT id) AS total_orders
   FROM orders
   GROUP BY plot_id, property_id
) AS ordr
  ON ordr.plot_id = plot.id
 AND ordr.property_id = plot.property_id
GROUP BY 
  plot.property_id
, prop.name

仅绘图

SELECT 
  plot.property_id
, plot.id AS plot_id
, (SUM(ordr.no_sqyds)/plot.no_sqyds)*100 AS percentage
, plot.no_sqyds AS plot_no_sqyds
, SUM(ordr.no_sqyds) AS order_no_sqyds
, COUNT(DISTINCT ordr.id) AS total_orders
FROM property_plots AS plot
LEFT JOIN orders AS ordr
  ON ordr.plot_id = plot.id
 AND ordr.property_id = plot.property_id
GROUP BY 
  plot.property_id
, plot.id
, plot.no_sqyds

【讨论】:

  • 感谢您的解决方案,但查询似乎没有按预期返回。它分别为属性 id 1 和百分比 100、150 和 200 返回 3 行
  • 现在可以了吗?
  • 它导致每个图的百分比这是正确的,但我需要整个属性的百分比,即 property_id。它为每个地块提供 100,66,50 但如果我想要总财产百分比怎么办。我猜是 (100,66,50)/3=72 不确定
  • 需要一个额外的组 by
  • 是的,我只是想将它与我的代码集成,然后看看它是如何进行的。但似乎非常适合我的问题。感谢您的宝贵时间
【解决方案2】:
SELECT orders.plot_id, 
       ROUND(100*SUM(orders.no_sqyds)/MAX(property_plots.no_sqyds)) purchase_percent
FROM orders
JOIN property_plots ON orders.plot_id = property_plots.id
GROUP BY orders.plot_id

https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=4c2bad2ff341ba94b3df2f278d1d7778

【讨论】:

  • 谢谢,如何从这 3 个结果中获取 property_id 1 的百分比?
  • @AbhiBurk 这是另一个分组级别。还有另一个任务。创建另一个问题,提供示例数据作为该示例数据的 2-3 个不同属性和所需输出。
猜你喜欢
  • 2021-10-29
  • 2022-10-05
  • 2013-02-17
  • 2012-01-20
  • 1970-01-01
  • 2021-11-22
  • 2014-05-27
  • 2021-03-24
相关资源
最近更新 更多