Это можно сделать с повторяющимися именами столбцов, однако я не могу себе представить, почему так хочется.
В @Col мы применяем псевдоним ...[#] as [Tier Value]...
Пример
Declare @Col varchar(max) = Stuff((Select Distinct ',' + concat(QuoteName(row_number() over (Partition By ContractID Order by TierDesc)),' as [Tier Value]') From Yourtable Order by 1 For XML Path('')),1,1,'')
Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(row_number() over (Partition By ContractID Order by TierDesc)) From Yourtable Order by 1 For XML Path('')),1,1,'')
Select @SQL = '
Select ContractID,'+@Col+'
From (
Select ContractID
,TierDesc
,ColNr = row_number() over (Partition By ContractID Order by TierDesc)
From YourTable
) Src
Pivot (max(TierDesc) for ColNr in ('+@SQL+') ) pvt
'
Exec(@SQL)
Возвращает
ContractID Tier Value Tier Value Tier Value
123 tier1 tier2 NULL
555 tier4 tier5 tier6
652 tier0 tier4 NULL
РЕДАКТИРОВАТЬ - затем сгенерированный SQL выглядитэто
Select ContractID
,[1] as [Tier Value]
,[2] as [Tier Value]
,[3] as [Tier Value]
From (
Select ContractID
,TierDesc
,ColNr = row_number() over (Partition By ContractID Order by TierDesc)
From YourTable
) Src
Pivot (max(TierDesc) for ColNr in ([1],[2],[3]) ) pvt
РЕДАКТИРОВАТЬ 2
Select Distinct
ColNr = row_number() over (partition by ContractID Order By TierDesc)
From Yourtable