我想根据我的条件显示其他列。我的条件基于类型。但是当我在条件时使用以下情况时,我似乎检索一行带有硬编码值,每行匹配一行。不匹配有1个空行。在条件
时,我似乎无法弄清楚我的错误select t1.id,
case when
t2.value like 'App%'
then 'Fruit'
end as 'Fruit'
case when
t2.value like 'Car%'
then 'Veggie'
end as 'Veggies'
case when
t3.value = 'Skittles'
then 'Candy'
end as 'Candy'
where t1.id = t2.id
and t1.id=t3.id
T1:
ID
1
2
3
4
5
6
7
8
9
T2:
ID(fk) value
1 Apple
1 Orange
2 Carrot
3 Berry
3 Melon
T3:
ID (fk) value
4 Mars
5 Twix
6 Skittles
7 Milkyway
期望的输出:
ID Fruits Veggies Candy
1 Fruit
2 Veggie
3
4
5
6 Candy
7
8
9
当前输出错误:
ID Fruits Veggies Candy
1 Fruit
1
2 Veggie
2
3
4
5
6 Candy
6
7
8
9
答案 0 :(得分:1)
select
t1.id,
CASE WHEN SUM(CASE WHEN t2.value like 'App%' THEN 1 END) > 0
THEN 'Fruit'
END as 'Fruit',
CASE WHEN SUM(CASE WHEN t2.value like 'Car%' THEN 1 END) > 0
THEN 'Veggie'
END as 'Veggies',
CASE WHEN SUM(CASE WHEN t2.value like 'Skittles' THEN 1 END) > 0
THEN 'Candy'
END as 'Candy'
where t1.id = t2.id
and t1.id = t3.id
GROUP BY t1.id