I have the following table:
ID Fruit
A apple,orange,grape
A apple,orange,cucumber
A apple,orange
B orange,grape
B apple
B grape
C grape,banana
C orange,banana
C banana
I am hoping to add new columns to denote whether a certain ID has had specific characters in the fruit row
ID Fruit apple banana orange
A apple,orange,grape yes no yes
A apple,orange,cucumber yes no yes
A apple,orange yes no yes
B orange,grape yes no yes
B apple yes no yes
B grape yes no yes
C grape,banana no yes yes
C orange,banana no yes yes
C banana no yes yes
I asked a similar question a while back here.
SELECT a.*,
case when count(case when a.Fruit like 'apple' then 1 end) over (partition by ID) > 0 then 'yes' else 'no' end [Apple],
case when count(case when a.Fruit like 'banana' then 1 end) over (partition by ID) > 0 then 'yes' else 'no' end [Banana],
case when count(case when a.Fruit like 'orange' then 1 end) over (partition by ID) > 0 then 'yes' else 'no' end [Orange]
FROM MyTable a
This, however, is being very resource heavy and slow, given the size of my tables. In terms of performance, is there a more efficient solution to this with coalesce
or join
or any other method?