Rather new to Access (using 2016) and very sketchy on my SQL skill set.
First query totals sales by ALL THREE (user-supplied) criteria:
- Salesman
- Customer
- Product_Code
- [Year]
Second query does same except for [Year-1] where [Year] is a user supplied variable. This shows me current and prior total sales grouped by the three criteria shown above.
First Query will produce:
AKER Cust1 Prod1 CurSales
AKER Cust1 Prod2 CurSales
CLUT Cust1 Prod3 CurSales
DELO Cust3 Prod1 CurSales
etc…. in any combination of the three fields – all producing a total sales number for that combination for the CURRENT YEAR.
Second Query produces same, except for the PRIOR YEAR and may contain more (or fewer) combinations of the three fields.
I want third query to combine the two and show each combination that has a value of "Total Sales" in EITHER YEAR. I can then do comparisons using calculated fields.
I'm using ACCESS 2016 – primarily using the GUI.
For reporting, I need to do full outer join (I think) of these queries (i.e. show all records in both, joining rows where appropriate). I will then use this combined query to report and show comparative date.
I need help creating this second query. Do I need to create a unique "key" perhaps? Chasing my tail…..
Also, a crosstab can give me the exact results I want but I cannot then create a report because the field names are dynamic and I can't see myself writing VBA code at this juncture. I anyone knows how to accomplish this without VBA, I'm all good .
Best Answer
As you note, you need to do a full outer join between the two queries.
Let's assume your first query (for the user-specified year) is:
Then, the combined query should look like this:
NOTE:
COALESCE()
may beISNULL()
in Access, or fall under some other name - it returns the first value of the values passed to it that isn't NULL (or NULL, if all values are NULL).Also: as I'm not particularly familiar with Access itself, this is in SQL Server syntax. I think modern versions of Access should be OK with that, but I'm not sure.
How you would convince a GUI query builder to do this for you, I have no idea - most of them have difficulty with more complex queries. Hopefully, Access will let you create the query in a text editor and paste it in, if nothing else.
Well, since apparently MS Access doesn't have FULL OUTER JOIN, we need to get trick it into giving us we what want.
A
FULL OUTER JOIN
ofA
andB
onA.col = B.col
gives you three things:A
where there's no matching row inB
(with the columns from bothA
andB
, where allB
columns are NULL); andB
where there's no matching row inA
(with the columns from bothA
andB
, where allA
columns are NULL).Now,
A LEFT OUTER JOIN B ON A.col = B.col
gives us the first two of the three items above.So, we need to a way to get just item 3 above, and then combine that with the results of
A LEFT OUTER JOIN B
.See if this (overly complicated) query seems to work:
UNION ALL
combines the results of two queries (with compatibleSELECT
lists) into one result set.The first query,
A LEFT OUTER JOIN B
, gives us items 1 and 2 from our bullet list above.If we simply created a query,
A RIGHT OUTER JOIN B
, we'd get items 1 and 3 from the bullet list.By adding the
WHERE
clause, where we only accept rows from theRIGHT JOIN
where our join columns fromA
are NULL, and at least one of our join columns fromB
is not NULL. The rows from item 1 will all have matching values inA
and inB
- so, either they will have a non-NULL value in at least one of the join columns, or all the join columns from bothA
andB
will be NULL. So, this should just give us the rows from item 3 in our bullet list.NOTE: this is all untested - please confirm that it works before using in production code!