Comment puis-je obtenir cet ensemble de données que j'ai créé en utilisant le pivot. pour ressembler à ceci comme ces données qui est ce qui est nécessaire pour moi d'afficherPivot sur plusieurs lignes dynamiques
Country town1 City1 town2 City2 town3 City3 town4 City4 town5 City5
India NULL NULL NULL NULL NULL NULL townin India Town NULL NULL
India NULL NULL NULL NULL Del New Delhi NULL NULL NULL NULL
India NULL NULL Bang Bangalore NULL NULL NULL NULL NULL NULL
India hyder Hyderabad NULL NULL NULL NULL NULL NULL NULL NULL
UK NULL NULL NULL NULL United Manchester NULL NULL NULL NULL
UK NULL NULL sunderland Birmingham NULL NULL NULL NULL NULL NULL
UK chelsea London NULL NULL NULL NULL NULL NULL NULL NULL
USA NULL NULL NULL NULL Dally Dallas NULL NULL NULL NULL
USA NULL NULL houst Houston NULL NULL NULL NULL NULL NULL
USA New Jersy New York NULL NULL NULL NULL NULL NULL NULL NULL
:
Country town1 City1 town2 City2 town3 City3 town4 City4 town5 City5
India hyder Hyderabad Bang Bangalore Del New Delhi townin India Town NULL NULL
UK chelsea London sunderland Birmingham United Manchester NULL NULL NULL NULL
USA New Jersy New York houst Houston Dally Dallas NULL NULL NULL NULL
mes créer et d'insérer le code ci-dessous a été utilisé:
Create Table Countries
(
Country nvarchar(50),
Town nvarchar(50),
City nvarchar(50)
)
GO
Insert into Countries values ('USA','New Jersy','New York')
Insert into Countries values ('USA','houst','Houston')
Insert into Countries values ('USA','Dally','Dallas')
Insert into Countries values ('India','hyder','Hyderabad')
Insert into Countries values ('India','Bang','Bangalore')
Insert into Countries values ('India','Del','New Delhi')
Insert into Countries values ('India','townin','India Town')
Insert into Countries values ('UK','chelsea','London')
Insert into Countries values ('UK','sunderland','Birmingham')
Insert into Countries values ('UK','United','Manchester')
Code pivot actuel basé sur quelques blogs et vidéos:
Select Country, town1, City1, town2, City2, town3, City3, town4, City4, town5, City5
From
(
Select Country, City, 'City'+ cast(row_number() over(partition by Country order by Country) as varchar(10)) ColumnSequence,
Town, 'Town'+ cast(row_number() over(partition by Country order by Country) as varchar(10)) ColumnSequence2
from Countries
) Temp
pivot
(
max(City)
for ColumnSequence in (City1, City2, City3, City4, City5)
) Piv
pivot
(
max(Town)
for ColumnSequence2 in (town1, town2, town3, town4, town5)
) Piv
group by Country, town1, City1, town2, City2, town3, City3, town4, City4,
town5, City5