2014-10-22 33 views
2

我目前正在爲客戶端創建一個導出視圖,並且他們請求了一個具有附加列的數據透視表,後面跟着第一個透視列以添加額外的詳細信息。如何以嵌套方式將2個或更多值轉換爲

什麼我已經是這樣的:

SAMPLE_TAG SAMPLE_TYPE LAB_METHOD LAB_ELEMENT LAB_RESULT_NUMERIC 
MCR0004140 OR    ME-MS41  Ag   0.03 
MCR0004140 OR    ME-MS41  Au   -0.2 
MCR0004140 OR    ME-MS41  Cu   15.3 
MCR0004140 OR    ME-MS41  Zn   19 
MCR0005015 OR    AG-OG46  Ag   421 
MCR0005015 OR    ME-MS41  Au   -0.2 
MCR0005015 OR    ME-MS41  Cu   30.7 
MCR0005015 OR    ME-MS41  Zn   15 

我要實現這一點:

SAMPLE_TAG SAMPLE_TYPE  Ag  Ag_Method Au  Au_Method Cu  Cu_Method Zn Zn_Method 
MCR0004140 OR    0.03 ME-MS41  -0.2 ME-MS41 15.3  ME-MS41  19 ME-MS41 
MCR0005015 OR    421  AG-OG46  -0.2 ME-MS41 30.7  ME-MS41  15 ME-MS41 

我能得到第一樞軸使用工作如下:

SELECT * 

FROM TEMP_PIVOT 

PIVOT (
     MAX(LAB_RESULT_NUMERIC) 
     FOR LAB_ELEMENT IN ([Au],[Ag],[Cu],[Zn],[Pt])) AS RESULT 

然而,這是我卡住的地方。

任何幫助或建議將不勝感激。我重視下創建表/插入腳本:

謝謝:)


CREATE TABLE [dbo].[TEMP_PIVOT](
[LAB_ID] [nvarchar](12) NOT NULL, 
[DESPATCH_ID] [nvarchar](30) NULL, 
[LAB_JOB_NO] [nvarchar](20) NULL, 
[PROJECT] [nvarchar](16) NULL, 
[SITE_ID] [nvarchar](16) NULL, 
[SAMPLE_TAG] [nvarchar](16) NULL, 
[SAMPLE_TYPE] [nvarchar](8) NULL, 
[LAB_METHOD] [nvarchar](30) NOT NULL, 
[LAB_ELEMENT] [nvarchar](8) NULL, 
[LAB_RESULT_NUMERIC] [float] NULL) ON [PRIMARY] 



INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0004140', N'OR', N'ME-MS41', N'Ag', 0.03) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0004140', N'OR', N'ME-MS41', N'Au', -0.2) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0004140', N'OR', N'ME-MS41', N'Cu', 15.3) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0004140', N'OR', N'ME-MS41', N'Zn', 19) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0005015', N'OR', N'AG-OG46', N'Ag', 421) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0005015', N'OR', N'ME-MS41', N'Au', -0.2) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0005015', N'OR', N'ME-MS41', N'Cu', 30.7) 

INSERT [dbo].[TEMP_PIVOT] ([LAB_ID], [DESPATCH_ID], [LAB_JOB_NO], [PROJECT], [SITE_ID], [SAMPLE_TAG], [SAMPLE_TYPE], [LAB_METHOD], [LAB_ELEMENT], [LAB_RESULT_NUMERIC]) VALUES (N'ALS_MDZ', N'MIR00717', N'AN14036339', N'GORBEA', N'POINT_SAMPLE', N'MCR0005015', N'OR', N'ME-MS41', N'Zn', 15) 

回答

0

如果你希望你的查詢工作動態試試這個

Fiddle demo here

DECLARE @L_ELEMENT  VARCHAR(max)='', 
     @aggL_ELEMENTMTD VARCHAR(max)='', 
     @L_ELEMENTMTD VARCHAR(max)='', 
     @aggL_ELEMENT VARCHAR(max)='', 
     @sql    NVARCHAR(max) 

SET @L_ELEMENT = (SELECT DISTINCT Quotename(lab_element) + ',' 
       FROM temp_pivot 
       FOR xml path('')) 
SET @aggL_ELEMENTMTD = (SELECT DISTINCT ' max(' 
            + Quotename(lab_element+'_Method') + 
            ') ' 
            + Quotename(lab_element+'_Method') + ',' 
        FROM temp_pivot 
        FOR xml path('')) 
SET @L_ELEMENTMTD = (SELECT DISTINCT Quotename(lab_element+'_Method') + ',' 
       FROM temp_pivot 
       FOR xml path('')) 
SET @aggL_ELEMENT = (SELECT DISTINCT ' max(' + Quotename(lab_element) + ') ' 
           + Quotename(lab_element) + ',' 
       FROM temp_pivot 
       FOR xml path('')) 

SELECT @L_ELEMENTMTD = LEFT(@L_ELEMENTMTD, Len(@L_ELEMENTMTD) - 1) 

SELECT @L_ELEMENT = LEFT(@L_ELEMENT, Len(@L_ELEMENT) - 1) 

SELECT @aggL_ELEMENT = LEFT(@aggL_ELEMENT, Len(@aggL_ELEMENT) - 1) 

SELECT @aggL_ELEMENTMTD = LEFT(@aggL_ELEMENTMTD, Len(@aggL_ELEMENTMTD) - 1) 

SET @sql = 'SELECT SAMPLE_TAG,SAMPLE_TYPE,' + @aggL_ELEMENT + ',' 
     + @aggL_ELEMENTMTD + ' FROM (select LAB_ELEMENT+''_Method'' L_ELEMENTMTD,* 
        FROM TEMP_PIVOT) AS T 
PIVOT (max(LAB_RESULT_NUMERIC) FOR LAB_ELEMENT 
IN  (' + @L_ELEMENT + ')) AS P1  
PIVOT  (max(LAB_METHOD) FOR L_ELEMENTMTD 
IN  (' + @L_ELEMENTMTD + ')) AS P1   
      group by SAMPLE_TAG, SAMPLE_TYPE' 

--PRINT @sql 
EXEC Sp_executesql 
    @sql 
+0

感謝Pradeep,精美的作品!該解決方案完全避免了表格功能。非常感謝你的幫助:) – Ibo 2014-10-22 05:10:47

+0

@Ibo快樂它幫助乾杯:) – 2014-10-22 05:12:37

0

僅供參考(並且如果你沒有,如果解不繞樞軸在意,但讓你的業務問題的結果),這是一個快速的選擇和簡單。

SELECT 
    SAMPLE_TAG, 
    SAMPLE_TYPE, 
    MAX(CASE WHEN LAB_ELEMENT = 'Ag' THEN LAB_RESULT_NUMERIC ELSE NULL END) AS [Ag], 
    MAX(CASE WHEN LAB_ELEMENT = 'Ag' THEN LAB_METHOD ELSE NULL END) AS [Ag_Method], 
    MAX(CASE WHEN LAB_ELEMENT = 'Au' THEN LAB_RESULT_NUMERIC ELSE NULL END) AS [Au], 
    MAX(CASE WHEN LAB_ELEMENT = 'Au' THEN LAB_METHOD ELSE NULL END) AS [Au_Method], 
    MAX(CASE WHEN LAB_ELEMENT = 'Cu' THEN LAB_RESULT_NUMERIC ELSE NULL END) AS [Cu], 
    MAX(CASE WHEN LAB_ELEMENT = 'Cu' THEN LAB_METHOD ELSE NULL END) AS [Cu_Method], 
    MAX(CASE WHEN LAB_ELEMENT = 'Zn' THEN LAB_RESULT_NUMERIC ELSE NULL END) AS [Zn], 
    MAX(CASE WHEN LAB_ELEMENT = 'Zn' THEN LAB_METHOD ELSE NULL END) AS [Zn_Method], 
    MAX(CASE WHEN LAB_ELEMENT = 'Pt' THEN LAB_RESULT_NUMERIC ELSE NULL END) AS [Pt], 
    MAX(CASE WHEN LAB_ELEMENT = 'Pt' THEN LAB_METHOD ELSE NULL END) AS [Pt_Method] 
FROM dbo.TEMP_PIVOT 
GROUP BY 
    SAMPLE_TAG, 
    SAMPLE_TYPE 

輸出

SAMPLE_TAG  SAMPLE_TYPE Ag      Ag_Method      Au      Au_Method      Cu      Cu_Method      Zn      Zn_Method      Pt      Pt_Method 
---------------- ----------- ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ 
MCR0004140  OR   0.03     ME-MS41      -0.2     ME-MS41      15.3     ME-MS41      19      ME-MS41      NULL     NULL 
MCR0005015  OR   421     AG-OG46      -0.2     ME-MS41      30.7     ME-MS41      15      ME-MS41      NULL     NULL 
+0

感謝您的答覆傑森, 我創建樞軸動態,但沒有想到使用聚合方法。我會將其從動態數據透視表改爲使用聚合案例。應該在理論上工作。 我會給它一個去讓你知道。 再次感謝:) – Ibo 2014-10-22 03:18:44

0

這裏是與多個樞軸

;WITH E AS (
    SELECT 
     SAMPLE_TAG, 
     SAMPLE_TYPE, 
     LAB_ELEMENT AS E1, 
     LAB_ELEMENT + '_Method' AS E2, 
     LAB_RESULT_NUMERIC, 
     LAB_METHOD 
    FROM dbo.TEMP_PIVOT 
) 
    SELECT 
     SAMPLE_TAG, 
     SAMPLE_TYPE, 
     MAX([Ag]) AS [Ag], 
     MAX([Ag_Method]) AS [Ag_Method], 
     MAX([Au]) AS [Au], 
     MAX([Au_Method]) AS [Au_Method], 
     MAX([Cu]) AS [Cu], 
     MAX([Cu_Method]) AS [Cu_Method], 
     MAX([Zn]) AS [Zn], 
     MAX([Zn_Method]) AS [Zn_Method], 
     MAX([Pt]) AS [Pt], 
     MAX([Pt_Method]) AS [Pt_Method] 
    FROM E 
     PIVOT (MAX(LAB_RESULT_NUMERIC) FOR E1 IN ([Au],[Ag],[Cu],[Zn],[Pt])) AS R1 
     PIVOT (MAX(LAB_METHOD) FOR E2 IN ([Au_Method],[Ag_Method],[Cu_Method],[Zn_Method],[Pt_Method])) AS R2 
    GROUP BY 
     SAMPLE_TAG, 
     SAMPLE_TYPE 
    ORDER BY 
     SAMPLE_TAG, 
     S 

和輸出的版本是一樣的

SAMPLE_TAG  SAMPLE_TYPE Ag      Ag_Method      Au      Au_Method      Cu      Cu_Method      Zn      Zn_Method      Pt      Pt_Method 
---------------- ----------- ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ ---------------------- ------------------------------ 
MCR0004140  OR   0.03     ME-MS41      -0.2     ME-MS41      15.3     ME-MS41      19      ME-MS41      NULL     NULL 
MCR0005015  OR   421     AG-OG46      -0.2     ME-MS41      30.7     ME-MS41      15      ME-MS41      NULL     NULL