0

I have a string of values like '409.7,409,334.2' ; the number of elements varies. I like to COUNT how many records there are with 409.7 + how many records there are with 409 + how many records there are with 334.2 Something like?

SELECT COUNT "PrimaryKey" 
FROM Data 
WHERE "Program Number" IN '409.7,409,334.2' AND "Date" >=  1/1/2020
GMB
  • 216,147
  • 25
  • 84
  • 135
user2548919
  • 111
  • 1
  • 5

1 Answers1

0

If you just want the overall count, you can use find_in_set() and aggregation:

select count(*) cnt
from data
where find_in_set(program_number, '409.7,409,334.2')

If you want a per-element count, then one option is conditional aggregation:

select 
    sum(find_in_set(program_number, '409.7,409,334.2') = 1) cnt1,
    sum(find_in_set(program_number, '409.7,409,334.2') = 2) cnt2,
    sum(find_in_set(program_number, '409.7,409,334.2') = 3) cnt3
from data
where find_in_set(program_number, '409.7,409,334.2')

If you have a lot of elements the the csv list, you might as well generate rows than column. This gives you the count of matches for each element index:

select find_in_set(program_number, '409.7,409,334.2') element_index, count(*) cnt
from data
group by element_index 
GMB
  • 216,147
  • 25
  • 84
  • 135