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

  • Home
  • SEARCH
  • 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 85469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:05:39+00:00 2026-05-10T22:05:39+00:00

I have a simple problem when querying the SQL Server 2005 database. I have

  • 0

I have a simple problem when querying the SQL Server 2005 database. I have tables called Customer and Products (1->M). One customer has most 2 products. Instead of output as

CustomerName, ProductName …

I like to output as

CustomerName, Product1Name, Product2Name …

Could anybody help me?

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. 2026-05-10T22:05:40+00:00Added an answer on May 10, 2026 at 10:05 pm

    Like others have said, you can use the PIVOT and UNPIVOT operators. Unfortunately, one of the problems with both PIVOT and UNPIVOT are that you need to know the values you will be pivoting on in advance or else use dynamic SQL.

    It sounds like, in your case, you’re going to need to use dynamic SQL. To get this working well you’ll need to pull a list of the products being used in your query. If you were using the AdventureWorks database, your code would look like this:

    USE AdventureWorks; GO  DECLARE @columns NVARCHAR(MAX);  SELECT x.ProductName INTO #products FROM (SELECT p.[Name] AS ProductName     FROM Purchasing.Vendor AS v     INNER JOIN Purchasing.PurchaseOrderHeader AS poh ON v.VendorID = poh.VendorID     INNER JOIN Purchasing.PurchaseOrderDetail AS pod ON poh.PurchaseOrderID = pod.PurchaseOrderID     INNER JOIN Production.Product AS p ON pod.ProductID = p.ProductID     GROUP BY p.[Name]) AS x;  SELECT @columns = STUFF(     (SELECT ', ' + QUOTENAME(ProductName, '[') AS [text()]        FROM #products FOR XML PATH ('')     ), 1, 1, '');  SELECT @columns; 

    Now that you have your columns, you can pull everything that you need pivot on with a dynamic query:

    DECLARE @sql NVARCHAR(MAX);  SET @sql = 'SELECT CustomerName, ' + @columns + ' FROM (     // your query goes here ) AS source PIVOT (SUM(order_count) FOR product_name IN (' + @columns + ') AS p';  EXEC sp_executesql @sql 

    Of course, if you need to make sure you get decent values, you may have to duplicate the logic you’re using to build @columns and create an @coalesceColumns variable that will hold the code to COALESCE(col_name, 0) if you need that sort of thing in your query.

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I found the solution myself. Override the ArrangeOverride method like… May 11, 2026 at 11:38 am
  • added an answer Implement the OnDataBinding event for the button. In the event,… May 11, 2026 at 11:38 am
  • added an answer A very good place to read up is OWASP; see… May 11, 2026 at 11:38 am

Related Questions

I have a simple problem when querying the SQL Server 2005 database. I have
I have a very simple problem which requires a very quick and simple solution
I have a very simple problem and a solution that will work, but I'm
I have a seemingly simple problem though i am unable to get my head
I have a fairly simple sync problem. I have a table with about 10
I have a problem with a simple included file. The file being included is
I have a simple, real life problem I want to solve using an OO
Seems like a simple problem: I have an SVN repo inside our firewall. I
Problem 1: I have a simple winforms app and I want to DataBind my
Ok, simple problem hopefully. I have started to make a simple server for a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.