简体   繁体   中英

Pivoting a SQL Server table into a more normalized version

What's the best way to pivot (or un-pivot) the table below:

CREATE TABLE dbo.Sections(
    SectionId varchar(50) NOT NULL (PK),
    Description varchar(255),
    SubSectionIdA varchar(50),
    SubSectionIdB varchar(50),
    SubSectionIdC varchar(50),
    SubSectionIdD varchar(50),
    SubSectionIdE varchar(50)
);

and turn it into a schema like so:

CREATE TABLE dbo.NormalizedSections(
    SectionId varchar(50) NOT NULL (FK and part of PK),
    Description varchar(255),
    SubSectionId varchar(50) NOT NULL (other part of PK),
    Order int
);

So the Sections table can have sample data like so:

SectionId   Description     SubSectionIdA       SubSectionIdB       SubSectionIdC       SubSectionIdD       SubSectionIdE
--------------------------------------------------------------------------------------------------------------------------------------------------------
Sec-01A     Special section     NULL            ''          SubsectionA1        SubsectionA2        ''
Sec-02B     Cheap seats     CheapSeciton1       ''          CheapSectionTop     NULL            LimitedView     
Sec-01B     VIP         Special         CourtsideSeatView   NULL            NULL            NULL

This needs to be turned into this:

SectionId   Description     SubSectionId        Order
-------------------------------------------------------------------------------
Sec-01A     Special section     SubsectionA1        1
Sec-01A     Special section     SubsectionA2        2
Sec-02B     Cheap seats     CheapSeciton1       1
Sec-02B     Cheap seats     CheapSectionTop     2
Sec-02B     Cheap seats     LimitedView     3       
Sec-01B     VIP         Special         1
Sec-01B     VIP         CourtsideSeatView   2

Is there a quick way to do this and load it into a temp table with some fancy non-cursor T-SQL in SQL Server?

If it's a one off job, try UNPIVOT to convert the SubSectionId..N columns into rows. Then add a ROW_NUMBER() to the result to generate the order numbers by section and subsection

-- Unpivot the table.  
SELECT unpvt.SectionId
       , unpvt.Description
       , unpvt.SubSection
       , ROW_NUMBER() OVER(PARTITION BY SectionId ORDER BY SubSection) AS RowOrder
FROM   
   ( SELECT SectionId
            , Description
            , SubSectionIdA
            , SubSectionIdB
            , SubSectionIdC
            , SubSectionIdD
            , SubSectionIdE
     FROM   Sections
   ) p  
   UNPIVOT  
   (
      SubSection FOR SubSectionId 
        IN (SubSectionIdA
            , SubSectionIdB
            , SubSectionIdC
            , SubSectionIdD
            , SubSectionIdE
         )  
)AS unpvt
WHERE ISNULL(SubSection, '') <> ''

Results:

SectionId | Description     | SubSection        | RowOrder
:-------- | :-------------- | :---------------- | -------:
Sec-01A   | Special section | SubsectionA1      |        1
Sec-01A   | Special section | SubsectionA2      |        2
Sec-01B   | VIP             | CourtsideSeatView |        1
Sec-01B   | VIP             | Special           |        2
Sec-02B   | Cheap seats     | CheapSeciton1     |        1
Sec-02B   | Cheap seats     | CheapSectionTop   |        2
Sec-02B   | Cheap seats     | LimitedView       |        3

See also db<>fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM