sum(case when (i.deep=1 OR i.deep>1) and i.type=0 then s.deep1 else 0 end) AS deep1, sum(case when (i.deep=1 OR i.deep>1) and i.type=1 then s.deep1 else 0 end) AS deep5, sum(case when (i.deep=1 OR i.deep>1) and i.type=2 then s.deep1 else 0 end) AS deep9, sum(case when (i.deep=1 OR i.deep>1) and i.type=3 then s.deep1 else 0 end) AS deep13, sum(case when (i.deep=2 OR i.deep>2) and i.type=0 then s.deep2 else 0 end) AS deep2, sum(case when (i.deep=2 OR i.deep>2) and i.type=1 then s.deep2 else 0 end) AS deep6, sum(case when (i.deep=2 OR i.deep>2) and i.type=2 then s.deep2 else 0 end) AS deep10, sum(case when (i.deep=2 OR i.deep>2) and i.type=3 then s.deep2 else 0 end) AS deep14, sum(case when (i.deep=3 OR i.deep>3) and i.type=0 then s.deep3 else 0 end) AS deep3, sum(case when (i.deep=3 OR i.deep>3) and i.type=1 then s.deep3 else 0 end) AS deep7, sum(case when (i.deep=3 OR i.deep>3) and i.type=2 then s.deep3 else 0 end) AS deep11, sum(case when (i.deep=3 OR i.deep>3) and i.type=3 then s.deep3 else 0 end) AS deep15, sum(case when i.deep=4 and i.type=0 then s.deep4 else 0 end) AS deep4, sum(case when i.deep=4 and i.type=1 then s.deep4 else 0 end) AS deep8, sum(case when i.deep=4 and i.type=2 then s.deep4 else 0 end) AS deep12, sum(case when i.deep=4 and i.type=3 then s.deep4 else 0 end) AS deep16, sum(i.deep) AS zf
FROM info AS i LEFT JOIN credits AS s ON i.type=s.type LEFT JOIN contact AS c ON i.uid=c.uid $wansql GROUP BY i.uid ORDER BY zf DESC