如何订购 UNPIVOT

How to order UNPIVOT

我有以下 UNPIVOT 代码,我想按 FactSheetSummary 列对其进行排序,这样当它转换为行时,它的顺序为 1 - 12:

INSERT INTO #Results
SELECT DISTINCT ReportingDate, PortfolioID,ISIN, PortfolioNme, Section,REPLACE(REPLACE(Risks,'‘',''''),'’','''')
FROM  
(SELECT  DISTINCT
            ReportingDate
        ,   PortfolioID
        ,   ISIN
        ,   PortfolioNme
        ,   Section
        ,   FactSheetSummary_1, FactSheetSummary_2, FactSheetSummary_3
        ,   FactSheetSummary_4, FactSheetSummary_5, FactSheetSummary_6  
        ,   FactSheetSummary_7, FactSheetSummary_8, FactSheetSummary_9  
        ,   FactSheetSummary_10, FactSheetSummary_11, FactSheetSummary_12   
  FROM #WorkingTableFactsheet) p
UNPIVOT
(Risks FOR FactsheetSummary IN 
    (   FactSheetSummary_1, FactSheetSummary_2, FactSheetSummary_3
        ,   FactSheetSummary_4, FactSheetSummary_5, FactSheetSummary_6  
        ,   FactSheetSummary_7, FactSheetSummary_8, FactSheetSummary_9  
        ,   FactSheetSummary_10, FactSheetSummary_11, FactSheetSummary_12)
 )AS unpvt;

--DELETE records where there are no Risk Narratives
DELETE FROM #Results
WHERE Risks = ''

SELECT 
ReportingDate
,   PortfolioID
,   ISIN
,   PortfolioNme
,   Section
,   Risks   
,   ROW_NUMBER() OVER(PARTITION BY ISIN,Section ORDER BY ISIN,Section,Risks) as SortOrder
FROM #Results
order by ISIN,  Risks

这可以吗?我以为 UNPIVOT 的 IN 会决定顺序?我是否需要添加一列来指示我希望成为 1 到 12 中的哪一个?

只用case表达式来下单:

order by case FactsheetSummary  
          when 'FactSheetSummary_1' then 1
          when 'FactSheetSummary_2' then 2
          when 'FactSheetSummary_12' then 12 end