1

I have a table called result that has 11 columns and look like that

Name Seat FA1 FA2 Fa3 FB1 FB2 FB3 FC1 FC2 FC3

what I am trying to do is break each row into 3 rows so the rows can be like this

Name Seat FA1 FB1 FC1

Name Seat FA2 FB2 FC2

Name Seat FA3 FB3 FC3
Dhaval
  • 2,801
  • 20
  • 39
JackNew
  • 95
  • 2
  • 11

2 Answers2

1

If the types match, you could use...

SELECT Name, Seat, FA1, FB1, FC1 from result

UNION ALL

SELECT Name, Seat, FA2, FB2, FC2 from result

UNION ALL

SELECT Name, Seat, FA3, FB3, FC3 from result

This will depend on whether you want to filter out results, you may want to only show lines where you have values in all...

SELECT Name, Seat, FA1, FB1, FC1 
from result
WHERE (FA1 IS NOT NULL AND FB1 IS NOT NULL AND FC1 IS NOT NULL)
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
1

I've found this technique - using a combination of cross apply and values to be extremely helpful - I got this from here where the author (Kenneth Fisher) argues it on performance grounds over use of unpivot. What I like most is the ease of layout control it provides and think it's easier than unpivot. It will work in SQL 2008.

SELECT
    Name
  , Seat
  , CA1.Aaisle
  , CA1.Baisle
  , CA1.Caisle
FROM UnPivotMe
CROSS APPLY (
    VALUES 
                (FA1, FB1, FC1)
              , (FA2, FB2, FC2)
              , (FA3, FB3, Fc3)
            ) AS CA1 (Aaisle, Baisle, Caisle)
;

EDIT - sorry altered aliases to something more logical

Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51