[英]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.