【问题标题】:SQL Math function in 3 joined Tables3 个连接表中的 SQL 数学函数
【发布时间】:2020-09-18 08:28:25
【问题描述】:

我对 SQL 查询有疑问

我有类似的表

表 1:

id_table1    ean    date_from      date_to     type     region   town_id     consumption 
  1          1234   2020-01-01    2021-01-01   blue      big       100        20000
  2          5678   2020-01-01    2021-01-01   blue      big2      200        80000
  2          6845   2020-01-01    2021-01-01   white    small      300        10000

表 2:

id_table2        day          type_blue_big_coefficient   type_blue_big2_coefficient  type_white_coefficient
  1          2020-01-01                 0.011120                0.32652                     0.054165
  2          2020-01-01                 0.023265                0.12543                     0.054165

表 3:

id_table3        day             town_id_100_coefficent  town_id_200_coefficent 
  1          2020-01-01                 0.011120             0.98556
  2          2020-01-01                 0.023265             0.58467

我需要像这样显示输出:

EAN         Day           consumption  
1234     2020-01-01          222.4
5678     2020-01-01         26 121.6
6845     2020-01-01         541.65

输出消耗是通过将表 2 中的系数与表 3 中的系数相乘,然后乘以系数 * 表 1 消耗来计算的。

如何正确连接这 3 个表? 如果我将表 3 加入表 1 date_from 和 date_to 我会得到不好的结果(比可能大得多)

【问题讨论】:

  • 表之间是什么关系?
  • 请使用架构和示例数据创建fiddle
  • 将表 2 和 3 标准化为 (id, day, attribute, coeff)。
  • table1 中的date_from、date_to 的值是否总是相等的?如果不是,那么必须取表 2 和表 3 中的哪些行?
  • table1 date_from 和 date_to 表示连接天数。 table2 和 table3 是表 2 和表 3 中这些天的系数,取表 1 date_from 到 date_to 上连接的系数行

标签: mysql sql mariadb


【解决方案1】:

要在查询中进行计算,您可以使用 SUM 例如:

SELECT a.ean AS EAN, a.day_from AS Day, SUM(a.consumption*(b.coefficient2*c.coefficient3)) AS consumption, 
FROM table1 a
LEFT JOIN table2 b ON a.town_id = b.town_id
LEFT JOIN table3 c ON b.town_id = c.town_id

您需要在每个表中都有一个相对列才能加入它们,并且根据我的经验,使用 id 是最好的。

【讨论】:

    【解决方案2】:

    根据table1 的type 和region 列的值,您似乎需要条件(CASE..WHEN)语句,并且不需要第三个表(table3)。考虑使用这个查询:

    SELECT t1.ean, t1.date_from AS day, 
           CASE 
           WHEN type = 'blue' 
                THEN 
                CASE 
                WHEN region = 'big' THEN
                     t2.type_blue_big_coefficient * t1.consumption
                WHEN region = 'big2' THEN  
                     t2.type_blue_big2_coefficient * t1.consumption
                END
           WHEN type = 'white' 
                THEN  
                     t2.type_white_coefficient * t1.consumption
            END AS consumption  
      FROM table1 t1
      JOIN table2 t2
        ON t2.id_table2 = t1.id_table1
     WHERE t2.day BETWEEN t1.date_from AND t1.date_to
    

    Demo

    【讨论】:

    • 需要表 3,因为表 3 中的 coe 对于该地区的实际消费很重要
    • 那么我们不需要 table2 @MilanLidmila 。事实上,我们能够在没有 table3 的情况下获得 3 行中的 2 行的预期结果 ...
    • no.. 在表 1 中是有关 ean - 消耗量、类型、区域的信息 在表 2 中,我们有 - coe 用于消耗计数,在表 3 中,我们有 - coe 用于消除消耗以获得实际消耗.所以需要所有表中的数据..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多