Helt seriøst? 14.000 kolonner i en visning? Du har et alvorligt designproblem her. Men hvis du vil prøve, så prøv denne dynamiske pivotforespørgsel. Det fungerer med de begrænsede data, du har angivet:
DECLARE @ColumnList VARCHAR (MAX)
DECLARE @SQL VARCHAR(MAX)
-- Create a list of distinct Item IDs which will become column headers
SELECT @ColumnList = COALESCE(@ColumnList + ', ','') + 'ItemID' + CAST(I.ItemID AS VARCHAR(12)) FROM (SELECT DISTINCT ItemID FROM Item) I
SET @SQL = '
SELECT
ShopID, ' + @ColumnList + '
FROM
(
SELECT
s.ShopID,
ItemID = ''ItemID'' + Cast(i.ItemID as varchar(12)),
sim.ItemCost
FROM
dbo.Shop_Item_Mapping AS sim
JOIN dbo.Shop AS s ON sim.ShopID = s.ShopID
JOIN dbo.Item AS i ON SIM.ItemID = i.ItemID
) T
PIVOT
(
MIN(ItemCost)
FOR T.ItemID IN (' + @ColumnList + ')
) AS PVT'
exec (@SQL)
Redigerede feltnavne i henhold til opdateret spørgsmål.