0

I have a table with rows like below

StoreId   | ToyId
-------------------
100       | Toy1
100       | Toy2
101       | Toy1
102       | Toy1
103       | Toy1
103       | Toy2
103       | Toy3

The number of ToyID per store is not limited to 3. A store can have one or many toy ids. I want to get result as:

StoreId | ToyId1 | ToyId2 | ToyId3
-----------------------------------
100     | Toy1   | Toy2   | NULL   
101     | Toy1   | NULL   | NULL
102     | Toy1   | NULL   | NULL
103     | Toy1   | Toy2   | Toy3

How can i achieve in SQL? It is not entire row to column pivoting, The rows dynamically get added based on number of unique rows of one column only.

Aruna
  • 155
  • 8
  • 20
  • This question has been answered numerous times here in SO. Please do some research before writing a new post. – Giorgos Betsos May 17 '17 at 07:14
  • I have gone through the original link and other links. First of all, I do not want entire pivoting.All answers posted have fixed rows (3), my rows are dynamic. Hence I consider this as a new question – Aruna May 17 '17 at 07:55
  • The answer by @bluefeet in the link of the duplicate answer also addresses the issue of dynamic *columns*. The number of rows is irrelevant in this case. – Giorgos Betsos May 17 '17 at 07:59
  • Can you please test it once before commenting... It moves the entire table as row with one of the columns as column name. I do not want this. I want to make only one column to be converted to row based on a ID not the ENTIRE table – Aruna May 17 '17 at 08:05
  • @Aruna Did find the anwer as you required –  May 17 '17 at 08:51
  • No. i am still searching – Aruna May 17 '17 at 09:12
  • @Aruna i have answer but how i could i send you –  May 17 '17 at 14:15
  • According to @GiorgosBetsos i tried with pivot but it will work only if column name is same as column data. But that is not my case. I have different data and column names are in no relation with the column data. – Aruna May 18 '17 at 05:33

0 Answers0