просто попробуйте это:
DECLARE @tbl TABLE(Continent VARCHAR(100),[State] VARCHAR(100),City VARCHAR(100));
INSERT INTO @tbl VALUES
('North America','California','Oakland')
,('North America','California','Los Angeles')
,('North America','California','Sacramento')
,('North America','California','San Diego')
,('North America','California','San Francisco')
,('North America','California','Upton')
,('North America','Florida','Jacksonville')
,('North America','Florida','Fort Lauderdale')
,('North America','Florida','Miami')
,('North America','Florida','Tampa')
,('North America','New York','Albany')
,('North America','New York','Buffalo')
,('North America','New York','New York')
,('North America','New York','Rochester')
,('North America','New York','Utica')
,('North America','Texas','Austin')
,('North America','Texas','Dallas')
,('North America','Texas','San Antonio');
SELECT *
FROM
(
SELECT Placename.Continent,
Placename.State,
Placename.City,
row_number() over(partition by Placename.State order by Placename.City) rn
FROM @tbl AS PlaceName
) AS SourceTable PIVOT(Min(City) FOR [State] IN([California],
[Florida],
[New York],
[Texas]
)) AS PivotTable;
Это точно ваш собственный запрос, работающий с таблицей макетов . Насколько я вижу, результат именно тот, который вам нужен:
+---------------+----+---------------+-----------------+-----------+-------------+
| Continent | rn | California | Florida | New York | Texas |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 1 | Los Angeles | Fort Lauderdale | Albany | Austin |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 2 | Oakland | Jacksonville | Buffalo | Dallas |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 3 | Sacramento | Miami | New York | San Antonio |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 4 | San Diego | Tampa | Rochester | NULL |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 5 | San Francisco | NULL | Utica | NULL |
+---------------+----+---------------+-----------------+-----------+-------------+
| North America | 6 | Upton | NULL | NULL | NULL |
+---------------+----+---------------+-----------------+-----------+-------------+
Если есть что-то еще позади, пожалуйста, используйте мой автономный образец сверху, настройте работающий пример, подобный моему, и опишите, что еще вам нужно.