Pivoting and adding new column in SQL Server Management Studio 2016? -




i have database looks this:

indexid questionid  answergiven 1       3           phone 1       7           agree 2       8           agree 2       5           yes 2       3           chat 3       6           null 3       3           phone 4       3           web 4       7           disagree 

and want write script pull out question #3 own column called contactchannel, this:

indexid questionid  contactchannel  answergiven 1       7           phone           agree 2       8           chat            agree 2       5           chat            yes 3       6           phone           disagree 4       7           web             disagree 

i'm new sql, suspect has pivoting , sub-queries, , know can vary database ideas ssms 2016?

you can use query following:

select t1.indexid, t1.questionid, t2.answergiven contactchannel, t1.answergiven mytable t1 left join mytable t2 on t1.indexid = t2.indexid , t2.questionid = 3 t1.questionid <> 3; 

the query in ansi sql , should work in rbdms.

demo here





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -