http://stackoverflow.com/questions/9157638/how-to-transform-vertical-fields-in-a-table-to-horizontal-result-by-sql/9157697#9157697
Q&A for Drupal developers and administrators
declare @t1 table( person_id int, item_name varchar(30), item_value varchar(100) ); insert into @t1 values(1,'NAME', 'john'); insert into @t1 values(1,'GENDER', 'M'); insert into @t1 values(1,'DOB', '1970/02/01') insert into @t1 values(1,'M_PHONE' , '1234567890'); insert into @t1 values(1,'ADDRESS','Some Addresses unknown'); Select person_id as 'NAME', MAX(case WHEN item_name = 'GENDER' then item_value end ) as 'GENDER', MAX(case WHEN item_name = 'M_PHONE' then item_value end) as 'M_PHONE', MAX(case WHEN item_name = 'ADDRESS' then item_value end) as 'ADDRESS' From @t1 group by person_id