Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:35:59+00:00 2026-06-03T00:35:59+00:00

I have two tables in SQL Server: Customer and Address Customer Table : CustomerID

  • 0

I have two tables in SQL Server: Customer and Address

Customer Table:

CustomerID  FirstName  LastName
----------- ---------- ----------
1           Andrew     Jackson         
2           George     Washington

Address Table:

AddressID   CustomerID  AddressType City
----------- ----------- ----------- ----------
1           1           Home        Waxhaw     
2           1           Office      Nashville    
3           2           Home        Philadelphia

This is the output that I need:

CustomerID  Firstname  HomeCity      OfficeCity
----------- ---------- ----------    ----------
1           Andrew     Waxhaw        Nashville
2           George     Philadelphia  Null

This is my query, but not getting the right result:

SELECT CustomerID, Firstname, HOme as HomeCity, Office as OfficeCity FROM 
   (SELECT C.CustomerID, C.FirstName, A.AddressID, A.AddressType, A.City 
    FROM Customer C, Address A 
    WHERE C.CustomerID = A.CustomerID)as P
PIVOT (MAX(city) FOR AddressType in ([Home],[Office])) as  PVT

This is the result that I am getting:

CustomerID  Firstname  HomeCity      OfficeCity
----------- ---------- ----------    ----------
1           Andrew     Waxhaw        NULL
1           Andrew     NULL          Nashville
2           George     Philadelphia  Null

As you can see Customer 1 is showing up twice in the final result. Is it possible to get only one row per customer?

I looked up this example, but didn’t help:http://stackoverflow.com/questions/6267660/sql-query-to-convert-rows-into-columns

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:36:00+00:00Added an answer on June 3, 2026 at 12:36 am

    It is giving this row because you have AddressID in the select list for you subquery “P”. So even though you don’t have AddressID in you top level select this, the PIVOT function is still grouping by it. You need to change this to:

    SELECT  CustomerID, Firstname, Home as HomeCity, Office as OfficeCity 
    FROM    (   SELECT C.CustomerID, C.FirstName, A.AddressType, A.City 
                FROM #Customer C, #Address A 
                WHERE C.CustomerID = A.CustomerID
            ) AS P
            PIVOT 
            (   MAX(city) 
                FOR AddressType in ([Home],[Office])
            ) AS  PVT
    

    Although I would be inclined to use an explicit INNER JOIN rather than an implicit join between customer and Address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008 I have two tables. One table for users: id_user -
I have two basic SQL Server tables: Customer (ID [pk], AddressLine1, AddressLine2, AddressCity, AddressDistrict,
I have a SQL Server 2008 database. This database has two related tables: Customer
I have two tables in SQL Server, tbl_disputes and tbl_disputetypes . The tbl_disputes table
We have two tables in a SQL Server 2005 database, A and B.There is
Good Afternoon All, I have two tables in my SQL Server 2005 DB, Main
In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
I have two identical SQL Server tables ( SOURCE and DESTINATION ) with lots
I have SQL Server 2008 Ent and OLTP database with two big tables. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.