如何旋转多列(整数和日期)?
How to pivot multiple columns (int and date)?
CREATE TABLE dbo.children_bd (
birthday_id int,
child_birthday_id int,
notification_parents date,
notification_grandparents date,
invitation_parents_id int,
invitation_grandparents_id int
);
INSERT INTO dbo.children_bd
VALUES (1,9, '02-01-2021','07-01-2021', 4, 5);
这是我的代码:
select birthday_id, child_birthday_id, tm, rm
from (
select p.birthday_id, p.child_birthday_id, p.notification_parents,
p.notification_grandparents, p.invitation_parents_id, p.invitation_grandparents_id
from dbo.children_bd p
) t
UNPIVOT
(tm for id in (invitation_parents_id, invitation_grandparents_id)) pvt1
UNPIVOT
(rm for rid in (notification_parents, notification_grandparents)) pvt2
我收到这个:
+------------+-------------------+-----+-------------+
|birthday_id | child_birthday_id | tm | rm |
+------------+------------------ +-----+-------------+
| 1 | 9 | 4 | 2021-02-01 |
| 1 | 9 | 4 | 2021-07-01 |
| 1 | 9 | 5 | 2021-02-01 |
| 1 | 9 | 5 | 2021-07-01 |
+-------+----------+-------------+-----+--------------
但我想收到这个:
+------------+-------------------+-----+-------------+
|birthday_id | child_birthday_id | tm | rm |
+------------+------------------ +-----+-------------+
| 1 | 9 | 4 | 2021-02-01 |
| 1 | 9 | 5 | 2021-07-01 |
+-------+----------+-------------+-----+--------------
如果你想逆透视多个值,你可以使用 CROSS APPLY
along with a table value constructor:
SELECT c.birthday_id,
c.child_birthday_id,
upvt.Type,
upvt.NotificationDate,
upvt.InvitationID
FROM dbo.children_bd AS c
CROSS APPLY
(VALUES
('Parents', c.notification_parents, c.invitation_parents_id),
('Grandparents', c.notification_grandparents, c.invitation_grandparents_id)
) AS upvt (Type, NotificationDate, InvitationID);
CREATE TABLE dbo.children_bd (
birthday_id int,
child_birthday_id int,
notification_parents date,
notification_grandparents date,
invitation_parents_id int,
invitation_grandparents_id int
);
INSERT INTO dbo.children_bd
VALUES (1,9, '02-01-2021','07-01-2021', 4, 5);
这是我的代码:
select birthday_id, child_birthday_id, tm, rm
from (
select p.birthday_id, p.child_birthday_id, p.notification_parents,
p.notification_grandparents, p.invitation_parents_id, p.invitation_grandparents_id
from dbo.children_bd p
) t
UNPIVOT
(tm for id in (invitation_parents_id, invitation_grandparents_id)) pvt1
UNPIVOT
(rm for rid in (notification_parents, notification_grandparents)) pvt2
我收到这个:
+------------+-------------------+-----+-------------+
|birthday_id | child_birthday_id | tm | rm |
+------------+------------------ +-----+-------------+
| 1 | 9 | 4 | 2021-02-01 |
| 1 | 9 | 4 | 2021-07-01 |
| 1 | 9 | 5 | 2021-02-01 |
| 1 | 9 | 5 | 2021-07-01 |
+-------+----------+-------------+-----+--------------
但我想收到这个:
+------------+-------------------+-----+-------------+
|birthday_id | child_birthday_id | tm | rm |
+------------+------------------ +-----+-------------+
| 1 | 9 | 4 | 2021-02-01 |
| 1 | 9 | 5 | 2021-07-01 |
+-------+----------+-------------+-----+--------------
如果你想逆透视多个值,你可以使用 CROSS APPLY
along with a table value constructor:
SELECT c.birthday_id,
c.child_birthday_id,
upvt.Type,
upvt.NotificationDate,
upvt.InvitationID
FROM dbo.children_bd AS c
CROSS APPLY
(VALUES
('Parents', c.notification_parents, c.invitation_parents_id),
('Grandparents', c.notification_grandparents, c.invitation_grandparents_id)
) AS upvt (Type, NotificationDate, InvitationID);