I have a syntax issue with my use of GROUP_CONTACT.
With a sql statement that looks like this:
SELECT
s.school_code
, s.school_name
,st.subject
,sg.subgroup
,GROUP_CONCAT(IF(r.year=2012,a.proficiency_index,NULL)) AS pi_2012
,GROUP_CONCAT(IF(r.year=2013,a.proficiency_index,NULL)) AS pi_2013
FROM
ayp_data a
INNER JOIN
report_year r ON
a.report_year_id = r.id
INNER JOIN
school s ON
a.school_code_id = s.id
INNER JOIN
sub_group sg ON
a.subgroup_id = sg.id
INNER JOIN
`subject` st ON
a.subject_id = st.id
GROUP BY
report_year_id,
s.school_code
, s.school_name
,st.subject
,sg.subgroup
HAVING
s.school_name = 'Moody Elementary School' AND
`subject` = 'Mathematics' AND
`subgroup` = 'All Students'
I am getting results like this:
SCHOOL_CODE SCHOOL_NAME SUBJECT SUBGROUP PI_2012 PI_2013
0065 Moody Elementary School Mathematics All Students 9.640000343322754 (null)
0065 Moody Elementary School Mathematics All Students (null) 10.920000076293945
I want to merge the two rows into one and put non-null field values PI_2012 and PI_2013 on the same line.
I thought I could do that with GROUP_CONTACT; but it’s not doing as I thought it would.
How could I use GROUP_CONCAT to merge these fields?
Or, is there an even smarter way to do this?
I have the full schema and query here on SQL Fiddle.
If you don’t want separate years to appear in separate rows, then you need to remove
report_year_idfrom theGROUP BYclause. That is — you need to change this:to this: