I have a table t1 as shown below:
create table t1 ( person_id int, item_name varchar(30), item_value varchar(100) );
There are five entries in this table:
person_id | item_name | item_value 1 'NAME' 'john' 1 'GENDER' 'M' 1 'DOB' '1970/02/01' 1 'M_PHONE' '1234567890' 1 'ADDRESS' 'Some Addresses unknown'
Now I want to use the crosstab function to retrieve NAME , GENDER , so I write SQL as:
select * from crosstab( 'select person_id, item_name, item_value from t1 where person_id=1 and item_name in ('NAME', 'GENDER') ') as virtual_table (person_id int, NAME varchar, GENDER varchar)
My problem is that the SQL in crosstab() contains the item_name , which will lead to incorrect quotes. How to solve the problem?
source share