We help IT Professionals succeed at work.
Get Started

Help with Pivot/rotate query.

Last Modified: 2015-04-24
I have a home table, and a related bed table.  Each home can have a max of 6 beds.   So,I want to end up with a once record per home, with the info for each of 6 bed possible bed in the record.  here's sample data:

home table
home#   HomeName        
100          "Jones home"
101           "smith home"

bed table
fk_home              BedNumber     BedStatus
100                             12                     vacant
100                             20                       vacant
100                              30                     occupied
101                             11                      occupied
101                             21                      vacant

I want to return 2 records like this, with 6 columns for bednumber and bedstatus, with NULLS where that bed does not exist.

home#           HomeName             Bed1Number              Bed1Status        Bed2Number                Bed2Status           etc.......
100                     "Jones home"                  12                        vacant                          20                             vacant
101                     "smith home"                  11                        occupied                      21                            vacant
Watch Question
SQL Developer
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE