簡體   English   中英

將 SQL 服務器表轉換為更規范化的版本

[英]Pivoting a SQL Server table into a more normalized version

pivot(或取消透視)下表的最佳方法是什么:

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)
);

並將其轉換為如下模式:

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
);

所以Sections表可以有這樣的示例數據:

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

這需要變成這樣:

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

在 SQL 服務器中,有沒有一種快速的方法可以將其加載到帶有一些花哨的非游標 T-SQL 的臨時表中?

如果這是一次性工作,請嘗試UNPIVOT將 SubSectionId..N 列轉換為行。 然后在結果中添加一個ROW_NUMBER()以按部分和小節生成訂單號

-- 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, '') <> ''

結果:

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

另見db<>fiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM