I am using VBA to generate SQL queries in Access. I have two SQL queries that return two lists of items with a status column that shows Required or Optional:
"SELECT *, 'Required' as status FROM tblMain WHERE " & where_1
"SELECT *, 'Optional' as status FROM tblMain WHERE " & where_2
*where_1* and *where_2* are string variables I build within the VBA–it’s not really important what they are though.
What I’d like to do is have one combined list, and if an item shows in both lists, I’d like that item to show just once with a “Required” status (“Required” trumps “Optional”). I started with a union query, but I’m not sure how to eliminate the duplicate rows with the “Optional” statuses.
SELECT * FROM
((SELECT *, 'Required' as status FROM tblMain WHERE where_1)
UNION
(SELECT *, 'Optional' as status FROM tblMain WHERE where_2))
I’m thinking a DISTINCT or possibly a FIRST could be used, but unfortunately I’m having trouble working out the syntax.
I suggest you eliminate IDs selected in the first statement from the second statement.