Mysql Get Column Name after Case Max
Thanks for the useful suggestion.

Now the query work as in this way:

select DISTINCT t3.uid,latitude,longitude
from (
  SELECT DISTINCT 
  t2.*, 
  MAX(CASE WHEN t2.datakey = 'latitude' THEN t2.data END) AS latitude,
  MAX(CASE WHEN t2.datakey = 'longitude' THEN t2.data END)AS longitude
FROM `#_social_fields_data` AS t1 
INNER JOIN `#_social_fields_data` AS t2 ON t1.uid  = t2.uid
GROUP BY t1.uid
     ) as t3
WHERE latitude &gt; 0 


Thanks, as suggested i have optimized the query removing self_join:

select DISTINCT t2.uid,latitude,longitude
from (
  SELECT DISTINCT 
  t1.*, 
  MAX(CASE WHEN t1.datakey = 'latitude' THEN t1.data END) AS latitude,
  MAX(CASE WHEN t1.datakey = 'longitude' THEN t1.data END)AS longitude
FROM `#_social_fields_data` AS t1 
GROUP BY t1.uid
     ) as t2
WHERE latitude &gt; 0