sql - SQL Get all records from table 1 based on criteria 2 if part of result of Query 1 Criteria 1 (Access)
问题描述
I am trying to do an operation on one table where I run an initial Query(Query1) which pulls data from Table 1 based on a condition. This Subquery is then used against Table 1 to get all records in Query 1 plus all Records in Table 1 which match a second criteria.
This is because I have many records per individual in Table 1 and I need all of them as long as one record matches the criteria from Query 1. I have tried many different queries to no avail. The closest I have thus far is:
SELECT A.*
FROM
Table1 A
INNER JOIN Query1 B on A.[ID] = B.[ID]
And Query1 looks like:
SELECT * FROM Table1
WHERE [Last Updated Date] >=#4/1/2018 4:00:00 AM#
And [Last Updated Date] <=#5/1/2018 4:00:00 AM#
The end result I want is something like
Table 1
ID Last Updated
1 1-Jan-18
2 6-May-18
2 3-Jan-18
2 5-Apr-18
3 6-Apr-18
4 5-May-18
Query Result:
ID Last Updated
2 6-May-18
2 3-Jan-18
2 5-Apr-18
3 6-Apr-18
The query I wrote gives errors and I end up with over 3x the total records so clearly something is going wrong. Any help is appreciated.
解决方案
您没有指定您尝试执行的操作,但这将为您提供Table1
符合您条件的所有记录。
SELECT * FROM Table1 WHERE ID IN (
SELECT ID FROM Table1
WHERE [Last Updated Date] >=#4/1/2018 4:00:00 AM#
And [Last Updated Date] <=#5/1/2018 4:00:00 AM# )
推荐阅读
- sql - 如何修复javafx中的“表上没有唯一或主键约束”sql错误?
- windows - 登录到 Web 服务器身份验证用户不是管理员
- mysql - 创建视图,错误代码:1052。字段中的“id”列不明确
- android - 是否可以远程更新在 Kiosk 模式下运行的 Android 应用程序?
- python - Python:使用事件循环从脚本启动时外部程序的缓慢/延迟执行[Windows]
- javascript - 使用概率选择数组值
- swift - 如何使用 dispatchQueues 创建引用循环?
- sql - 使用 Oracle SQL 窗口函数以部分数量满足多个需求的 SQL 查询
- angular - 在 app.module.ts 中初始化 firestore 时如何禁用 firestore 持久性?
- c++ - 表达式 std::enable_if 中的 type* 是什么