Skip to content
Advertisement

SQL Query for multiple Pivot on same column

As per my requirement, I need to apply pivot twice on same column to get different values as column header. Please see below how the data is present in table and expected results.

Table

Question Response TranslatedResponse activityid createdon
Reason Testing testaus 1 01-01-2022
EMail abc@gmail.com abc@gmail.com 1 01-01-2022
Action test testata 1 01-01-2022

Desired Output :

Email Reason Action Translated Action Translated Reason activityid createdon
abc@gmail.com Testing test testata testaus 1 01-01-2022

I have achieved this by using below query where it works by using inner join with two different pivot select statement.

Is there another way of writing SQL query to optimize and get rid of inner join.

Advertisement

Answer

Use conditional aggregation:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement