-1
我有一個Y和N的「矩陣」,請參閱下面的圖片,Id lile是下面的圖像,它被展平爲1行,每個SID顯示所有在一行上的Y和像我這樣的N是對行中的數據進行ORing。我可以在SQL Server中做到嗎?SQL Server:試圖將一個「矩陣」拼成一行
這是我原來的查詢
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上看到一個錯誤請參閱下面的圖片
由於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
在發佈之前,您曾研究過哪些內容? – dfundako
我試着使用pivot並將'Y'和'N'轉換爲0和1 ... stull正在工作,希望有一種方法可以將它們組合在一起 –