Dengan serius? 14.000 kolom dalam tampilan? Anda memiliki masalah desain yang serius di sini. Namun jika Anda ingin mencobanya, coba kueri pivot dinamis ini. Ini berfungsi dengan data terbatas yang Anda berikan:
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)
Nama bidang yang diedit sesuai pertanyaan yang diperbarui.