samedi 9 mai 2015

MSAccess/SQL lookup table for match field based on sum of current table.field

I've been battling this for the last week with many attempted solutions. I want to return the unique names in table with the sum of their points and their current dance level based on that sum. Ultimately I want compare the returned dance level with what is stored in the customer table against the customer and show only the records where the two dance levels are different (the stored dance level and the calculated dance level based on the current sum of the points.

The final solution will be a web page using ADODB connection to MSAccess DB (2013). But for starters just want it to work in MSAccess.

I have a MSAccess DB (2013) with the following 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

I want to find the current DLevel for each customer by using the SUM of their Points in the first table. I have this first...

SELECT SUM(Points), CustomerID  FROM PointsAllocation GROUP BY CustomerID

Works well and gives me total points per customer. I can then INNER JOIN this to the customer table to get the persons name. Perfect.

Now I want to add the DLevel from the DanceLevel table to the results where the SUM total is used to lookup the Threshold and not exceed the value so I get the following:

(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 is not shown as her SUM of Points is less than or equal to 2 giving her a calculated dance level of Beginner and this already matches the her Dancelevel1 in the Customer table.

Any ideas anyone?

Aucun commentaire:

Enregistrer un commentaire