【发布时间】:2018-01-11 20:35:32
【问题描述】:
我有一个由以下代码生成的玩具数据集:
declare @tbl table (year int, month int, property varchar(4), nights varchar(4), LOS int)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'VA', '1', 33)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'VA', '2', 43)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'VA', '3', 51)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'VA', '4', 27)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'VA', '5+', 82)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'PZ', '1', 37)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'PZ', '2', 63)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'PZ', '3', 41)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'PZ', '4', 67)
insert into @tbl (year, month, property, nights, LOS) VALUES (2017, 1, 'PZ', '5+', 52)
我想将property 列拆分为property_VA 和property_PZ 列,如下所示:
另一个问题是,我怎样才能追加一个名为total 的新行,以仅获得LOS_VA 和LOS_PZ 的总和。我不确定 SQL 能不能做到这一点
【问题讨论】:
-
你试过什么?结果与您的预期有何不同? Stack Overflow 不是代码编写服务。向我们展示您的尝试,我们可以帮助您找出为什么它不起作用。
-
我明白,我知道如何在 R 中做到这一点,但我不知道如何在 SQL 中做到这一点,我正在寻找一些在 SQL 中类似的
rbind函数。这就是我在这里寻求帮助的原因
标签: sql sql-server split