SQL 多列逆轴
SQL unpivot of multiple columns
我希望将以下宽 table 取消透视,但仅限于用户对该字段具有真实值以及适当日期的情况。
当前状态:
CUSTOMER_ID
First_Party_Email
Third_Party_Email
First_Party_Email_Date
Third_Party_Email_Date
40011111
1
1
2021-01-22 04:38:00.000
2021-01-17 06:38:00.000
50022222
NULL
1
NULL
2021-01-18 04:38:00.000
80066666
1
NULL
2021-01-24 05:38:00.000
NULL
_______________
_______________________
_______________________
_______________________________
_______________________________
要求的状态:
Customer_ID
Type
Value
Date
40011111
First_Party_Email
1
22/01/2021 04:38
40011111
Third_Party_Email
1
17/01/2021 06:38
50022222
Third_Party_Email
1
18/01/2021 04:38
80066666
First_Party_Email
1
24/01/2021 05:38
_______________________________________________________________________
创建 table 的关联查询,我的尝试无效:
create table Permissions_Obtained
(Customer_ID bigint
,First_Party_Email bit
,Third_Party_Email bit
,First_Party_Email_Date datetime
,Third_Party_Email_Date datetime
)
insert into Permissions_Obtained
(Customer_ID
,First_Party_Email
,Third_Party_Email
,First_Party_Email_Date
,Third_Party_Email_Date
)
VALUES
(40011111, 1, 1, '2021-01-22 04:38', '2021-01-17 06:38'),
(50022222, NULL, 1, NULL, '2021-01-18 04:38'),
(80066666, 1, NULL, '2021-01-24 05:38', null)
select *
from Permissions_Obtained
select
customer_id, Permission
from Permissions_Obtained
unpivot
(
GivenPermission
for Permission in (
First_Party_Email, Third_Party_Email
)
) unpiv1,
unpivot
(
GivenPermissionDate
for PermissionDate in (
First_Party_Email_Date, Third_Party_Email_Date
)
) unpiv2
where GivenPermission = 1
--drop table Permissions_Obtained
如有任何帮助,我们将不胜感激。 TIA
您不能同时拥有多个逆轴。相反,您可以根据需要使用交叉应用或内部联接或联合、联合所有或某种联接。我已经使用 join 和 unpivot 为此添加了一个示例答案。
SELECT
unpvt.Customer_ID
, [Type]
, ISNULL(po.First_Party_Email ,po.Third_Party_Email) AS [Value]
,CASE WHEN unpvt.Type = 'First_Party_Email' THEN po.First_Party_Email_Date
ELSE po.Third_Party_Email_Date
END AS [Date]
FROM
(
SELECT
Customer_ID, First_Party_Email , Third_Party_Email
FROM Permissions_Obtained
) p
UNPIVOT
( [Value] FOR [Type] IN
(First_Party_Email , Third_Party_Email )
)AS unpvt
INNER JOIN Permissions_Obtained [po]
on [po].Customer_ID = unpvt.Customer_ID
当取消透视多列时,CROSS APPLY (VALUES
通常是最简单和最有效的解决方案。
它为前一个 table 的每行创建一个虚拟的 table,因此将其取消旋转成单独的行。
SELECT
p.Customer_Id,
v.[Type],
v.Value,
v.Date
FROM Permissions_Obtained p
CROSS APPLY (VALUES
('First_Party_Email', p.First_Party_Email, p.First_Party_Email_Date),
('Third_Party_Email', p.Third_Party_Email, p.Third_Party_Email_Date)
) v([Type], Value, Date)
where v.Value IS NOT NULL;
我希望将以下宽 table 取消透视,但仅限于用户对该字段具有真实值以及适当日期的情况。
CUSTOMER_ID | First_Party_Email | Third_Party_Email | First_Party_Email_Date | Third_Party_Email_Date |
---|---|---|---|---|
40011111 | 1 | 1 | 2021-01-22 04:38:00.000 | 2021-01-17 06:38:00.000 |
50022222 | NULL | 1 | NULL | 2021-01-18 04:38:00.000 |
80066666 | 1 | NULL | 2021-01-24 05:38:00.000 | NULL |
_______________ | _______________________ | _______________________ | _______________________________ | _______________________________ |
要求的状态:
Customer_ID | Type | Value | Date |
---|---|---|---|
40011111 | First_Party_Email | 1 | 22/01/2021 04:38 |
40011111 | Third_Party_Email | 1 | 17/01/2021 06:38 |
50022222 | Third_Party_Email | 1 | 18/01/2021 04:38 |
80066666 | First_Party_Email | 1 | 24/01/2021 05:38 |
_______________________________________________________________________ |
创建 table 的关联查询,我的尝试无效:
create table Permissions_Obtained
(Customer_ID bigint
,First_Party_Email bit
,Third_Party_Email bit
,First_Party_Email_Date datetime
,Third_Party_Email_Date datetime
)
insert into Permissions_Obtained
(Customer_ID
,First_Party_Email
,Third_Party_Email
,First_Party_Email_Date
,Third_Party_Email_Date
)
VALUES
(40011111, 1, 1, '2021-01-22 04:38', '2021-01-17 06:38'),
(50022222, NULL, 1, NULL, '2021-01-18 04:38'),
(80066666, 1, NULL, '2021-01-24 05:38', null)
select *
from Permissions_Obtained
select
customer_id, Permission
from Permissions_Obtained
unpivot
(
GivenPermission
for Permission in (
First_Party_Email, Third_Party_Email
)
) unpiv1,
unpivot
(
GivenPermissionDate
for PermissionDate in (
First_Party_Email_Date, Third_Party_Email_Date
)
) unpiv2
where GivenPermission = 1
--drop table Permissions_Obtained
如有任何帮助,我们将不胜感激。 TIA
您不能同时拥有多个逆轴。相反,您可以根据需要使用交叉应用或内部联接或联合、联合所有或某种联接。我已经使用 join 和 unpivot 为此添加了一个示例答案。
SELECT
unpvt.Customer_ID
, [Type]
, ISNULL(po.First_Party_Email ,po.Third_Party_Email) AS [Value]
,CASE WHEN unpvt.Type = 'First_Party_Email' THEN po.First_Party_Email_Date
ELSE po.Third_Party_Email_Date
END AS [Date]
FROM
(
SELECT
Customer_ID, First_Party_Email , Third_Party_Email
FROM Permissions_Obtained
) p
UNPIVOT
( [Value] FOR [Type] IN
(First_Party_Email , Third_Party_Email )
)AS unpvt
INNER JOIN Permissions_Obtained [po]
on [po].Customer_ID = unpvt.Customer_ID
当取消透视多列时,CROSS APPLY (VALUES
通常是最简单和最有效的解决方案。
它为前一个 table 的每行创建一个虚拟的 table,因此将其取消旋转成单独的行。
SELECT
p.Customer_Id,
v.[Type],
v.Value,
v.Date
FROM Permissions_Obtained p
CROSS APPLY (VALUES
('First_Party_Email', p.First_Party_Email, p.First_Party_Email_Date),
('Third_Party_Email', p.Third_Party_Email, p.Third_Party_Email_Date)
) v([Type], Value, Date)
where v.Value IS NOT NULL;