I have been reading about the difference between UNIONS and JOINS and believe UNION is what I need for my solution, however I am unable to get this working as I think it's intended.
I've been looking on some other SO articles, including this one. What is the difference between JOIN and UNION?
According to some of the detailed answers in that link, UNION would combine two queries together to form one dataset.
My scenario is that I have a sales database showing number of sales, and at what sales level. I have two such tables with similar information that need to come together to form a complete set.
Table 1: North Side
Item | Type | SalesLevel
--------------------------------
John | Apple | 1
John | Pear | 2
Craig | Apple | 1
Dylan | Apple | 1
Table 2: South Side
Item | Type | SalesLevel
--------------------------------
John | Apple | 1
John | Pear | 1
Craig | Apple | 1
Dylan | Apple | 2
As you can see the tables contain the same columns, and my expected results needs to show only the item for each individual where the Type has the highest SalesLevel.
The results that I am after would then look like the following.
Item | Type | SalesLevel
--------------------------------
John | Apple | 1
John | Pear | 2
Craig | Apple | 1
Dylan | Apple | 2
When I run my query, I am only getting the Max value per Select statement. I can see 'why' this might happen with the construct of the query, but would have thought that the UNION would have joined these two select statements into one query first? I'm looking for some assistance on how to better construct this query to obtain the expected results.
My query so far
Select Name, Item, MAX(SalesLevel)
FROM NorthSide
Group By Name, Item
UNION
Select Name,
Item, MAX(SalesLevel)
FROM SouthSide
Group By Name, Item