MS Access SQL – 如何计算一个 table 的数字在另一个 table 的字符串中出现的次数
MS Access SQL – How can I count the occurrence of a number from one table in strings of another table
在 MS Access 365 中,我有 2 个 table,我想计算从第一个 table 开始的年份在第二个 table 的字符串的一部分中的出现次数,纯粹用SQL(到目前为止我用的是VBA,但我想简化)。
第一个 table (tDistinctYears) 包含我们的一位会员支付的所有年份:
ID
付款年份
1
2015
2
2016
3
2017
3
2018
4
2019
5
2020
6
2021
7
2022
第二个 table (tPayments) 包含来自会员的所有付款,其中一列包含会员编号,另一列包含付款年份。有时会员会支付一年的费用,有时会支付几年的费用。 table 因此看起来像这样:
会员编号
年付款
11
2016
11
2017
11
2018
26
2017
26
2018;2019
26
2020;2021;2022
38
2016
38
2017
38
2018;2019;2020;2021
我想要一个查询,告诉我有多少会员在哪一年付款:
付款年份
计数
2015
0
2016
2
2017
3
2018
3
2019
2
2020
2
2021
2
我使用了以下 SQL 查询,这是我在 Whosebug 上使用各种答案发现的:
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tDistinctYears.PaymentYear like "*" & tPayments.YearPayment & "*"
WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <= YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
但我得到的是:
付款年份
计数
2015
0
2016
2
2017
3
2018
1
2019
0
2020
0
2021
0
上面的查询似乎没有在 JOIN ON 部分使用“like”表达式。
有人可以帮我吗?
我认为你已经接近了,只是改变列,其中条件 tPayments.YearPayment
应该是第一个,tDistinctYears.PaymentYear
应该像运算符一样在里面。
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tPayments.YearPayment like "*" &
tDistinctYears.PaymentYear
& "*" WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <=
YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
在 MS Access 365 中,我有 2 个 table,我想计算从第一个 table 开始的年份在第二个 table 的字符串的一部分中的出现次数,纯粹用SQL(到目前为止我用的是VBA,但我想简化)。
第一个 table (tDistinctYears) 包含我们的一位会员支付的所有年份:
ID | 付款年份 |
---|---|
1 | 2015 |
2 | 2016 |
3 | 2017 |
3 | 2018 |
4 | 2019 |
5 | 2020 |
6 | 2021 |
7 | 2022 |
第二个 table (tPayments) 包含来自会员的所有付款,其中一列包含会员编号,另一列包含付款年份。有时会员会支付一年的费用,有时会支付几年的费用。 table 因此看起来像这样:
会员编号 | 年付款 |
---|---|
11 | 2016 |
11 | 2017 |
11 | 2018 |
26 | 2017 |
26 | 2018;2019 |
26 | 2020;2021;2022 |
38 | 2016 |
38 | 2017 |
38 | 2018;2019;2020;2021 |
我想要一个查询,告诉我有多少会员在哪一年付款:
付款年份 | 计数 |
---|---|
2015 | 0 |
2016 | 2 |
2017 | 3 |
2018 | 3 |
2019 | 2 |
2020 | 2 |
2021 | 2 |
我使用了以下 SQL 查询,这是我在 Whosebug 上使用各种答案发现的:
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tDistinctYears.PaymentYear like "*" & tPayments.YearPayment & "*"
WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <= YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
但我得到的是:
付款年份 | 计数 |
---|---|
2015 | 0 |
2016 | 2 |
2017 | 3 |
2018 | 1 |
2019 | 0 |
2020 | 0 |
2021 | 0 |
上面的查询似乎没有在 JOIN ON 部分使用“like”表达式。
有人可以帮我吗?
我认为你已经接近了,只是改变列,其中条件 tPayments.YearPayment
应该是第一个,tDistinctYears.PaymentYear
应该像运算符一样在里面。
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tPayments.YearPayment like "*" &
tDistinctYears.PaymentYear
& "*" WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <=
YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;