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

时间:2019-02-08 03:00:30

标签: sql sql-server tsql

嗨,我有示例数据

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

3 个答案:

答案 0 :(得分:1)

您可以使用条件聚合:

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;

Here是db <>小提琴。

答案 1 :(得分:0)

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

alter table main_iteminstance add offer_last_updated DATETIME default null

答案 2 :(得分:0)

此方法在枢轴内使用自我联接来枚举具有多个员工和价值观的公司。然后,它使用右连接返回到表上以枚举没有这些雇员的公司。输出的差异是保留了所有空排列。除此之外,它还可以满足您的需求。

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;