【问题标题】:How to calculate percentage difference - mysql?如何计算百分比差异 - mysql?
【发布时间】:2021-07-20 13:04:13
【问题描述】:

我正在使用带有 2 个表的房地产数据库:销售(突变)和房屋(bien)。 销售表具有金额 (ValeurFonciere) 属性,而房屋表具有房地产类型 (TypeLoc;即公寓或房屋)、房间数 (NoPP) 和平方米 (SurCar1) 属性。

我现在需要计算 2 室公寓和 3 室公寓之间价格/平方米的百分比差异。

我知道如何获得任何一种公寓的平均价格/平方米;

select NoPP, avg(ValeurFonciere/SurCar1)
FROM mutation, bien
where mutation.Bien_ID = bien.Bien_ID
and NoPP = 2
and TypeLoc = "Appartement";

但我不知道如何使用百分比差异公式将 2 结合起来 ((A-B)/((A+B)/2))*100:A 为 NoPP=2,B 为 NoPP=3。

如有任何帮助,我们将不胜感激。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    您可以为每个条件使用子查询创建派生表

    SELECT ((A-B)/((A+B)/2))*100
    FROM
    (
        select avg(ValeurFonciere/SurCar1) as A, 1 as key
        FROM mutation, bien
        where mutation.Bien_ID = bien.Bien_ID
        and NoPP = 2
        and TypeLoc = "Appartement"
    ) two
    JOIN
    (
        select avg(ValeurFonciere/SurCar1) as B, 1 as key
        FROM mutation, bien
        where mutation.Bien_ID = bien.Bien_ID
        and NoPP = 3
        and TypeLoc = "Appartement"
    ) three
    ON two.key = three.key
    

    【讨论】:

      【解决方案2】:

      选择 (100/(ValeurFonciere/SurCar1) - (选择 100/(ValeurFonciere/SurCar1))

      FROM 突变,bien

      mutation.Bien_ID = bien.Bien_ID

      和 NoPP = 2

      和 TypeLoc = "Appartement" ) ) 作为'价格'

      FROM 突变,bien

      mutation.Bien_ID = bien.Bien_ID

      和 NoPP = 3

      和 TypeLoc = "公寓"

      【讨论】:

        猜你喜欢
        • 2020-11-27
        • 2021-06-17
        • 2021-01-05
        • 2015-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多