I want to add a column to my query which will specify one or more categories a row matches. I want to take this:
+--------------+---------------+
Row | Product | Quantity_Sold |
+--------------+---------------+
1 | Coca-Cola | 15 |
2 | Cigarettes | 4 |
3 | Pretzel | 6 |
4 | Beer | 25 |
5 | Popcorn | 10 |
6 | Candy Bar | 10 |
+--------------+---------------+
And return this:
+--------------+---------------+----------------------+
Row | Product | Quantity_Sold | Category |
+--------------+---------------+----------------------+
1 | Coca-Cola | 15 | Beverages |
2 | Cigarettes | 4 | Controlled Substance |
3 | Pretzel | 6 | Snacks |
4 | Beer | 25 | Beverages |
5 | Beer | 25 | Controlled Substance |
6 | Popcorn | 10 | Snacks |
7 | Candy Bar | 10 | Snacks |
+--------------+---------------+----------------------+
Notice on line 4-5 of the output, "Beer" is on two lines, because it fits in two categories.
If I try to do this with CASE, only the first match will be counted.
This query
SELECT
Product,
Quantity_Sold,
CASE
WHEN
Product IN ('Coca-Cola', 'Beer')
THEN
'Beverages'
CASE
WHEN
Product IN ('Pretzel', 'Popcorn', 'Candy Bar')
THEN
'Snacks'
CASE
WHEN
Product IN ('Cigarettes', 'Beer')
THEN
'Controlled Substance'
END
AS Category
FROM sales_table;
Would only return this output
+--------------+---------------+----------------------+
Row | Product | Quantity_Sold | Category |
+--------------+---------------+----------------------+
1 | Coca-Cola | 15 | Beverages |
2 | Cigarettes | 4 | Controlled Substance |
3 | Pretzel | 6 | Snacks |
4 | Beer | 25 | Beverages |
5 | Popcorn | 10 | Snacks |
6 | Candy Bar | 10 | Snacks |
+--------------+---------------+----------------------+
(Notice "Beer" only appears once)
So how can I get it to show up on separate lines for all categories it matches?