【问题标题】:SQL query for Ms Access database. To query access database from similar id groupMs Access 数据库的 SQL 查询。从相似 id 组查询访问数据库
【发布时间】:2021-04-01 06:24:29
【问题描述】:

下面是我在ms access中的表结构,

ID Stage_Name Time
1 A 07:00
1 B 08:00
1 C 09:00
2 A 08:00
2 B 09:00
3 A 07:00

输出应检索尚未通过阶段 B 的所有记录。输出将是,

ID Stage_Name Time
3 A 07:00

请帮帮我????

【问题讨论】:

  • Stackoverflow 并不是一个要求人们为您完成工作的地方。相反,请尝试展示您已经尝试过的内容、遇到的事情以及遇到的问题。

标签: sql database ms-access subquery ms-access-2010


【解决方案1】:

您可以使用not exists 和相关子查询进行过滤。这样做的目的是确保在阶段 B 中没有相同 id 的更早记录:

select t.*
from mytable as t
where not exists (
    select 1 
    from mytable as t1 
    where t1.id = t.id and t1.stage_name = 'B' and t1.time < t.time
)

这符合您对问题的描述(尚未通过阶段 B 的记录),但不符合您想要的结果。

如果您想排除 id 为任何 B 的记录,甚至稍后,您可以从子查询中删除条件 t1.time &lt; t.time

【讨论】:

    【解决方案2】:

    你可以使用not exists:

    select t.*
    from t
    where not exists (select 1
                      from t t2
                      where t2.id = t.id and
                             t2.stage_name = 'B'
                     );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多