有没有办法在满足某个值的情况下将一行打断?
Is there a way to break out a row if it meets a certain value?
例如,假设有这样一个 table:
ID Food
-- ----
1 Fruit
所以每当它说水果时,我想把它分成 3 行:
ID Food
-- ----
1 Apple
1 Banana
1 Orange
有什么建议吗?
嗯。 . .你可以这样做:
select t.id, coalesce(a.alt, t.food) as food
from t outer apply
(select alt
from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
where t.food = 'Fruit'
) a;
select ID, Food FROM YourTable WHERE food <> 'Fruit'
UNION
select ID, f2 FROM YourTable JOIN
(SELECT'Apple' f2
union
SELECT 'Banana' f2
UNION
SELECT 'ORANGE' f2
) DT
ON food = 'Fruit'
例如,假设有这样一个 table:
ID Food
-- ----
1 Fruit
所以每当它说水果时,我想把它分成 3 行:
ID Food
-- ----
1 Apple
1 Banana
1 Orange
有什么建议吗?
嗯。 . .你可以这样做:
select t.id, coalesce(a.alt, t.food) as food
from t outer apply
(select alt
from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
where t.food = 'Fruit'
) a;
select ID, Food FROM YourTable WHERE food <> 'Fruit'
UNION
select ID, f2 FROM YourTable JOIN
(SELECT'Apple' f2
union
SELECT 'Banana' f2
UNION
SELECT 'ORANGE' f2
) DT
ON food = 'Fruit'