1

I needed help with pivoting a table. This is my first attempt.

I have extracted data into a #temp table. I have two columns, in my #temp table, Customer Nr and product

One customer number can have more than one 1 product. How do I show, for one customer number, up to 3 products?

This is my very first attempt:

SELECT DISTINCT
    cust_no,
    products
FROM 
    #temp
PIVOT 
    (cust_no, 
     CASE WHEN [Product 1] IS NULL THEN NULL ELSE [Product 2] END AS [Product 2],
     CASE WHEN [Product 2] IS NULL THEN NULL ELSE [Product 3] END AS [Product 3])

I got stuck here. I am working in SQL Server 2012

1
  • I have a follow-up question as it made me try a new thing. If I had in a table ,cust_no,product and serviceid, The ServiceId corresponding to each product. How can I make sure the service ID is next to the respective product number? I basically copy pasted the row_number() line and modified it to include serviceid. The problem is that, it requires me to includes an aggregate function to allow the query to work. I tried doing MAX(serviceid) and it does end next to the productname, however it is for some other product. Any hints? Mar 19, 2015 at 15:15

1 Answer 1

2

You don't really need to use the PIVOT function to get the result. You could use an aggregate function with a CASE expression, along with a windowing function like row_number() to convert the data from rows into columns. The windowing function will generate a unique number for each product within a customer. This number is then used to place the value into each of the columns.

Here is some sample data:

CREATE TABLE temp
    ([CustomerNr] int, [Product] varchar(9));

INSERT INTO temp
    ([CustomerNr], [Product])
VALUES
    (1234, 'Rice'),
    (1234, 'Tuna'),
    (1234, 'Sauce'),
    (2345, 'Jam'),
    (3456, 'Spaghetti'),
    (3456, 'Jelly');

Using an aggregate function and a CASE expression your code would be:

select CustomerNr,
  Product1 = max(case when rn = 1 then Product else null end),
  Product2 = max(case when rn = 2 then Product else null end),
  Product3 = max(case when rn = 3 then Product else null end)
from
(
  select CustomerNr, Product,
    rn = row_number() over(partition by customerNr order by product)
  from temp
) d
group by CustomerNr;

See SQL Fiddle with Demo.

But if you wanted to use the PIVOT function you can you'll just still need to use a windowing function to help determine the number of products for each Customer:

select CustomerNr, Product1, Product2, Product3
from
(
  select CustomerNr, Product,
    new_col = 'Product'
                +cast(row_number() over(partition by customerNr order by product) as varchar(1))
  from temp
) d
pivot
(
  max(product) 
  for new_col in (Product1, Product2, Product3)
) piv;

See SQL Fiddle with Demo. Either version will give a result:

| CustomerNr | Product1 |  Product2 | Product3 |
|------------|----------|-----------|----------|
|       1234 |     Rice |     Sauce |     Tuna |
|       2345 |      Jam |    (null) |   (null) |
|       3456 |    Jelly | Spaghetti |   (null) |
1
  • 1
    Wow! Thank you for helping and teaching the easier way of doing it:=) I completely forgot my old friend , ROW_NUMBER() OVER. Much appreciated.! Mar 19, 2015 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.