Marco Marco - 2 years ago 71
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?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download