sql - 如何在查询多个表的多列时从单个表的单列使用MAX

标签 sql postgresql date select greatest-n-per-group

所以我收到了我公司某人的一些数据请求。此数据请求需要对多个表中的多个列进行 SQL 查询,但只能是其中一个表中某一列的最新事件。这是踢球者......每个表都有一个与另一个表共享的列。所以要得到这份“报告”,我必须一 block 一 block 地做。

在我意识到我只需要对其中一个表进行最新更新之前,这是我的初始查询的样子:

SELECT a.description  AS "Description", 
       a.pricing      AS "Price", 
       b.id           AS "ID", 
       c.descriptionb AS "DescriptionB", 
       c.date         AS "date", 
       d.descriptionc AS "DescriptionC" 
FROM   database.table1 a, 
       database.table2 b, 
       database.table3 c, 
       database.table4 d 
WHERE  a.description = b.descriptive_info 
       AND b.id = c.comp_id 
       AND c.descriptionb = d.long_description
       AND d.id_for_a = a.id
       AND a.company IN ( '000', '001', '002', '003', '004' ) 
       AND b.expdate >= Now()

我意识到上面的“c.date”只需要显示每个唯一 ID/DescriptionC 的最新日期。

这是初始查询的示例结果:

Description|Price   |ID       |DescriptionB   |date     |DescriptionC
---------------------------------------------------------------------
Computer   |300     |554      |5% Off         |3/2/2010 |Includes CPU
Computer   |300     |554      |5% Off         |3/2/2010 |Includes DOG
Computer   |300     |554      |5% Off         |3/2/2010 |Includes CAT 
Computer   |300     |554      |9% Off         |4/3/2011 |Includes CPU
Computer   |300     |554      |9% Off         |4/3/2011 |Includes DOG
Computer   |300     |554      |9% Off         |4/3/2011 |Includes CAT 
Computer   |300     |554      |7% Off         |9/1/2019 |Includes CPU
Computer   |300     |554      |7% Off         |9/1/2019 |Includes DOG
Computer   |300     |554      |7% Off         |9/1/2019 |Includes CAT 
Printer    |75      |801      |3% Off         |6/3/2012 |Includes DOS
Printer    |75      |801      |3% Off         |6/3/2012 |Includes PIG
Printer    |75      |801      |3% Off         |6/3/2012 |Includes RAT
Printer    |75      |801      |9% Off         |8/3/2013 |Includes DOS
Printer    |75      |801      |9% Off         |8/3/2013 |Includes PIG
Printer    |75      |801      |9% Off         |8/3/2013 |Includes RAT
Printer    |75      |801      |1% Off         |1/3/2019 |Includes DOS
Printer    |75      |801      |1% Off         |1/3/2019 |Includes PIG
Printer    |75      |801      |1% Off         |1/3/2019 |Includes RAT

下面是 Laurenz 查询的结果:

Description|Price   |ID       |DescriptionB   |date     |DescriptionC
---------------------------------------------------------------------
Computer   |300     |554      |7% Off         |9/1/2019 |Includes CAT

...关闭但尚未完成。

期望的结果:

Description|Price   |ID       |DescriptionB   |date     |DescriptionC
---------------------------------------------------------------------
Computer   |300     |554      |7% Off         |9/1/2019 |Includes CPU
Computer   |300     |554      |7% Off         |9/1/2019 |Includes DOG
Computer   |300     |554      |7% Off         |9/1/2019 |Includes CAT 
Printer    |75      |801      |1% Off         |1/3/2019 |Includes DOS
Printer    |75      |801      |1% Off         |1/3/2019 |Includes PIG
Printer    |75      |801      |1% Off         |1/3/2019 |Includes RAT

如您所见,“同一产品”有多个日期,具有不同描述的重复产品等。我基本上只想要每个唯一 ID/DescriptionC 具有最新“日期”的行。希望这比我原来的帖子更容易理解。

顺便说一句,这些是简化的示例,因为我不想给我的公司带来麻烦,但查询和概念是相同的。如果您可以想象多个产品多次包含每个产品的多个实例,您就可以想象数据集会有多大。我只关心每个唯一 ID/DescriptionC 的最新实例。

最佳答案

我知道,从当前查询的结果中,您只想选择在 c.date 上具有最大值的结果。

一种解决方案是将现有查询转换为子查询,并使用 ROW_NUMBER()c.date 降序排列记录。然后,外部查询可以只过滤排名最高的记录。

查询:

SELECT *
FROM (
    SELECT 
        a.description  AS "Description", 
        a.pricing      AS "Price", 
        b.string       AS "String", 
        c.description  AS "Description", 
        c.date         AS "date", 
        d.descriptionb AS "DescriptionB",
        ROW_NUMBER() OVER (ORDER BY c.date DESC) AS rn
    FROM
       database.table1 a 
       INNER JOIN database.table2 b ON a.id = b.table1_id 
       INNER JOIN database.table3 c ON b.element = c.table2_element AND b.expdate >= Now()
       INNER JOIN database.table4 d ON c.value = d.table3_value 
    WHERE 
        a.company IN ( '000', '001', '002', '003', '004' ) 
) x WHERE rn = 1;

附言:

  • 总是喜欢显式连接,而不是老式的隐式连接;我相应地更改了初始查询

  • 如果您需要分区的最大日期(如果不查看示例数据则无法明显判断),那么您只需将 PARTITION BY 子句添加到 ROW_NUMBER () 函数。

关于sql - 如何在查询多个表的多列时从单个表的单列使用MAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861983/

相关文章:

mysql - 如何将内部查询返回的值传递给外部查询

sql - 树结构和递归

sql - 创建自定义事件时间表。我应该使用 "LIKE"吗?

sql - 如何使用批处理脚本将多个 sql 文件导入到 postgreSQL 中?

c# - NodaTime,我们应该使用 LocalDateTime 作为预订时间吗?

php - 在mysql中从午夜减去时间

mysql - 如何从我的 MySQL 表中删除相似的行?

php - 我需要做什么来更正这条 SQL 语句?

sql - 如何在 SQL 和关系代数中无论列顺序如何只列出每对元组一次?

javascript - JavaScript 将日期时间四舍五入到最后 10 分钟