I know the title is a little vague, but I have a situation where the query I am writing uses a CASE WHEN
statement to identify categories selected on the front end. The table (Purchased
) I am querying has a column named item
. The item table of course has the unique identifier for each item. The item column in the Purchased table will show the number of the item purchased, but if multiple items were purchased, then the numbers are sum together. An example would be:
ITEM TABLE
-------------
1 Item A
2 Item B
4 Item C
8 Item D
and so on where the max Item unique identifier is 256 (1,2,4,8,16,32,64,128,256). A doubling sequence was used so that every number in the Purchased
table can only have one possible item or groups of items associated with it.
So in the Purchased
table, if in the item column there is a '3', then both Item A and Item B were purchased. I can do a CASE WHEN
to show the Item name that was purchased if only 1 item was purchased, but looking for a way to write a query where I designate what each number is equal to as an Item name and then the query takes the '3' and then CONCAT
the Items that match that number.
Otherwise, I would have a very long Case When statement and I would have hundreds on possibilities.
Hope this makes sense. Relatively new to SQL. Thank you in advance.
Tables to Start
I would normally perform a:
CASE WHEN s.ItemsPurchased = 1 THEN 'Bike' WHEN s.ItemsPurchased = 2 THEN 'Helmet' .... END as ItemsPurchased,
The issue is the multiple items in one order and the many, many possibilities within the structure.