MSAccess/SQL 根据当前 table.field 的总和查找 table 匹配字段
MSAccess/SQL lookup table for match field based on sum of current table.field
上周我一直在与这个问题作斗争,尝试了许多解决方案。我想 return table 中的唯一名称以及他们的积分总和以及基于该总和的当前舞蹈水平。最后,我想比较 returned 舞蹈水平与存储在客户 table 中的舞蹈水平,并仅显示两个舞蹈水平不同的记录(存储的舞蹈水平和计算的舞蹈水平根据当前积分总和
最终解决方案将是使用 ADODB 连接到 MSAccess DB (2013) 的网页。但对于初学者来说,只是希望它能在 MSAccess 中工作。
我有一个 MSAccess 数据库 (2013),其中 tables.
PointsAllocation
CustomerID Points
100 2
101 1
102 1
100 1
101 4
DanceLevel
DLevel Threshold
Beginner 2
Intermediate 4
Advanced 6
Customer
CID Firstname Dancelevel1
100 Bob Beginner
101 Mary Beginner
102 Jacqui Beginner
我想通过在第一个 table 中使用他们的积分总和来找到每个客户的当前 DLevel。我先有这个...
SELECT SUM(Points), CustomerID FROM PointsAllocation GROUP BY CustomerID
效果很好,每个客户给我总分。然后我可以 INNER JOIN 到客户 table 以获取此人的姓名。完美。
现在我想将 DanceLevel table 中的 DLevel 添加到使用 SUM 总计查找阈值且不超过该值的结果中,因此我得到以下结果:
(1) (2) (3) (4)
Bob 3 Beginner Intermediate
Mary 5 Beginner Advanced
Where...
(1) Customer.Firstname
(2) SUM(PointsAllocation.Points)
(3) Customer.Dancelevel1
(4) Dancelevel.DLevel
Jacqui 未显示,因为她的总分小于或等于 2,这表明她计算出的舞蹈水平为初学者,这已经与她在客户 table 中的 Dancelevel1 相匹配。
有什么想法吗?
您可以从客户 table 开始,因为您想要列出每个客户。然后将它与计算舞蹈水平和总分的子查询连接起来。最里面的子查询总计点数,然后加入有效的舞蹈级别并从舞蹈级别中选择最大阈值。然后在阈值上再次加入 DanceLevel table 以获取关卡描述。
Select Customer.Firstname,
CustomerDanceLevels.Points,
Customer.Dancelevel1,
Dancelevel.DLevel
from Customer
left join
(select CustomerID, Points, Min(Threshold) Threshold
from
(select CustomerID, sum(Points) Points
from PointsAllocation
group by CustomerID
) PointsTotal
left join DanceLevel
on PointsTotal.Points <= DanceLevel.Threshold
group by CustomerID, Points
) CustomerDanceLevels
on Customer.CID = CustomerDanceLevels.CustomerID
left join DanceLevel
on CustomerDanceLevels.Threshold = DanceLevel.Threshold
上周我一直在与这个问题作斗争,尝试了许多解决方案。我想 return table 中的唯一名称以及他们的积分总和以及基于该总和的当前舞蹈水平。最后,我想比较 returned 舞蹈水平与存储在客户 table 中的舞蹈水平,并仅显示两个舞蹈水平不同的记录(存储的舞蹈水平和计算的舞蹈水平根据当前积分总和
最终解决方案将是使用 ADODB 连接到 MSAccess DB (2013) 的网页。但对于初学者来说,只是希望它能在 MSAccess 中工作。
我有一个 MSAccess 数据库 (2013),其中 tables.
PointsAllocation
CustomerID Points
100 2
101 1
102 1
100 1
101 4
DanceLevel
DLevel Threshold
Beginner 2
Intermediate 4
Advanced 6
Customer
CID Firstname Dancelevel1
100 Bob Beginner
101 Mary Beginner
102 Jacqui Beginner
我想通过在第一个 table 中使用他们的积分总和来找到每个客户的当前 DLevel。我先有这个...
SELECT SUM(Points), CustomerID FROM PointsAllocation GROUP BY CustomerID
效果很好,每个客户给我总分。然后我可以 INNER JOIN 到客户 table 以获取此人的姓名。完美。
现在我想将 DanceLevel table 中的 DLevel 添加到使用 SUM 总计查找阈值且不超过该值的结果中,因此我得到以下结果:
(1) (2) (3) (4)
Bob 3 Beginner Intermediate
Mary 5 Beginner Advanced
Where...
(1) Customer.Firstname
(2) SUM(PointsAllocation.Points)
(3) Customer.Dancelevel1
(4) Dancelevel.DLevel
Jacqui 未显示,因为她的总分小于或等于 2,这表明她计算出的舞蹈水平为初学者,这已经与她在客户 table 中的 Dancelevel1 相匹配。
有什么想法吗?
您可以从客户 table 开始,因为您想要列出每个客户。然后将它与计算舞蹈水平和总分的子查询连接起来。最里面的子查询总计点数,然后加入有效的舞蹈级别并从舞蹈级别中选择最大阈值。然后在阈值上再次加入 DanceLevel table 以获取关卡描述。
Select Customer.Firstname,
CustomerDanceLevels.Points,
Customer.Dancelevel1,
Dancelevel.DLevel
from Customer
left join
(select CustomerID, Points, Min(Threshold) Threshold
from
(select CustomerID, sum(Points) Points
from PointsAllocation
group by CustomerID
) PointsTotal
left join DanceLevel
on PointsTotal.Points <= DanceLevel.Threshold
group by CustomerID, Points
) CustomerDanceLevels
on Customer.CID = CustomerDanceLevels.CustomerID
left join DanceLevel
on CustomerDanceLevels.Threshold = DanceLevel.Threshold