繁体   English   中英

如何对这个分层数据进行透视

[英]How to do pivoting on this layered data

嗨,我有样本数据

declare @emp table(id int identity(1,1),E_Name varchar(20),E_company varchar(20),Emp_Val VARCHAR(10))
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Rahim','WELLS','A')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Jag','collebra',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Vasu','nunet',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Kiran','crystal',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Sajan','tiato',NULL)

insert into @emp(E_Name,E_company,Emp_Val)VALUES('RAM','WELLS','A')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Srinu','Cognizant','B')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Raju','Cognizant','B')

样本数据 :

    id  E_Name  E_company   Emp_Val
    1   Rahim   WELLS        A
    2   Jag     collebra    NULL
    3   Vasu    nunet       NULL
    4   Kiran   crystal     NULL
    5   Sajan   tiato       NULL
    6   RAM     WELLS        A
    7   Srinu   Cognizant    B
    8   Raju    Cognizant    B

脚本 :

SELECT [WELLS],[Cognizant],[NULL] from (
select E_Name,E_company,Emp_Val from @emp)T
PIVOT (MAX(E_Name)FOR E_company IN([WELLS],[Cognizant],[NULL]))PVT

输出 :

   WELLS    Cognizant   NULL
    Rahim   Srinu      collebra
    RAM     Raju       tiato
    NULL    Srinu      crystal
    NULL    NULL       NUNET

您可以使用条件聚合:

select max(case when e_company = 'WELLS' then e_name end) as wells,
       max(case when e_company = 'Cognizant' then e_name end) as cognizant,
       max(case when e_company not in ('WELLS', 'Cognizant') then e_name end) as nulls       
from (select e.*,
             row_number() over (partition by (case when e_company in ('WELLS', 'Cognizant') then e_company end) order by id) as seqnum
      from @emp e
     ) e
group by seqnum
order by seqnum;

是一个 db<>fiddle。

你的错误是在最后一个 select 语句中,它应该是这样的:

SELECT * 
from (
select * from @emp)T
PIVOT (MAX(Emp_Val)FOR E_company IN([WELLS],[Cognizant],[NULL]))PVT
order by 1

这种方法在枢轴中使用自联接来枚举具有多个员工和价值观的公司。 然后它使用右连接返回到表来枚举没有这些员工的公司。 输出的不同之处在于保留了所有空排列。 除此之外,这应该涵盖您正在寻找的内容。

declare @emp table(id int identity(1,1),E_Name varchar(20),E_company 
varchar(20),Emp_Val VARCHAR(10))
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Rahim','WELLS','A')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Jag','collebra',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Vasu','nunet',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Kiran','crystal',NULL)
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Sajan','tiato',NULL)

insert into @emp(E_Name,E_company,Emp_Val)VALUES('RAM','WELLS','A')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Srinu','Cognizant','B')
insert into @emp(E_Name,E_company,Emp_Val)VALUES('Raju','Cognizant','B')


select distinct WELLS, Cognizant,case E_Company when 'Wells' then NULL when 
'Cognizant' then null else E_Company end as [NULL] from 
(
SELECT [WELLS],[Cognizant],[collebra], [nunet], [crystal], [tiato] from (
select e.E_Name,e2.E_name as E2_Name, e.E_company,e2.Emp_Val as Emp2_Val, e.Emp_Val 
from @emp e inner join @emp e2 on e.id=e2.id)T
PIVOT (MAX(E_Name)FOR E_company IN([WELLS],[Cognizant],[collebra], [nunet], 
[crystal], [tiato]))PVT) stagingtable
right join (select E_Company, E_Name from @emp) c on stagingtable.Cognizant=c.E_Name 
or stagingtable.WELLS=c.E_Name
order by 1 desc, 2 desc, 3 desc;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM