假设我有以下内容:
if object_id('tempdb..#Dependents') is not null
drop table #Dependents
create table #Dependents
(EmpNo varchar(12), DepID varchar(12), Relation varchar(12));
go
if object_id('tempdb..#Dep_Benefits') is not null
drop table #Dep_Benefits
create table #Dep_Benefits
(DepID varchar(12), PlanCode varchar(12), Coverage varchar(25));
go
INSERT INTO #Dependents VALUES('001', '111', 'SON');
INSERT INTO #Dependents VALUES('001', '222', 'SON');
INSERT INTO #Dependents VALUES('001', '333', 'DAUGHTER');
INSERT INTO #Dependents VALUES('002', '666', 'SON');
INSERT INTO #Dependents VALUES('002', '777', 'DAUGHTER');
INSERT INTO #Dep_Benefits VALUES('111', 'AAAA', 'MEDICAL');
INSERT INTO #Dep_Benefits VALUES('111', 'BBBB', 'DENTAL');
INSERT INTO #Dep_Benefits VALUES('111', 'CCCC', 'VISION');
INSERT INTO #Dep_Benefits VALUES('111', 'DDDD', 'DISABL');
INSERT INTO #Dep_Benefits VALUES('222', 'AAAA', 'MEDICAL');
INSERT INTO #Dep_Benefits VALUES('222', 'BBBB', 'DENTAL');
INSERT INTO #Dep_Benefits VALUES('222', 'CCCC', 'VISION');
INSERT INTO #Dep_Benefits VALUES('333', 'AAAA', 'MEDICAL');
INSERT INTO #Dep_Benefits VALUES('333', 'BBBB', 'DENTAL');
INSERT INTO #Dep_Benefits VALUES('666', 'AAAA', 'MEDICAL');
INSERT INTO #Dep_Benefits VALUES('666', 'BBBB', 'DENTAL');
INSERT INTO #Dep_Benefits VALUES('666', 'CCCC', 'VISION');
SELECT * FROM #Dependents;
SELECT * FROM #Dep_Benefits;
我怎样才能得到这个的输出:
Employee | Dependent | Plan 1 | Plan 2 | Plan 3
001 111 AAAA BBBB DDDD
001 222 AAAA BBBB CCCC
001 333 AAAA BBBB
002 666 AAAA BBBB CCCC
我只关心3个计划,即使有更多分配给依赖者。这是来自客户的固定请求(只有3个计划)所以请不要评论为什么这可能是一个坏主意:)我已经告诉他们,如果一个受抚养者超过3,我们无法保证可以返回3个计划
我查看了一个数据透视表,但似乎无法使其正常工作。
答案 0 :(得分:0)
使用row_number()
对计划进行编号,并使用条件聚合:
select
d.empno
, d.depid
, Plan_1 = max(case when rn = 1 then PlanCode end)
, Plan_2 = max(case when rn = 2 then PlanCode end)
, Plan_3 = max(case when rn = 3 then PlanCode end)
, Plan_4 = max(case when rn = 4 then PlanCode end)
from (
select d.empNo, db.*
, rn = row_number() over(partition by db.depid order by plancode)
from #Dependents d
inner join #Dep_Benefits db
on d.depid = db.depid
) d
group by d.empno, d.depid
rextester演示:http://rextester.com/FNLH5237
返回:
+-------+-------+--------+--------+--------+--------+
| empno | depid | Plan_1 | Plan_2 | Plan_3 | Plan_4 |
+-------+-------+--------+--------+--------+--------+
| 001 | 111 | AAAA | BBBB | CCCC | DDDD |
| 001 | 222 | AAAA | BBBB | CCCC | NULL |
| 001 | 333 | AAAA | BBBB | NULL | NULL |
| 002 | 666 | AAAA | BBBB | CCCC | NULL |
+-------+-------+--------+--------+--------+--------+
使用条件聚合按类型转动计划:
select
d.empno
, d.depid
, Medical = max(case when db.Coverage = 'Medical' then PlanCode end)
, Dental = max(case when db.Coverage = 'Dental' then PlanCode end)
, Vision = max(case when db.Coverage = 'Vision' then PlanCode end)
, Disability = max(case when db.Coverage = 'disabl' then PlanCode end)
from #Dependents d
inner join #Dep_Benefits db
on d.depid = db.depid
group by d.empno, d.depid
返回:
+-------+-------+---------+--------+--------+------------+
| empno | depid | Medical | Dental | Vision | Disability |
+-------+-------+---------+--------+--------+------------+
| 001 | 111 | AAAA | BBBB | CCCC | DDDD |
| 001 | 222 | AAAA | BBBB | CCCC | NULL |
| 001 | 333 | AAAA | BBBB | NULL | NULL |
| 002 | 666 | AAAA | BBBB | CCCC | NULL |
+-------+-------+---------+--------+--------+------------+