我有一个case语句,根据每个列中的答案,该语句用于从几列创建一列。每当答案在任何一列中为1时,我都需要显示一个新行,但是目前,对于匹配的第一行,它只显示了一行。
请参见代码和示例。谢谢
我正在使用SQL Server 17 我正在从没有访问权限的表中查询,因此无法创建表等。我正在查询而不是声明和设置值。
ID,
CASE
WHEN ORG1 = 1 THEN 'ORG1'
WHEN ORG2 = 1 THEN 'ORG2'
WHEN ORG3 = 1 THEN 'ORG3'
WHEN ORG4 = 1 THEN 'ORG4'
WHEN ORG5 = 1 THEN 'ORG5'
else 'None' end as ORG
FROM TBL
当前表如下:
ID ORG1 ORG2 ORG3 ORG4 ORG5
1 1 0 0 0 1
2 0 1 1 0 0
3 0 0 1 0 1
4 0 1 0 0 0
5 0 0 0 1 0
6 1 0 0 1 0
我希望结果看起来像是
ID ORG
1 ORG1
1 ORG5
2 ORG2
2 ORG3
3 ORG3
3 ORG5
4 ORG2
5 ORG4
6 ORG1
6 ORG4
我的代码得到的是:
ID ORG
1 ORG1
2 ORG2
3 ORG3
4 ORG2
5 ORG4
6 ORG1
答案 0 :(得分:2)
您可以使用它。
-- Sample Data
DECLARE @TBL TABLE (ID INT, ORG1 INT, ORG2 INT, ORG3 INT, ORG4 INT, ORG5 INT)
INSERT INTO @TBL VALUES
(1, 1, 0, 0, 0, 1),
(2, 0, 1, 1, 0, 0),
(3, 0, 0, 1, 0, 1),
(4, 0, 1, 0, 0, 0),
(5, 0, 0, 0, 1, 0),
(6, 1, 0, 0, 1, 0),
(7, 0, 0, 0, 0, 0)
SELECT ID, UNPVT.ORG FROM @TBL
UNPIVOT( VAL FOR ORG IN ([ORG1], [ORG2], [ORG3], [ORG4], [ORG5])) UNPVT
WHERE VAL = 1
结果:
ID ORG
----------- ------------
1 ORG1
1 ORG5
2 ORG2
2 ORG3
3 ORG3
3 ORG5
4 ORG2
5 ORG4
6 ORG1
6 ORG4
如果您的情况是所有列均为“ 0”
SELECT ID, UNPVT.ORG FROM
(SELECT *,
([ORG1] + [ORG2] + [ORG3] + [ORG4] + [ORG5])^1 AS [None]
FROM @TBL) SRC
UNPIVOT( VAL FOR ORG IN ([ORG1], [ORG2], [ORG3], [ORG4], [ORG5], [None])) UNPVT
WHERE VAL = 1
结果:
ID ORG
----------- ------------
1 ORG1
1 ORG5
2 ORG2
2 ORG3
3 ORG3
3 ORG5
4 ORG2
5 ORG4
6 ORG1
6 ORG4
7 None
答案 1 :(得分:0)
一种简单的方法是像这样的union all
:
select * from
(
select id, 'ORG1' as org from tbl where org1=1
union all
select id, 'ORG2' as org from tbl where org1=2
union all
select id, 'ORG3' as org from tbl where org1=3
union all
select id, 'ORG4' as org from tbl where org1=4
union all
select id, 'ORG5' as org from tbl where org1=5
) as tmp
我猜您想过滤掉所有0
列。如果不是,只需删除where clause
答案 2 :(得分:0)
使用大小写表达式后不要取消操作
select id,org from (
select id, case when org1=1 then 'org1' end as og1,
case when org2=1 then 'org2' end as og2,
case when org3=1 then 'org3' end as og3,
case when org4=1 then 'org4' end as og4,
case when org5=1 then 'org5' end as og5
from table_name
) a unpivot (org for value in (og1,og2,og3,og4,og5)) p
id org
1 org1
1 org5
2 org2
2 org3
3 org3
3 org5
4 org2
5 org4
6 org1
6 org4
答案 3 :(得分:0)
这是另一种方法
declare @Tbl as table (id int, org1 int, org2 int, org3 int, org4 int, org5 int)
insert into @Tbl values
(1,1,0,0,0,1),
(2,0,1,1,0,0),
(3,0,0,1,0,1),
(4,0,1,0,0,0),
(5,0,0,0,1,0),
(6,1,0,0,1,0)
select * from @Tbl
declare @temp as table (org varchar(5))
insert into @temp values
('org1'),('org2'),('org3'),('org4'),('org5')
select * from @temp
select * from @Tbl cross join @temp
where (org='org1' and org1=1) or
(org='org2' and org2=1) or
(org='org3' and org3=1) or
(org='org4' and org4=1) or
(org='org5' and org5=1)
order by id
答案 4 :(得分:0)
我会使用cross apply
。但是,对于'NONE'
值,您需要附加的逻辑-即使您的示例数据不包含任何示例,
SELECT t.ID, v.ORG
FROM TBL t CROSS APPLY
(VALUES ('ORG1', ORG1),
('ORG2', ORG2),
('ORG3', ORG3),
('ORG4', ORG4),
('ORG5', ORG5)
) V(ORG, val),
WHERE val = 1
UNION ALL
SELECT t.ID, 'None'
FROM TBL
WHERE ORG1 = 0 AND ORG2 = 0 AND ORG3 = 0 AND ORG4 = 0 AND ORG5 = 0;
或者没有UNION ALL
:
SELECT t.ID, v.ORG
FROM TBL t CROSS APPLY
(VALUES ('ORG1', ORG1),
('ORG2', ORG2),
('ORG3', ORG3),
('ORG4', ORG4),
('ORG5', ORG5),
('NONE', (CASE WHEN ORG1 = 0 AND ORG2 = 0 AND ORG3 = 0 AND ORG4 = 0 AND ORG5 = 0 THEN 1 ELSE 0 END))
) V(ORG, val),
WHERE val = 1