如何将 table 的前两列堆叠成一列,同时仅将第三列与第一列配对?
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?
前面问了一个很,这次的要求有点复杂。我有一个如下所示的 table:
| id | code_1 | code_2 | pair_1 |
|----|--------|--------|--------|
| 1 | a1 | a2 | b1 |
| 2 | a3 | a4 | (null) |
| 3 | a5 | (null) | b2 |
| 4 | a6 | (null) | (null) |
| 5 | (null) | a7 | b3 |
| 6 | (null) | a8 | (null) |
| 7 | (null) | (null) | b4 |
| 8 | (null) | (null) | (null) |
我想将 code_1 和 code_2 堆叠到一个列中,这可以通过使用来完成:
select id, code, pair_1 as pair
from source
unpivot (code for code_ in (code_1, code_2)) as unpvt;
,产生以下输出:
| id | code | pair |
|----|------|--------|
| 1 | a1 | b1 |
| 1 | a2 | b1 |
| 2 | a3 | (null) |
| 2 | a4 | (null) |
| 3 | a5 | b2 |
| 4 | a6 | (null) |
| 5 | a7 | b3 |
| 6 | a8 | (null) |
,但我也想捕获 code_1
与 pair_1
配对的条件,即每当 code
来自 code_1
时,它将使用中的值pair_1
对于 pair
;每当 code
来自 code_2
时,它将始终使用 null
作为 pair
.
例如,给定原始 table,目标 table 应该是这样的:
| id | code | pair |
|----|------|--------|
| 1 | a1 | b1 |
| 2 | a2 | (null) |
| 3 | a3 | (null) |
| 4 | a4 | (null) |
| 5 | a5 | b2 |
| 6 | a6 | (null) |
| 7 | a7 | (null) |
| 8 | a8 | (null) |
DDL:
http://sqlfiddle.com/#!18/743a5/16
create table source (
id int identity(1, 1) primary key,
code_1 varchar(10),
code_2 varchar(10),
pair_1 varchar(10)
);
insert into source values
('a1', 'a2', 'b1'),
('a3', 'a4', null),
('a5', null, 'b2'),
('a6', null, null),
(null, 'a7', 'b3'),
(null, 'a8', null),
(null, null, 'b4'),
(null, null, null);
create table target (
id int identity(1, 1) primary key,
code varchar(10),
pair varchar(10)
);
insert into target values
('a1', 'b1'),
('a2', null),
('a3', null),
('a4', null),
('a5', 'b2'),
('a6', null),
('a7', null),
('a8', null);
Can't you do simple UNION ALL
?
试试下面的查询
SELECT * FROM
(
SELECT id, code_1 as code, pair_1 as pair FROM source WHERE code_1 IS NOT NULL
UNION ALL
SELECT id, code_2 as code, NULL as pair FROM source WHERE code_2 IS NOT NULL
) UnionTable
ORDER BY id
前面问了一个很
| id | code_1 | code_2 | pair_1 |
|----|--------|--------|--------|
| 1 | a1 | a2 | b1 |
| 2 | a3 | a4 | (null) |
| 3 | a5 | (null) | b2 |
| 4 | a6 | (null) | (null) |
| 5 | (null) | a7 | b3 |
| 6 | (null) | a8 | (null) |
| 7 | (null) | (null) | b4 |
| 8 | (null) | (null) | (null) |
我想将 code_1 和 code_2 堆叠到一个列中,这可以通过使用来完成:
select id, code, pair_1 as pair
from source
unpivot (code for code_ in (code_1, code_2)) as unpvt;
,产生以下输出:
| id | code | pair |
|----|------|--------|
| 1 | a1 | b1 |
| 1 | a2 | b1 |
| 2 | a3 | (null) |
| 2 | a4 | (null) |
| 3 | a5 | b2 |
| 4 | a6 | (null) |
| 5 | a7 | b3 |
| 6 | a8 | (null) |
,但我也想捕获 code_1
与 pair_1
配对的条件,即每当 code
来自 code_1
时,它将使用中的值pair_1
对于 pair
;每当 code
来自 code_2
时,它将始终使用 null
作为 pair
.
例如,给定原始 table,目标 table 应该是这样的:
| id | code | pair |
|----|------|--------|
| 1 | a1 | b1 |
| 2 | a2 | (null) |
| 3 | a3 | (null) |
| 4 | a4 | (null) |
| 5 | a5 | b2 |
| 6 | a6 | (null) |
| 7 | a7 | (null) |
| 8 | a8 | (null) |
DDL:
http://sqlfiddle.com/#!18/743a5/16
create table source (
id int identity(1, 1) primary key,
code_1 varchar(10),
code_2 varchar(10),
pair_1 varchar(10)
);
insert into source values
('a1', 'a2', 'b1'),
('a3', 'a4', null),
('a5', null, 'b2'),
('a6', null, null),
(null, 'a7', 'b3'),
(null, 'a8', null),
(null, null, 'b4'),
(null, null, null);
create table target (
id int identity(1, 1) primary key,
code varchar(10),
pair varchar(10)
);
insert into target values
('a1', 'b1'),
('a2', null),
('a3', null),
('a4', null),
('a5', 'b2'),
('a6', null),
('a7', null),
('a8', null);
Can't you do simple
UNION ALL
?
试试下面的查询
SELECT * FROM
(
SELECT id, code_1 as code, pair_1 as pair FROM source WHERE code_1 IS NOT NULL
UNION ALL
SELECT id, code_2 as code, NULL as pair FROM source WHERE code_2 IS NOT NULL
) UnionTable
ORDER BY id