SQL Convert Rows to Columns and Columns to Rows without using Pivot Functions

SQL Convert Rows to Columns and Columns to Rows without using Pivot Functions

Ankit Bansal

2 года назад

100,850 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

gaurav singh
gaurav singh - 26.09.2023 15:22

Is there any way we can use without union

Ответить
Yatin Shekhar
Yatin Shekhar - 22.09.2023 20:13

8/141

Ответить
tarun
tarun - 21.09.2023 13:38

awesome vide

Ответить
Tushar Vijay
Tushar Vijay - 30.08.2023 02:43

Your solution is the more general one but an alternate solution can be proposed for this case ONLY since there is only 1 salary, 1 bonus, 1 hike per emp_id as:

with cte1 as
(select *, row_number() over() as rn_sal from
(select val as salary from interview_test_14 where salary_component_type='salary') sal_table)
, cte2 as
(select *, row_number() over() as rn_bon from
(select val as bonus from interview_test_14 where salary_component_type='bonus') bon_table)
, cte3 as
(select *, row_number() over() as rn_hike from
(select val as hike_percent from interview_test_14 where salary_component_type='hike_percent') sal_table)
select rn_sal as emp_id,salary,bonus,hike_percent
from cte1 join cte2 on rn_sal=rn_bon join cte3 on rn_bon=rn_hike;

However, a case and summing operation has to be used like the one you did if there exist multiple values for each emp_id which is pretty much the most general case of a pivotted table. Thanks for your approach!

Ответить
Subhojeet Learning
Subhojeet Learning - 24.08.2023 13:50

Very nicely explained

Ответить
MrKingoverall
MrKingoverall - 23.08.2023 13:42

Thank you so much sir for your help. 👍👍🙏

Ответить
Trishul Thakur
Trishul Thakur - 05.08.2023 20:23

Thank you valuable share

Ответить
SRK
SRK - 05.08.2023 13:20

Thank you . What if I add one more type called Gender that has "Male" or "Female" - we cannot do SUM or any numerical aggregate like MIN/MAX in that case - so how do we solve that?

Ответить
Vaishnavi
Vaishnavi - 04.08.2023 09:30

What if there are 2 columns example val and expense_amount. So i want 2 colums from 1 row. Salary_val and salary_expense_amount. How to do that?

Ответить
Aditya Fargade
Aditya Fargade - 02.08.2023 20:40

This is very helpful!
Thanks

Ответить
Deepak Kumar Padliya
Deepak Kumar Padliya - 30.07.2023 11:13

Hello sir i have scenario where i have dynamic columns can you guide what can be solution for that.

Ответить
Ashish Dukare
Ashish Dukare - 27.07.2023 10:05

Thank you for your efforts : )

Ответить
Parth Maheshwari
Parth Maheshwari - 26.07.2023 00:41

Instead of "SUM" can we use "MIN" and "MAX"?

Ответить
ujjwal varshney
ujjwal varshney - 25.07.2023 05:09

select a.emp_id ,a. val as salary , b.val as bonus ,c.val as hike_percent
from emp_compensation a join emp_compensation b on a.emp_id = b.emp_id
join emp_compensation c on a.emp_id = c.emp_id
where a.salary_component_type = 'salary' and b.salary_component_type ='bonus' and c.salary_component_type = 'hike_percent'
order by 1

Ответить
uday kumar
uday kumar - 24.07.2023 09:04

Thanks a lot sir for explaining in detail.

Ответить
Arcot Sunder Raghu Bhooshan
Arcot Sunder Raghu Bhooshan - 06.07.2023 17:58

Practising from your videos before my interview. Thanks for the videos.

Ответить
Arnab Sarkar
Arnab Sarkar - 29.06.2023 00:02

@ankit How to dynamically pick pivot rows to columns without using pivot function?

Ответить
toshita chakraborty
toshita chakraborty - 27.06.2023 13:27

I am a big fan if you Ankit 💓

Ответить
Millar
Millar - 05.06.2023 17:39

how to create duplicate table ( with new table name) in oracle with existing table values

create table old as
select emp_id,
sum(case when salary_component_type ='salary' then val end )as salary ,
sum(case when salary_component_type ='hike_percent' then val end) as hike_percent,
sum(case when salary_component_type ='bonus' then val end) as bonus
from emp_compensation
group by emp_id ;

How to create a copy of an Oracle table without copying existing data but table structure should remain same?

create table old as
select emp_id,
sum(case when salary_component_type ='salary' then val end )as salary ,
sum(case when salary_component_type ='hike_percent' then val end) as hike_percent,
sum(case when salary_component_type ='bonus' then val end) as bonus
from emp_compensation
where 1=0;
group by emp_id ;

Ответить
Ramanjeet Singh
Ramanjeet Singh - 28.05.2023 13:19

I have one doubt, if I have string like first_name in column and how to eliminate those Null values just like you did using Sum function in case of numbers. anyone knows the solution?

Ответить
Rushi Vanjare
Rushi Vanjare - 14.05.2023 07:39

Dynamic sp sikhaoooo kitne barre bol rha hu

Ответить
Sujit Nayak
Sujit Nayak - 08.05.2023 18:06

The same query how can we perform if the value has non numeric values.

Ответить
Pavan Awasthi
Pavan Awasthi - 07.05.2023 12:07

Sir make please plsql series.

Ответить
Babu Jee
Babu Jee - 04.05.2023 09:34

what happend when someone enter the new salary component type
the query set was only the exitsting data when someone enter the new salary component type
so every time i change the query
it's not good

please help me this

like whenever some add new salary component type how to automatically add val in query

Ответить
TOM THOMAS
TOM THOMAS - 26.04.2023 12:45

Using PIVOT, we can solve it by:

select emp_id,salary,bonus,hike_percent
from
(select emp_id,salary_component_type,val from emp_compensation) p
PIVOT(
max(val)
for salary_component_type in ([salary],[bonus],[hike_percent])
)as pivot_table

Ответить
Ashutosh Pandey
Ashutosh Pandey - 26.04.2023 00:21

this is what I was searching for.. thanks man

Ответить
Deepak C L
Deepak C L - 15.04.2023 06:18

Thank you so much ! 🙂

Ответить
Vivek Singh Negi
Vivek Singh Negi - 01.04.2023 13:23

My Answer:
with pviot as (select emp_id,sum(case when salary_component_type='salary' then val else 0 end) as Salary,
sum(case when salary_component_type='bonus' then val else 0 end) as bonus,
sum(case when salary_component_type='hike_percent' then val else 0 end) as hike_percent
from emp_compensation
group by 1)
select emp_id,Case when Salary then 'Salary'
end as salary_component_type,
case when salary then Salary end as val
from pviot
union
select emp_id,Case when bonus then 'bonus'
end as salary_component_type,
case when bonus then bonus end as val
from pviot
union
select emp_id,Case when hike_percent then 'hike_percent'
end as salary_component_type,
case when hike_percent then hike_percent end as val
from pviot;

Ответить
Brian Ducharme
Brian Ducharme - 31.03.2023 03:18

Sir you are brilliant, so much easier then using SQL pivot operator. Thank you kindly.

Ответить
JJJJJUN
JJJJJUN - 31.03.2023 00:25

it's hard to see so so clear and easy understanding video. thank you

Ответить
Kundan Kumar
Kundan Kumar - 27.03.2023 18:56

True superhero👍

Ответить
Vinoth Sankar
Vinoth Sankar - 21.03.2023 21:52

Hi Ankit - Thanks for Video. How we can use this if we have char/varchar instead of integers in the table

Ответить
Sanchit Vaid
Sanchit Vaid - 20.03.2023 21:23

I was asked this question in an interview (US based Health insurance company) but I only knew how to solve it using Pivot function and could not answer it. I wish if I had found this video sooner!

Ответить
SRIKANTH P
SRIKANTH P - 15.03.2023 12:54

sir there is one doubt for me correct me if I'm wrong as you have shown in the video there are only a few rows so we can use the case statement but if there is n number of rows how to do then we can't use the case function right.

Ответить
vin
vin - 10.03.2023 11:02

Thank you Ankit for awesome explanation.
Is there another video to practice this topic?

Ответить
Des Flanagan
Des Flanagan - 07.03.2023 14:18

Thank you, this help me a lot.

Ответить
Lokesh Vaishnav
Lokesh Vaishnav - 07.03.2023 07:45

Man you are an amazing person helping a lot of people

Ответить
Ritish Adhikari
Ritish Adhikari - 03.03.2023 18:46

Thanks Ankit for the video, we have to use pivot function using pandas for subsequent ml work, but now we can do the data transformation directly in SQL and post which we can directly train the ml model

Ответить
Anshul Marele
Anshul Marele - 27.02.2023 19:13

Hi, how to unpivot data in Athena SQL

Ответить
K H A N ' s
K H A N ' s - 21.02.2023 20:56

This was the first video of yours that I watched and it's enough to subscribe to your channel! 👍🏼

Ответить
Shivam Bansal
Shivam Bansal - 20.02.2023 06:53

I think, We can also use MIN/MAX instead of SUM, isn't it ?

Ответить
Naveen Kumar
Naveen Kumar - 16.02.2023 14:43

super sir make a more interview quations.....

Ответить
kawshal chettri
kawshal chettri - 14.02.2023 15:36

Hello Ankit,
I bought multiple courses on SQL but finally started to understand by watching your videos. Thank you so much.
I want to know where can I get more use cases to practice for case statements using sum and other aggregate functions.

Ответить
Chrono Ray
Chrono Ray - 12.02.2023 00:54

So fantastic. Thank you so much. :)

Ответить
harshit aggarwal
harshit aggarwal - 29.01.2023 22:34

Thanks a lot Ankit it was very helpful. Just one question in case we have multiple column like let's say 10-15 then we need to write 10-15 case statement from row to column and 10-15 union all in case od column to row or do we have any other approach

Ответить
mallikarjun patil
mallikarjun patil - 28.01.2023 15:42

Great session 👍

Ответить
Kenneth spacebars
Kenneth spacebars - 24.01.2023 22:30

Great work Ankit so helpful thank you.

Ответить
Rutuja Kelkar
Rutuja Kelkar - 22.01.2023 17:26

Hi Ankit, Your videos are helpful to learn SQL in depth, do you have any github repository where you have hosted all your YT questions and solutions ? Or something in text/ blog format ?

Ответить