MS Access连接2个表,精确匹配和范围匹配。

huangapple go评论87阅读模式
英文:

MS Access Join 2 tables with exact match and also range match

问题

我想要在MS Access中连接两个表格,一个是精确值,一个是值范围。我不知道如何继续操作。

这是我的结果表格

MS Access连接2个表,精确匹配和范围匹配。

我想要与限制表格连接

MS Access连接2个表,精确匹配和范围匹配。

这里的InfoEntity在两个表格中匹配,LIMIT应该是RefDateFrom_DateTo_Date范围内的地方。

所以输出应该如下所示
MS Access连接2个表,精确匹配和范围匹配。

限制表格

Info Entity From_Date To_Date Limit
M1 PARENT 31/03/2023 01/01/2050 384%
M1 PARENT 28/02/2022 31/03/2023 350%
M1 PARENT 28/02/2021 27/02/2022 275%
M1 PARENT 01/01/2020 04/03/2021 235%
M1 PARENT 01/12/2019 31/12/2019 209%
M2 CHILD 21/10/2020 01/01/2050 115%
M2 CHILD 01/12/2019 20/10/2020 140%
M2 PARENT 21/10/2022 01/01/2050 135%
M2 PARENT 01/12/2019 21/10/2022 140%
M2 PARENT 05/02/2019 30/11/2019 138%

结果表格

Info Entity RefDate Value
M2 PARENT 31/07/2023 168.9%
M2 CHILD 31/07/2023 482.01%
M1 PARENT 31/07/2023 278.53%
M1 CHILD 31/07/2023 482.01%
M2 PARENT 10/06/2023 164.35%
M2 CHILD 10/06/2023 150.17%
M1 PARENT 10/06/2023 344.79%
M1 CHILD 10/06/2023 213.94%

查询输出

Info Entity RefDate Value Limit
M2 PARENT 31/07/2023 168.9% 135%
M2 CHILD 31/07/2023 482.01% 115%
M1 PARENT 31/07/2023 278.53% 384%
M1 CHILD 31/07/2023 482.01%
M2 PARENT 10/06/2023 164.35% 140%
M2 CHILD 10/06/2023 150.17% 115%
M1 PARENT 10/06/2023 344.79% 350%
M1 CHILD 10/06/2023 213.94%
英文:

I want to join to tables in MS access with both exact values and range of values. I am at a loss on how to proceed

This is my results table

MS Access连接2个表,精确匹配和范围匹配。

I want to join with limits table

MS Access连接2个表,精确匹配和范围匹配。

here Info and Entity match in both the tables and LIMIT should be where RefDate is within range From_Date and To_Date

so the output would look like
MS Access连接2个表,精确匹配和范围匹配。

Limits_Table

Info Entity From_Date To_Date Limit
M1 PARENT 31/03/2023 01/01/2050 384%
M1 PARENT 28/02/2022 31/03/2023 350%
M1 PARENT 28/02/2021 27/02/2022 275%
M1 PARENT 01/01/2020 04/03/2021 235%
M1 PARENT 01/12/2019 31/12/2019 209%
M2 CHILD 21/10/2020 01/01/2050 115%
M2 CHILD 01/12/2019 20/10/2020 140%
M2 PARENT 21/10/2022 01/01/2050 135%
M2 PARENT 01/12/2019 21/10/2022 140%
M2 PARENT 05/02/2019 30/11/2019 138%

Results_Table

Info Entity RefDate Value
M2 PARENT 31/07/2023 168.9%
M2 CHILD 31/07/2023 482.01%
M1 PARENT 31/07/2023 278.53%
M1 CHILD 31/07/2023 482.01%
M2 PARENT 10/06/2023 164.35%
M2 CHILD 10/06/2023 150.17%
M1 PARENT 10/06/2023 344.79%
M1 CHILD 10/06/2023 213.94%

Query_Output

Info Entity RefDate Value Limit
M2 PARENT 31/07/2023 168.9% 135%
M2 CHILD 31/07/2023 482.01% 115%
M1 PARENT 31/07/2023 278.53% 384%
M1 CHILD 31/07/2023 482.01%
M2 PARENT 10/06/2023 164.35% 140%
M2 CHILD 10/06/2023 150.17% 115%
M1 PARENT 10/06/2023 344.79% 350%
M1 CHILD 10/06/2023 213.94%

答案1

得分: 1

这个SQL语句应该可以工作,但如果结果与限制重叠的日期在同一天,它会排除重复的值。

SELECT	RT.Info,
    	RT.Entity,
    	RefDate,
    	ActualValue,
    	Limit
FROM	Results_Table RT LEFT JOIN Limits_Table LT ON RT.Info = LT.Info AND  
    													  RT.Entity = LT.Entity AND  
    													  RT.RefDate >= LT.From_Date AND
    													  RT.RefDate <= LT.To_Date  

MS Access连接2个表,精确匹配和范围匹配。

英文:

This SQL should work, but will throw out duplicate values if a Result is on the same day that the Limits overlap.

SELECT	RT.Info,
		RT.Entity,
		RefDate,
		ActualValue,
		Limit
FROM	Results_Table RT LEFT JOIN Limits_Table LT ON RT.Info = LT.Info AND  
													  RT.Entity = LT.Entity AND  
													  RT.RefDate &gt;= LT.From_Date AND
													  RT.RefDate &lt;= LT.To_Date  

MS Access连接2个表,精确匹配和范围匹配。

huangapple
  • 本文由 发表于 2023年8月10日 21:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定