I am trying to calculate the median from a table with various types of data mixed together (I can’t modify the table). Rows 3 and 6 are examples of this data mixed in, and rows 1, 2, 4, and 5 are examples of rows which I legitimately need the data from (WHERE form_id = 1 AND field_id = 1):
id form_id field_id value
1 1 1 200
2 1 1 290
3 1 2 'Delicious Bacon'
4 1 1 320
5 1 1 120
6 2 3 9000000
I understand that I can derive median from a table using the following:
SELECT x.value from form_data x, form_data y
GROUP BY x.value
HAVING SUM(SIGN(1-SIGN(y.value-x.value))) = (COUNT(*)+1)/2;
However, the data mixed in challenges my SQL abilities. How can I find the median of this data, only where form_id = 1 and field_id = 1?
Try this