X-Received: by 2002:a0c:9104:: with SMTP id q4mr10074141qvq.61.1586620860232; Sat, 11 Apr 2020 09:01:00 -0700 (PDT) X-Received: by 2002:a37:e214:: with SMTP id g20mr8018611qki.160.1586620860007; Sat, 11 Apr 2020 09:01:00 -0700 (PDT) Path: csiph.com!xmission!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: microsoft.public.sqlserver.programming Date: Sat, 11 Apr 2020 09:00:59 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=173.162.45.17; posting-account=oUsksgoAAADjmotEgGTiI_3PeNLPQGJq NNTP-Posting-Host: 173.162.45.17 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <9c212941-b3a2-4f07-b29c-e8145580bbad@googlegroups.com> Subject: Ordering the columns in a pivot table From: John Hall Injection-Date: Sat, 11 Apr 2020 16:01:00 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 26 Xref: csiph.com microsoft.public.sqlserver.programming:31350 I have a data table with these 3 columns SELECT [TestID],[ParameterID],[TextValue] FROM [ParameterEntries] select TestID, [1] as Param1, [2] as Param2, [3] as Param3, [4] as Param4, [5] as Param5 from (select TestID, TextValue, row_number() over(partition by TestID order by ParameterID) rnk from ParameterEntries WHERE TestID = (SELECT TOP 1 TestID FROM Subgroups WHERE CharID = 502107) ) p pivot(max(TextValue) for rnk in ([1], [2], [3], [4], [5])) piv TestID Param1 Param2 Param3 Param4 Param5 502014 20147338 3w 2.14 1.49 1.73 SELECT [PartID], [ParameterID], [PresentOrder] FROM [ParametersUsed] WHERE PartID = (SELECT PartID FROM CharList WHERE CharID = 502107) ORDER BY PresentOrder TestID ParameterID PresentOrder 502014 154 1 502014 502009 2 502014 502022 3 502014 502023 4 502014 502024 5 How do I put the Param1, Param2, Param3... in PresentOrder?