Warm tip: This article is reproduced from stackoverflow.com, please click
ms-access

Comparing master list of users against a second list with multiple entries per user

发布于 2020-03-27 10:31:02

I am trying to determine what courses each user has remaining based on certain criteria. I receive 2 data sets. I would like to use MS Access 2013 to process the data.

Data Set 1: Master list of all users

Data Set 2: Course completion list for each user

Depending which category a user falls under determines which courses they need to complete. The data set is not entirely clean nor is it perfect.

Data Set 1

NAME           CATEGORY
TOASTER, OVEN      3
MICRO, WAVE        2
WHITE, BOARD       1
MOUSE, PAD         1
KEY, BOARD         4
PHONE, CHARGER     2
WATER, BOTTLE      2

Data Set 2

NAME          COURSE    DATE
TOASTER, OVEN   3A  2000-05-05
TOASTER, OVEN   3B  2019-03-02
TOASTER, OVEN   3A  2018-11-05
MICRO, WAVE     2A  2017-10-20
WHITE, BOARD    1A  2018-01-10
KEY, BOARD     3A-1 2019-01-15
PHONE, CHARGER 2A-1 2018-09-17
WATER, BOTTLE 2A-CL 2015-06-26
WATER, BOTTLE   2A  2011-12-15

DESIRED RESULTS

NAME        CATEGORY    REQUIREMENT   STATUS
TOASTER, OVEN   3       ADVANCED      COMPLETED
MICRO, WAVE     2       INTERMEDIATE  COURSE 2 REMAINING
WHITE, BOARD    1       INTRODUCTORY  COMPLETED
MOUSE, PAD      1       INTRODUCTORY  NOT COMPLETED
KEY, BOARD      4       ADVANCED      COURSE 1 REMAINING
PHONE, CHARGER  2       INTERMEDIATE  COURSE 2 REMAINING
WATER, BOTTLE   2       INTERMEDIATE  COMPLETED

Based on the 2 data sets, I would like the desired results to show 2 additional columns to Data Set 1.

Logic:

If CATEGORY = 3, REQUIREMENT = ADVANCED. If CATEGORY = 2, REQUIREMENT = INTERMEDIATE. If CATEGORY = 1, REQUIREMENT = INTRODUCTORY. This logic, I can perform.

If REQUIREMENT = ADVANCED, then check Data Set 2 to see if course 3A AND 3B exist, OR if 3A-CL, OR if 3A-EQ exist, then STATUS = COMPLETED, ELSE IF ONLY 3A exists, then say Course 2 Remaining, ELSE IF ONLY 3B exists, then say Course 1 Remaining.

If REQUIREMENT = INTERMEDIATE, then check Data Set 2 to see if course 2A and 2B exist, OR if 2A-CL, OR if 2A-EQ exist, then STATUS = COMPLETED, ELSE IF ONLY 2A exists, then say Course 2 Remaining, ELSE IF ONLY 3B exists, then say Course 1 Remaining.

If REQUIREMENT = INTRODUCTORY, then check Data Set 2 to see if Course 1A OR 1A-CL OR 1A-EQ exist, then STATUS = COMPLETED, ELSE NOT COMPLETED.

Please let me know if I need to clarify anything.

I would appreciate any assistance or guidance on this at all!

Questioner
MonkeyMonkey
Viewed
33
SunKnight0 2019-07-04 03:31

One way to do is is by multiple joins of your second data set. This example includes all the elements you need to complete your task. You just have to expand it to the rest of yout required logic:

SELECT T1.NAME,
    T1.CATEGORY,
    ISNULL(IS_3A.DATE) AS EXISTS_3A_EXAMPLE,
    NOT (ISNULL(IS_3A.DATE) OR ISNULL(IS_3B.DATE)) AS EXISTS_3A_AND_3B_EXAMPLE
FROM (( [DATA SET 1] T1
    LEFT JOIN [DATA SET 2] IS_3A ON IS_3A.COURSE='3A' AND T1.NAME=IS_3A.NAME )
    LEFT JOIN [DATA SET 2] IS_3B ON IS_3B.COURSE='3B' AND T1.NAME=IS_3B.NAME )
    LEFT JOIN [DATA SET 2] IS_3A_CL ON IS_3A_CL.COURSE='3A-CL' AND T1.NAME=IS_3A_CL.NAME