【问题标题】:Query for last values from column1 show all rows查询 column1 中的最后一个值显示所有行
【发布时间】:2021-09-20 21:21:17
【问题描述】:

我有一个示例表:

Column1 | Column2 | Column3
--------+---------+--------
1          Test1   1.1.2021
2          Test1   1.1.2020
1          Test2   2.1.2021
3          Test1   4.1.2021
2          Test2   5.1.2020
1          Test3   6.1.2021

我想得到:

Column1 | Column2 | Column3
--------+---------+--------
2          Test2    5.1.2020
3          Test1    4.1.2021
1          Test3    6.1.2021

有没有办法使用查询语言?

如:

SELECT * 
FROM TABLE1, 
ORDER BY Column4 ASC, DISTINCT Column1.

【问题讨论】:

    标签: sql vba ms-access


    【解决方案1】:

    如果我理解正确,您需要最新的行——基于column3 的column1 中的每个值。如果是这样,您可以使用相关子查询:

    select t.*
    from table1 as t
    where t.column3 = (select max(t2.column3)
                       from table1 as t2
                       where t2.column1 = t.column1
                      );
    

    【讨论】:

    • 谢谢 Gordon,它运行良好!它究竟是如何工作的?您是否创建了两个子表 t1 和 t2?可悲的是,我没有足够的声誉来支持您的评论......
    • @DamnSpaceship 。 . .这种类型的子查询称为相关子查询。网络上有许多资源可供您了解构造。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多