I have a table tab
contains events where participants are listed as a comma separated list of ids: 1,5,6,24,124
I want to get the AVG participant number for events only if the resolution
is equal to Completed
This is what I tried:
SELECT avg(case when (resolution = 'Completed' then LENGTH(REPLACE(participants, ',', '')) end) participants FROM tab;
but it doesn't work
Fix your data model: Storing lists in a string is wrong for a lot of reasons:
The right thing to do is to fix the data model. Sometimes, you are stuck with someone else's really, really bad data model. In that case, you can count the number of commas and add one:
SELECT AVG( 1 + LENGTH(participants) - LENGTH(REPLACE(participants, ',', ''))) as avg_participants
FROM tab
WHERE resolution = 'Completed';
However, your real effort should be on fixing the data model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.