Not getting desired result using PIVOT Operator

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I have a table 'LungCapData' with 726 rows. It has 6 columns named as 'LungCap', 'Age', 'Height', 'Smoke', 'Gender', 'Caesarean'.

Below is the query to get the age average for Male and Female for yes and no category of Caesarean.

Code:
select gender, yes, no
from lungcapdata
pivot
(
avg(age)
for caesarean 
in ([yes],[no])
)as pivot1

Below is the result I am getting using above query

genderyesno
female3NULL
femaleNULL3
femaleNULL4
maleNULL3
femaleNULL5
femaleNULL3
maleNULL6
femaleNULL8
maleNULL3
femaleNULL7
femaleNULL8
male8NULL
maleNULL5
maleNULL7

<tbody>
</tbody>

My question is why is it not consolidating the results for gender. There should not be multiple results for male and female. I mean the result is not as good as we get in excel pivot tables like below

Row LabelsnoyesGrand Total
female12.3912.6712.45
male12.0212.8112.21
Grand Total12.2012.7412.33

<tbody>
</tbody>

<tbody>
</tbody>

Kindly help.

Thanks in advance!

Regards,
Shweta Jain
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Got the desired result using below query

Code:
select Gender, [yes], [no]
from 
     (select Age,Gender,Caesarean
     from lungcapdata) as sourcetable
pivot
(
avg(Age)
for Caesarean 
in ([yes],[no])
)as pivot1

Thank you:)
 
Upvote 0
Sorry..I have a little issue with this result as well.

I want to keep the average of age upto 2 decimal points only. But when I try to round it up using round() or cast() function, get an error.

Please suggest.
 
Upvote 0
Are you using SQL Server?

Code:
select Gender, round([yes],2) as [yes], round([no],2) as [no]
from 
     (select Age,Gender,Caesarean
     from #foo) as sourcetable
pivot
(
avg(Age)
for Caesarean 
in ([yes],[no])
)as pivot1
 
Upvote 0
Thanks Xenou..Yes, I am using SQL server. Let me try the one you suggested.
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,117
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top