如何获取使用pivot创建的这组数据。
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
我使用了下面的创建和插入代码:
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')
基于一些博客和视频的当前数据透视代码:
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
答案 0 :(得分:1)
使用row_number()
和条件聚合来转移数据:
select
Country
, Town_01 = max(case when rn = 1 then Town end)
, City_01 = max(case when rn = 1 then City end)
, Town_02 = max(case when rn = 2 then Town end)
, City_02 = max(case when rn = 2 then City end)
, Town_03 = max(case when rn = 3 then Town end)
, City_03 = max(case when rn = 3 then City end)
, Town_04 = max(case when rn = 4 then Town end)
, City_04 = max(case when rn = 4 then City end)
from (
select *
, rn = row_number() over (partition by Country order by Town)
from Countries
) as t
group by Country
rextester演示:http://rextester.com/GYV10206
返回:
+---------+---------+-----------+------------+------------+-----------+------------+---------+------------+
| Country | Town_01 | City_01 | Town_02 | City_02 | Town_03 | City_03 | Town_04 | City_04 |
+---------+---------+-----------+------------+------------+-----------+------------+---------+------------+
| India | Bang | Bangalore | Del | New Delhi | hyder | Hyderabad | townin | India Town |
| UK | chelsea | London | sunderland | Birmingham | United | Manchester | NULL | NULL |
| USA | Dally | Dallas | houst | Houston | New Jersy | New York | NULL | NULL |
+---------+---------+-----------+------------+------------+-----------+------------+---------+------------+
答案 1 :(得分:0)
COALESCE是您正在寻找的功能。它将返回第一个非null值。更多信息:
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql
答案 2 :(得分:0)
试试这段代码:
with cte as
(Select Country, min(town1) as town1, min(City1) as City1, min(town2)as town2, min(City2)as City2, min(town3)as town3,
min(City3) as City3, min(town4) as town4, min(City4) as City4, min(town5) as town5, min(City5) as 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)
Select t.Country, COALESCE(t.town1,d.town1), COALESCE(t.City1,d.city1), COALESCE(t.town2,d.town2),
COALESCE(t.City2,d.city2), COALESCE(t.town3,d.town3), COALESCE(t.City3,d.city3), COALESCE(t.town4,d.town4),
COALESCE(t.City4,d.city4), COALESCE(t.town5,d.town5), COALESCE(t.City5,d.city5)
from cte d iNNER JOIN CTE t ON d.country = t.country