【问题标题】:How to join two SQL tables by extracting maximum numbers from one then into another?如何通过从一个中提取最大数字然后到另一个来连接两个 SQL 表?
【发布时间】:2020-08-20 09:44:07
【问题描述】:

正如其他人评论的那样,我现在要添加一些代码:

导入的表格

  1. table3 Case No. is the primary key. Each report date shows one patient. Depending on if the patient is import or local, the cumulative column increases. You can see some days there are no cases so the date like 25/01/2020 is skipped

  2. 表2 Report date has no duplicate.

现在,我想加入表格。这里的示例结果: enter image description here

每个日期的最大累计加入到新表中。因此,虽然 table3 的 26/01/2020 显示从 6、7 到 8 的增加,但我只想要那里的最高累积数。

感谢您让我知道如何改进我之前的查询。您的意见对我帮助很大。

我尝试过用 Gordon Linoff 替换实际名称(我最初省略了,因为我认为它们模棱两可)。

他的代码如下(我已经投票了):

SELECT t3.`Report date`,
        max(max(t3.cumulative_local)) over (order by t3.`Report date`),
        max(max(t3.cumulative_import)) over (order by t3.`Report date`)
from table3 t3 left join
    table2 t2
    using (`Report date`)
group by t2.`Report date`;

但我遇到了一个错误

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'new.t3.Report date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

无论如何,我现在正在尝试。两个答案都有帮助。如果您知道如何修复 1055,请告诉我,或者您是否可以提出其他解决方案。谢谢

【问题讨论】:

  • 提供一个小提琴(或 CREATE TABLE + INSERT INTO 脚本),其中包含示例数据和所需结果以及解释。
  • 以表格格式提供样本数据和预期结果,这将有助于其他人快速做出反应。
  • 从 3 月到 8 月的每个日期,没有重复或缺少日期 显示保证这一点的约束。 这里的每个日期都被table1覆盖 一样。 累积 A 和累积 B 的问题是它们永远不会同时增加。因此,当您在表格中向下时(按 id 顺序)一次只更改一个,并且一次只增加一个。 也是如此。
  • 你使用的是 MySQL 还是 Postgresql?

标签: sql logic


【解决方案1】:

我认为您只需要聚合和窗口函数:

select t1.date,
       max(max(cumulativea)) over (order by t1.date),
       max(max(cumulativeb)) over (order by t1.date)
from table1 t1 left join
     table2 t2
     on t1.date = t2.date
group by t1.date;

这会返回截至每个日期的两列的最大值,我认为这就是您要描述的内容。

【讨论】:

    【解决方案2】:

    我不明白为什么 table1 上有 cumulA 和 cumulB。我想这将是每天存储 Max cumulA 和 cumulB。

    您必须先自连接 table2 才能找到每个日期的最大值(带有 GROUP BY 日期):

    SELECT t2.id, t2.date, cA
    FROM t2
        JOIN (
            SELECT id, MAX(cumulA) AS cA, date AS d2
            FROM t2
            GROUP BY d2
        ) AS td
            ON t2.id=td.id
            AND t2.date=d2
    ORDER BY t2.date
    

    之后,你在自加入表2的结果上加入左表1以拥有每一天。

    SELECT * FROM `t1` LEFT JOIN t2 ON t1.date = t2.date ORDER BY t1.date
    

    这是两个路口的融合:

    SELECT * FROM `t1` LEFT JOIN (
    
      SELECT t2.id, t2.date, cA
      FROM t2
          JOIN (
              SELECT id, MAX(cumulA) AS cA, date AS d2
              FROM t2
              GROUP BY d2
          ) AS td
              ON t2.id=td.id
              AND t2.date=d2
      ORDER BY t2.date
      ) AS tt
    
    ON t1.date = tt.date ORDER BY t1.date
    

    你对 cumulB 做同样的事情。 在(我想)之后,您将结果插入到 table1 中。

    希望我回答了你的问题。

    很好的延续。

    _泰迪_

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2013-09-26
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多