The first problem is how you want to identify which one is first in the group. If you have some other column(s) available to determine the order then you can use that in the generation of the row number.
I used the purchase amount - from smallest to largest such that the largest amount will be the last item.
Declare @passid table (PassangerID varchar(7)
, ClassID varchar(3)
, Purchase money
, Country varchar(20)
, Age smallint);
Insert Into @passid (PassangerID, ClassID, Purchase, Country, Age)
Values ('GIA2545', '101', '2250', 'USA', '42')
, ('GIA2545', '101', '1525', 'CANADA', '56')
, ('GIA2545', '101', '2550', 'USA', '33')
, ('GIA2545', '101', '1500', 'JAPAN', '47')
, ('GIA2545', '113', '2250', 'CHINA', '29')
, ('GIA2545', '113', '1500', 'CANADA', '40')
, ('GIA4020', '120', '1150', 'USA', '55')
, ('GIA4020', '120', '2000', 'USA', '67')
, ('GIA1111', '105', '3000', 'CHINA', '33')
, ('GIA1111', '105', '2000', 'JAPAN', '47')
, ('GIA1111', '105', '2150', 'CANADA', '31')
, ('GIA1111', '108', '2500', 'CHINA', '29');
With rowData
As (
Select *
, rn = row_number() over(Partition By p.PassangerID, p.ClassID Order By p.Purchase)
From @passid p
)
Select PassangerID
, ClassID
, Purchase_1 = max(Case When rn = 1 Then Purchase End)
, Country_1 = max(Case When rn = 1 Then Country End)
, Age_1 = max(Case When rn = 1 Then Age End)
, Purchase_2 = max(Case When rn = 2 Then Purchase End)
, Country_2 = max(Case When rn = 2 Then Country End)
, Age_2 = max(Case When rn = 2 Then Age End)
, Purchase_3 = max(Case When rn = 3 Then Purchase End)
, Country_3 = max(Case When rn = 3 Then Country End)
, Age_3 = max(Case When rn = 3 Then Age End)
, Purchase_4 = max(Case When rn = 4 Then Purchase End)
, Country_4 = max(Case When rn = 4 Then Country End)
, Age_4 = max(Case When rn = 4 Then Age End)
From rowData
Group By
PassangerID
, ClassID;
If there can be a variable number - then you either need to determine a fixed number and always calculated for that fixed number, or you need to use dynamic SQL I would recommend setting up and returning a fixed number instead of using a dynamic approach.