0

I have a table that tests an item and stores any faliures similar to:

Item|Test|FailureValue
1   |1a  |"ZZZZZZ"
1   |1b  | 123456
2   |1a  |"MMMMMM"
2   |1c  | 111111
1   |1d  |"AAAAAA"

Is there a way in SQL to essential pivot these and have the failure values be output to individual columns? I know that I can already use STUFF to achieve what I want for the Test field but I would like the results as individual columns if possible.

I'm hoping to achieve something like:

Item|Tests |FailureValue1|FailureValue2|FailureValue3|Failure......
1   |1a,1b |"ZZZZZZ"     |123456       |NULL         |NULL   ......
2   |1a,1b |"MMMMMM"     |111111       |"AAAAAA"     |NULL   ......

Kind regards

Matt

Matt Bartlett
  • 348
  • 1
  • 3
  • 21
  • 1
    *"Is there a way in SQL to essential pivot"* -- Yes, and rather appropriately you use [`pivot`](https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017) – iamdave Oct 05 '18 at 10:04
  • 1
    Possible duplicate of [Convert Rows to columns using 'Pivot' in SQL Server](https://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql-server) – Eric Brandt Oct 05 '18 at 14:16
  • The duplicate flag isn't necessarily a bad thing. It just let's anyone who finds this question know where you found your answer. Well done doing the research and getting to your answer. – Eric Brandt Oct 05 '18 at 14:18

0 Answers0