2017-01-31 60 views
-1

我有一個Y和N的「矩陣」,請參閱下面的圖片,Id lile是下面的圖像,它被展平爲1行,每個SID顯示所有在一行上的Y和像我這樣的N是對行中的數據進行ORing。我可以在SQL Server中做到嗎?SQL Server:試圖將一個「矩陣」拼成一行

enter image description here

enter image description here

這是我原來的查詢

SELECT TERM_SID, 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data' THEN 'Y' ELSE 'N' END AS 'PUBLIC', 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data in aggregate' THEN 'Y' ELSE 'N' END as 'PUBLIC_AGGREGATE', 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data' THEN 'Y' ELSE 'N' END as 'INTERNAL', 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data in aggregate' THEN 'Y' ELSE 'N' END as 'INTERNAL_AGGREGATE', 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Limited data' THEN 'Y' ELSE 'N' END as 'LIMITED', 
    CASE WHEN d2.DATA_CLASSIFICATION_DESC='Restricted data' THEN 'Y' ELSE 'N' END as 'RESTRICTED' 
    FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2 
    JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 ON dm2.DATA_CLASSIFICATION_SID=d2.DATA_CLASSIFICATION_SID order by TERM_SID 

以及我是如何試圖將 'Y'= 1 'N'= 0

後總結各列
select * from 
     (
     SELECT TERM_SID, 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data' THEN 1 ELSE 0 END AS 'PUBLIC', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data in aggregate' THEN 1 ELSE 0 END as 'PUBLIC_AGGREGATE', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data' THEN 1 ELSE 0 END as 'INTERNAL', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data in aggregate' THEN 1 ELSE 0 END as 'INTERNAL_AGGREGATE', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Limited data' THEN 1 ELSE 0 END as 'LIMITED', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Restricted data' THEN 1 ELSE 0 END as 'RESTRICTED' 
     FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2 
     JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 ON dm2.DATA_CLASSIFICATION_SID=d2.DATA_CLASSIFICATION_SID 
    ) x 
     PIVOT 
     (sum (x.PUBLIC) for TERM_SID in (*)) as piv 

但我在x.PUBLIC上看到一個錯誤請參閱下面的圖片

enter image description here

由於SqlZim這裏是我想出了

select TERM_SID 
,max(PUBLICDATA) as PublicData 
,max(PUBLIC_AGGREGATE) as Public_AGGregate 
,max(INTERNAL) as Internal 
,max(INTERNAL_AGGREGATE) as internal_Aggregate 
,max(LIMITED) as Limited 
,max(RESTRICTED) as Restricted 
from 
(
     SELECT TERM_SID, 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data' THEN 'Y' ELSE 'N' END AS 'PUBLICDATA', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data in aggregate' THEN 'Y' ELSE 'N' END as 'PUBLIC_AGGREGATE', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data' THEN 'Y' ELSE 'N' END as 'INTERNAL', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data in aggregate' THEN 'Y' ELSE 'N' END as 'INTERNAL_AGGREGATE', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Limited data' THEN 'Y' ELSE 'N' END as 'LIMITED', 
     CASE WHEN d2.DATA_CLASSIFICATION_DESC='Restricted data' THEN 'Y' ELSE 'N' END as 'RESTRICTED' 
     FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2 
     JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 ON dm2.DATA_CLASSIFICATION_SID=d2.DATA_CLASSIFICATION_SID 
) mat 
group by Term_SID 

也是這個作品使用CTE格式建議

with mat (TERM_SID,PUBLICDATA,PUBLIC_AGGREGATE,INTERNAL,INTERNAL_AGGREGATE,LIMITED,RESTRICTED) 
as 
(
     SELECT dm2.TERM_SID 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Public data' THEN 'Y' ELSE 'N' END AS PUBLICDATA 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Public data in aggregate' THEN 'Y' ELSE 'N' END as PUBLIC_AGGREGATE 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Internal data' THEN 'Y' ELSE 'N' END as INTERNAL 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Internal data in aggregate' THEN 'Y' ELSE 'N' END as INTERNAL_AGGREGATE 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Limited data' THEN 'Y' ELSE 'N' END as LIMITED 
      ,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Restricted data' THEN 'Y' ELSE 'N' END as RESTRICTED 
     FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2 
      JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 
       ON dm2.DATA_CLASSIFICATION_SID = d2.DATA_CLASSIFICATION_SID 
) 
select m1.TERM_SID 
     ,max(m1.PUBLICDATA) as PublicData 
     ,max(m1.PUBLIC_AGGREGATE) as Public_AGGregate 
     ,max(m1.INTERNAL) as Internal 
     ,max(m1.INTERNAL_AGGREGATE) as internal_Aggregate 
     ,max(m1.LIMITED) as Limited 
     ,max(m1.RESTRICTED) as Restricted 
     ,count(1) as mat2count 
from mat m1 
    left join mat m2 
     on(m1.TERM_SID = m2.TERM_SID) 
group by m1.Term_SID 
+1

在發佈之前,您曾研究過哪些內容? – dfundako

+0

我試着使用pivot並將'Y'和'N'轉換爲0和1 ... stull正在工作,希望有一種方法可以將它們組合在一起 –

回答

2

我不知道是怎麼回事您的第二張圖片中有兩排爲Term_SID=3,但這會讓您更接近:

;with mat as (
    select 
     term_sid 
    , [Public] = case 
     when d2.data_classification_desc='Public data' 
      then 'Y' else 'N' end 
    , [Public_Aggregate] = case 
     when d2.data_classification_desc='Public data in aggregate' 
      then 'Y' else 'N' end 
    , [Internal] = case 
     when d2.data_classification_desc='Internal data' 
      then 'Y' else 'N' end 
    , [Internal_Aggregate] = case 
     when d2.data_classification_desc='Internal data in aggregate' 
      then 'Y' else 'N' end 
    , [Limited] = case 
     when d2.data_classification_desc='Limited data' 
      then 'Y' else 'N' end 
    , [Restricted] = case 
     when d2.data_classification_desc='Restricted data' 
      then 'Y' else 'N' end 
    from [udw_data_dictionary].[dbo].[data_classification] d2 
     join [udw_data_dictionary].[dbo].[data_classification_mapping] dm2 
     on dm2.data_classification_sid=d2.data_classification_sid 
) 

select 
     Term_SID 
    , Public    = max(Public) 
    , Public_Aggregate = max(Public_Aggregate) 
    , Internal   = max(Internal) 
    , Internal_Aggregate = max(Internal_Aggregate) 
    , Limited   = max(Limited) 
    , Restricted   = max(Restricted) 
    from mat 
    group by Term_SID 
    order by term_sid 
相關問題