0

I have some data listed like the attached picture

SELECT Code, compCode FROM KitItems

The data is currently formatted like this:

enter image description here

But i want it to be transposed in to a number of columns

Like this:

enter image description here

Any ideas

2
  • Please read this: spaghettidba.com/2015/04/24/… Commented Oct 10, 2018 at 11:01
  • I have provided the script how it is now image How i want it after What have i done wrong? Commented Oct 10, 2018 at 11:05

1 Answer 1

1

You can use row_number() & do the conditional aggregation :

select code, 
       max(case when seq = 1 then compcode end) as one,
       max(case when seq = 2 then compcode end) as two,
       . . .
       max(case when seq = 7 then compcode end) as seven
from (select t.*,
             row_number() over (partition by code order by compcode) as seq
      from table t
     ) t
group by code;

Same could be achieve via dynamic PIVOT SQL if code has too many compcodes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.