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?