https://www.youtube.com/watch?v=L8-I_XPL_Dk
-- Create the table
CREATE TABLE occupation (
name VARCHAR(20),
occup VARCHAR(20)
);
-- Insert values into the table
INSERT INTO occupation (name, occup)
VALUES
('Samantha', 'Doctor'),
('Julia', 'Actor'),
('Maria', 'Actor'),
('Meera', 'Singer'),
('Ashely', 'Professor'),
('Ketty', 'Professor'),
('Christeen', 'Professor'),
('Jane', 'Actor'),
('Jenny', 'Doctor'),
('Priya', 'Singer');
select * from occupation
with cte as (
select
name,occup,row_number() OVER (partition by occup order by name) as rn
from occuption
)
select
MAX(case when occup = 'Actor' THEN name END) AS Actor
,MAX(case when occup = 'doctor' THEN name END) AS Doctor
,MAX(case when occup = 'Professor' THEN name END) AS Professor
,MAX(case when occup = 'Singer' THEN name END) AS Singer
from cte
group by rn
order by rn;