Bonjour, j'ai des exemples de données
WELLS Cognizant NULL
Rahim Srinu collebra
RAM Raju tiato
NULL Srinu crystal
NULL NULL NUNET
Exemple de données:
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
script:
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
3 Réponses :
Vous pouvez utiliser l'agrégation conditionnelle:
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;
merci pour la réponse @gordon si je veux le rendre dynamique ce que je dois faire au lieu de mentionner WELLS et conscient .. parfois comme IBM, Accenture et d'autres entreprises
votre erreur est dans la dernière instruction select, elle devrait être comme ceci:
SELECT * from ( select * from @emp)T PIVOT (MAX(Emp_Val)FOR E_company IN([WELLS],[Cognizant],[NULL]))PVT order by 1
Cette approche utilise une auto-jointure dans le pivot pour énumérer les entreprises ayant plusieurs employés et valeurs. Il utilise ensuite une jointure droite sur la table pour énumérer les entreprises qui n'ont pas ces employés. La différence dans la sortie est que toutes les permutations nulles sont conservées. À part cela, cela devrait couvrir ce que vous recherchez.
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;
Comment avez-vous trois valeurs sous Cognizant?