Marco Marco - 4 months ago 5x
SQL Question

Is there a way to break out a row if it meets a certain value?

For example, let's say there's a table like this:

ID Food
-- ----
1 Fruit

So whenever it says Fruit, I want to break it out into 3 rows:

ID Food
-- ----
1 Apple
1 Banana
1 Orange

Any tips?


Hmmm . . . You could do:

select, coalesce(a.alt, as food
from t outer apply
     (select alt
      from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
      where = 'Fruit'
     ) a;