【发布时间】:2022-01-07 11:45:49
【问题描述】:
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 |
+-------+----------+-------------+-----+--------------
【问题讨论】:
-
我收到这个:prnt.sc/214tz04
标签: sql sql-server tsql pivot