2

I am having difficulty in understanding the below SQL query to pivot the table,

Table Name : Occupations

Name    | Occupation
-----------------
Sam     | Actor
Julia   | Singer
Ketty   | Actor

Expected result:

Actor   | Singer
----------------
Sam     | Julia
Ketty   | null

I got only one row, When I executed the below query,

Select [Actor],[Singer] From ( Select Occupation,Name From Occupations
 )sub Pivot (Max(Name) For Occupation in ([Actor],[Singer])) pvt

output :

Actor   | Singer
----------------
Sam | Julia

When I modified the above query using RowNumber(), I got the expected result(multiple rows).

 Select [Actor],[Singer] 
 From ( Select Occupation,Name,Row_Number() over(partition by Occupation order by Name)SNo
 From Occupations )sub
 Pivot (Max(Name) For Occupation in ([Actor],[Singer])) pvt

Can you explain , How adding Row_Number function in the sub query gives multiple row?

4
  • Your source data is insufficient to pivot, because there is no column which logically connects one actor to another singer. Commented May 14, 2018 at 10:16
  • Actually, I just want the list of Actor and Singer. If I execute the second query in the question I got the result, But I don't know how it works. Commented May 14, 2018 at 10:22
  • For this it would better to do google instead of asking the question, just checkout how PIVOT and Row_Number() works ? Commented May 14, 2018 at 10:25
  • @SuryaPrakash The Yogesh answer below should work for you then. Commented May 14, 2018 at 10:27

2 Answers 2

4

Pivot is a tricky syntax. It returns one row for all the combination of columns other than the columns involved with the pivot. When you do:

Select [Actor], [Singer] 
From (Select Occupation, Name,
      From Occupations
     ) sub
Pivot (Max(Name) For Occupation in ([Actor], [Singer])) pvt

There are no other rows. Hence, one row is returned.

When you add:

row_Number() over (partition by Occupation order by Name) as sno

You now have another column that varies, and it is used (implicitly) by the pivot.

This awkward behavior is one reason why I prefer conditional aggregation to pivot.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gordon, This is the answer I was looking for.
1

You want conditional aggregation :

select 
        max(case when Occupation = 'Actor' then Name end) [Actor],
        max(case when Occupation = 'Singer' then Name end) [Singer]
from (select *,
            row_number() over (partition by Occupation order by Name) Seq
      from table t
     ) t
group by Seq;

Since the above data model has no any grouping column/field based on that desired result achieve. So, you can generate the Sequence using analytical function and use them as in GROUPING.

2 Comments

Hi Yogesh, your query works fine. But I just want to achieve the same using pivot.
@SuryaPrakash... Your pivot query should also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.