With table "Groceries" having columns "Name" and "Item", with the format:
*Name* | *Item*
Bill | Eggs
Bill | Milk
Sam | Eggs
Sam | Turkey
Sam | Butter
Josh | Milk
Josh | Butter
I'm looking to generalize a query to cast these "Items" out into their own columns, acting as flags.
My current query is essentially:
SELECT
MAX(CASE WHEN grocery.Item = 'Eggs' THEN 1 ELSE 0 END) 'Eggs',
MAX(CASE WHEN grocery.Item = 'Milk' THEN 1 ELSE 0 END) 'Milk',
MAX(CASE WHEN grocery.Item = 'Turkey' THEN 1 ELSE 0 END) 'Turkey',
MAX(CASE WHEN grocery.Item = 'Butter' THEN 1 ELSE 0 END) 'Butter'
FROM
Groceries grocery
which works, but I want to generalize it somehow so that rather than listing out each unique value and using this "MAX(CASE WHEN" logic on each one, the query does this automatically for whichever distinct values are in the grocery.Item column.
Is there any way to do this generalization, or will I simply have to list out each value? Thanks in advance.