Skip to content
Advertisement

Rotate Database Table

Imagine I have a database table that has the following format:

I need to pivot it in the format as below. Here each distinct value of quarter becomes a column, and each quarterly sales is the value of the corresponding column for each sales person.

Is this possible with SQL? I am using SQL Server, but prefer a plain SQL if possible.

We do not know the values that go into Quarter column up front i.e. we are not limited to only Q1/Q2/Q3 etc. Let’s say the users can enter Q1, Quarter 1, or anything else they like.

Advertisement

Answer

You can try to use aggregation condition SUM with CASE WHEN

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