将多列反透视成行
Posted
技术标签:
【中文标题】将多列反透视成行【英文标题】:Unpivot Multiple Columns into Rows 【发布时间】:2019-03-27 19:52:39 【问题描述】:我正在处理审计日志报告,用户希望查看在创建记录时插入的原始值 - 但不幸的是,我们的审计日志只是将记录存储为“已创建记录”,但记录并没有包含插入的值。请看下面的截图:
所以我最初只知道该认证是在 2019 年 3 月 14 日插入的 - 对于我想要做的事情并不理想,但它就是这样。
我有兴趣从该表中找到四列的插入值:
CertificationTypeID 证书标识符 StateCertificationIssued 颁发日期认证使用审核日志和认证表,我能够重建插入记录时的原始值 - 但认证表中的每一列都由我的审核日志记录集中的四列表示:
原始值 字段名称 行动日期 记录创建我希望完成的是将包含原始值记录的列取消透视(或交叉应用)到代表插入的每一列的行中。
例如,原始记录的前四列是 CT_OriginalValue、CT_FieldName、CT_ActionDate 和 CT_RecordCreation。我需要将它们变成四行,以替换第一个不包含任何信息的“已创建记录”。
理想情况下 - CT_FieldName 和 CT_RecordCreation 将被合并并反透视到一行并落在 ColumnName 下,CT_OriginalValue 将反透视到 NewValue 列,CT_ActionDate 将反透视到 DateChanged。
我尝试过UNPIVOT
,但运气不佳,我已经看到CROSS APPLY
在这些情况下使用,但我不知道如何应用于这种情况。
示例数据如下:
CREATE TABLE #AuditLogCustomerCert
(
ColumnName NVARCHAR (128) ,
OldValue VARCHAR (MAX) ,
NewValue VARCHAR (MAX) ,
DateChanged DATETIME2 (7) ,
ActionType CHAR (1) ,
AuditLogPK VARCHAR (400) ,
CT_OriginalValue VARCHAR (200) ,
CT_FieldName VARCHAR (19) ,
CT_ActionDate DATETIME2 (7) ,
CT_RecordCreation VARCHAR (14) ,
CI_OriginalValue VARCHAR (MAX) ,
CI_FieldName VARCHAR (21) ,
CI_ActionDate DATETIME2 (7) ,
CI_RecordCreation VARCHAR (14) ,
SCI_OriginalValue VARCHAR (MAX) ,
SCI_FieldName VARCHAR (24) ,
SCI_ActionDate DATETIME2 (7) ,
SCI_RecordCreation VARCHAR (14) ,
DCI_OriginalValue DATETIME ,
DCI_FieldName VARCHAR (23) ,
DCI_ActionDate DATETIME2 (7) ,
DCI_RecordCreation VARCHAR (14)
);
INSERT INTO #AuditLogCustomerCert
VALUES ( N'Record Created', NULL, NULL, N'2019-03-14T10:55:06.0014212', 'I', '<CustomerCertificationID=327857>', 'Hunter Safety Certificate', 'CertificationTypeID', N'2019-03-14T10:55:06.0014212', 'Record Created', 'IA299038794' ,
'CertificateIdentifier' , N'2019-03-14T10:55:06.0014212', 'Record Created', '66', 'StateCertificationIssued', N'2019-03-14T10:55:06.0014212', 'Record Created', N'2019-02-28T00:00:00', 'DateCertificationIssued' ,
N'2019-03-14T10:55:06.0014212' , 'Record Created' ) ,
( N'StateCertificationIssued', '66', 'UA', N'2019-03-14T13:04:45.1401862', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 14 2019 10:55AM', 'Mar 19 2019 1:52PM', N'2019-03-19T13:52:29.5221885', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 19 2019 1:52PM', 'Mar 22 2019 10:43AM', N'2019-03-22T10:43:42.1068855', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 22 2019 10:43AM', 'Mar 22 2019 2:38PM', N'2019-03-22T14:38:57.5266904', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'StateCertificationIssued', 'UA', 'NJ', N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'CountryCertificationIssued', 'Gabon ', 'United States ', N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 22 2019 2:38PM', 'Mar 26 2019 2:23PM', N'2019-03-26T14:23:27.8660009', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'Record Created', NULL, NULL, N'2019-03-26T10:30:19.3982917', 'I', '<CustomerCertificationID=327877>', 'Boater Safety Certificate', 'CertificationTypeID', N'2019-03-26T10:30:19.3982917', 'Record Created', 'IAC031900001' ,
'CertificateIdentifier' , N'2019-03-26T10:30:19.3982917', 'Record Created', 'IA', 'StateCertificationIssued', N'2019-03-26T10:30:19.3982917', 'Record Created', N'2019-03-07T00:00:00', 'DateCertificationIssued' ,
N'2019-03-26T10:30:19.3982917' , 'Record Created' ) ,
( N'CertificateIdentifier', 'IAC031900001', 'IAC031900002', N'2019-03-26T22:02:21.3292077', 'U', '<CustomerCertificationID=327877>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'Record Created', NULL, NULL, N'2019-03-26T22:12:29.4201277', 'I', '<CustomerCertificationID=327887>', 'Temporarily Disabled', 'CertificationTypeID', N'2019-03-26T22:12:29.4201277', 'Record Created', '', 'CertificateIdentifier' ,
N'2019-03-26T22:12:29.4201277' , 'Record Created', 'IA', 'StateCertificationIssued', N'2019-03-26T22:12:29.4201277', 'Record Created', N'2019-03-13T00:00:00', 'DateCertificationIssued', N'2019-03-26T22:12:29.4201277' ,
'Record Created' ) ,
( N'CertificateIdentifier', '', 'IAC9985478', N'2019-03-26T22:12:47.3471693', 'U', '<CustomerCertificationID=327887>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
SELECT * FROM #AuditLogCustomerCert;
--SELECT ColumnName, up.NewValue
--FROM (
--SELECT CONCAT(ColumnName,'-',CT_FieldName), CT_OriginalValue
--FROM #AuditLogCustomerCert
--) AS cp
--UNPIVOT
--(
--NewValue FOR NewValue IN ( CT_OriginalValue )) AS up;
DROP TABLE IF EXISTS #AuditLogCustomerCert;
【问题讨论】:
【参考方案1】:您需要的“UNPIVOT”以及与其他人的“UNION ALL”怎么样?而且我不关心行的顺序。
IF OBJECT_ID('tempdb..#AuditLogCustomerCert') IS NOT NULL
DROP TABLE #AuditLogCustomerCert;
CREATE TABLE #AuditLogCustomerCert(
ColumnName NVARCHAR (128) ,
OldValue VARCHAR (MAX) ,
NewValue VARCHAR (MAX) ,
DateChanged DATETIME2 (7) ,
ActionType CHAR (1) ,
AuditLogPK VARCHAR (400) ,
CT_OriginalValue VARCHAR (200) ,
CT_FieldName VARCHAR (19) ,
CT_ActionDate DATETIME2 (7) ,
CT_RecordCreation VARCHAR (14) ,
CI_OriginalValue VARCHAR (MAX) ,
CI_FieldName VARCHAR (21) ,
CI_ActionDate DATETIME2 (7) ,
CI_RecordCreation VARCHAR (14) ,
SCI_OriginalValue VARCHAR (MAX) ,
SCI_FieldName VARCHAR (24) ,
SCI_ActionDate DATETIME2 (7) ,
SCI_RecordCreation VARCHAR (14) ,
DCI_OriginalValue DATETIME ,
DCI_FieldName VARCHAR (23) ,
DCI_ActionDate DATETIME2 (7) ,
DCI_RecordCreation VARCHAR (14)
);
INSERT #AuditLogCustomerCert
VALUES (N'Record Created', NULL, NULL, N'2019-03-14T10:55:06.0014212', 'I',
'<CustomerCertificationID=327857>', 'Hunter Safety Certificate',
'CertificationTypeID', N'2019-03-14T10:55:06.0014212',
'Record Created', 'IA299038794', 'CertificateIdentifier',
N'2019-03-14T10:55:06.0014212', 'Record Created', '66',
'StateCertificationIssued', N'2019-03-14T10:55:06.0014212',
'Record Created', N'2019-02-28T00:00:00', 'DateCertificationIssued',
N'2019-03-14T10:55:06.0014212', 'Record Created'),
(N'StateCertificationIssued', '66', 'UA', N'2019-03-14T13:04:45.1401862',
'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ,
(N'createDate', 'Mar 14 2019 10:55AM', 'Mar 19 2019 1:52PM',
N'2019-03-19T13:52:29.5221885', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 19 2019 1:52PM', 'Mar 22 2019 10:43AM',
N'2019-03-22T10:43:42.1068855', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 22 2019 10:43AM', 'Mar 22 2019 2:38PM',
N'2019-03-22T14:38:57.5266904', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'StateCertificationIssued', 'UA', 'NJ', N'2019-03-26T11:25:28.9015254',
'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(N'CountryCertificationIssued', 'Gabon ', 'United States ',
N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 22 2019 2:38PM', 'Mar 26 2019 2:23PM',
N'2019-03-26T14:23:27.8660009', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'Record Created', NULL, NULL, N'2019-03-26T10:30:19.3982917', 'I',
'<CustomerCertificationID=327877>', 'Boater Safety Certificate',
'CertificationTypeID', N'2019-03-26T10:30:19.3982917', 'Record Created',
'IAC031900001', 'CertificateIdentifier' , N'2019-03-26T10:30:19.3982917',
'Record Created', 'IA', 'StateCertificationIssued',
N'2019-03-26T10:30:19.3982917', 'Record Created', N'2019-03-07T00:00:00',
'DateCertificationIssued', N'2019-03-26T10:30:19.3982917',
'Record Created'),
(N'CertificateIdentifier', 'IAC031900001', 'IAC031900002',
N'2019-03-26T22:02:21.3292077', 'U', '<CustomerCertificationID=327877>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'Record Created', NULL, NULL, N'2019-03-26T22:12:29.4201277', 'I',
'<CustomerCertificationID=327887>', 'Temporarily Disabled',
'CertificationTypeID', N'2019-03-26T22:12:29.4201277', 'Record Created',
'', 'CertificateIdentifier', N'2019-03-26T22:12:29.4201277',
'Record Created', 'IA', 'StateCertificationIssued',
N'2019-03-26T22:12:29.4201277', 'Record Created', N'2019-03-13T00:00:00',
'DateCertificationIssued', N'2019-03-26T22:12:29.4201277',
'Record Created' ) ,
(N'CertificateIdentifier', '', 'IAC9985478', N'2019-03-26T22:12:47.3471693',
'U', '<CustomerCertificationID=327887>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
SELECT
ColumnName,
OldValue,
NewValue,
DateChanged,
ActionType,
AuditLogPK
FROM #AuditLogCustomerCert
WHERE ColumnName != 'Record Created'
UNION ALL
SELECT
CASE ColumnName
WHEN 'CT_OriginalValue' THEN 'Record Created - CertificationTypeID'
WHEN 'CI_OriginalValue' THEN 'Record Created - CertificateIdentifier'
WHEN 'SCI_OriginalValue' THEN 'Record Created - StateCertificationIssued'
WHEN 'DCI_OriginalValue' THEN 'Record Created - DateCertificationIssued'
END,
NULL,
NewValue,
DateChanged,
ActionType,
AuditLogPK
FROM (
SELECT
DateChanged,
ActionType,
AuditLogPK,
CAST(CT_OriginalValue AS NVARCHAR(MAX)) CT_OriginalValue,
CAST(CI_OriginalValue AS NVARCHAR(MAX)) CI_OriginalValue,
CAST(SCI_OriginalValue AS NVARCHAR(MAX)) SCI_OriginalValue,
CAST(DCI_OriginalValue AS NVARCHAR(MAX)) DCI_OriginalValue
FROM #AuditLogCustomerCert
WHERE ColumnName = 'Record Created'
) AS t
UNPIVOT (
NewValue
FOR ColumnName
IN (CT_OriginalValue, CI_OriginalValue, SCI_OriginalValue, DCI_OriginalValue)
) AS unp;
输出:
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
| ColumnName | OldValue | NewValue | DateChanged | ActionType | AuditLogPK |
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
| StateCertificationIssued | 66 | UA | 14/03/2019 13:04:45 | U | <CustomerCertificationID=327857> |
| createDate | Mar 14 2019 10:55AM | Mar 19 2019 1:52PM | 19/03/2019 13:52:29 | U | <CustomerCertificationID=327857> |
| createDate | Mar 19 2019 1:52PM | Mar 22 2019 10:43AM | 22/03/2019 10:43:42 | U | <CustomerCertificationID=327857> |
| createDate | Mar 22 2019 10:43AM | Mar 22 2019 2:38PM | 22/03/2019 14:38:57 | U | <CustomerCertificationID=327857> |
| StateCertificationIssued | UA | NJ | 26/03/2019 11:25:28 | U | <CustomerCertificationID=327857> |
| CountryCertificationIssued | Gabon | United States | 26/03/2019 11:25:28 | U | <CustomerCertificationID=327857> |
| createDate | Mar 22 2019 2:38PM | Mar 26 2019 2:23PM | 26/03/2019 14:23:27 | U | <CustomerCertificationID=327857> |
| CertificateIdentifier | IAC031900001 | IAC031900002 | 26/03/2019 22:02:21 | U | <CustomerCertificationID=327877> |
| CertificateIdentifier | | IAC9985478 | 26/03/2019 22:12:47 | U | <CustomerCertificationID=327887> |
| Record Created - CertificationTypeID | | Hunter Safety Certificate | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - CertificateIdentifier | | IA299038794 | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - StateCertificationIssued | | 66 | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - DateCertificationIssued | | Feb 28 2019 12:00AM | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - CertificationTypeID | | Boater Safety Certificate | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - CertificateIdentifier | | IAC031900001 | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - StateCertificationIssued | | IA | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - DateCertificationIssued | | Mar 7 2019 12:00AM | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - CertificationTypeID | | Temporarily Disabled | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - CertificateIdentifier | | | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - StateCertificationIssued | | IA | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - DateCertificationIssued | | Mar 13 2019 12:00AM | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
使用db<>fiddle在线测试。
【讨论】:
以上是关于将多列反透视成行的主要内容,如果未能解决你的问题,请参考以下文章