Looking for some advice on a particular situation in my DB...
Right now I have a database that has a query that links to a source data table. In the query, I have a table that has Group IDs, the subgroup IDs and their corresponding states. It is linked to the source data by the Group ID and is pulling States based on this.
What I am aiming to do is if the subgroup ID contains a keyword, lets say "Dog", instead of pulling states by Group ID, pull it by subgroup ID.
I have attempted using an iif statement and setting up a standalone query for just subgroup ids but I get empty values.
In the example below, records 1 & 4, would retrieve CA & WI. Records 2 & 3, because they have "Dog" in the description, they would retrieve NY.
My query is organised like so,
Group ID Subgroup ID State
1 1000067 Omega 541111 Aplha
2 1000056 Epsilon 542222 Bravo Dog
3 2000653 Gamma 546066 Echo Dog
4 2000654 Theta 968886 Charlie
& the reference table looks like this,
ID
1000067 Omega CA
1000056 Epsilon FL
2000653 Gamma TX
2000654 Theta WI
541111 Alpha CA
542222 Bravo Dog NY
546066 Echo Dog NY
968886 Charlie FL
Really appreciate any help or thoughts on best way to proceed!
In essence what you want to do is run two slightly different queries - one "dog" query and one "non-dog" query - and merge the results. A UNION query can do that. With our main table [dogMain]
Record_ID Group_ID Subgroup_ID
--------- --------------- -----------------
1 1000067 Omega 541111 Alpha
2 1000056 Epsilon 542222 Bravo Dog
3 2000653 Gamma 546066 Echo Dog
4 2000654 Theta 968886 Charlie
and our reference table [dogRef]
ID State
----------------- -----
1000067 Omega CA
1000056 Epsilon FL
2000653 Gamma TX
2000654 Theta WI
541111 Alpha CA
542222 Bravo Dog NY
546066 Echo Dog NY
968886 Charlie FL
the query
SELECT dogMain.Record_ID, dogRef.State
FROM dogMain INNER JOIN dogRef ON dogRef.ID = dogMain.Group_ID
WHERE dogMain.Subgroup_ID NOT LIKE '*dog'
UNION ALL
SELECT dogMain.Record_ID, dogRef.State
FROM dogMain INNER JOIN dogRef ON dogRef.ID = dogMain.Subgroup_ID
WHERE dogMain.Subgroup_ID LIKE '*dog'
ORDER BY 1
returns
Record_ID State
--------- -----
1 CA
2 NY
3 NY
4 WI
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.