X-Received: by 2002:a05:620a:670:: with SMTP id a16mr12806760qkh.467.1586719661502; Sun, 12 Apr 2020 12:27:41 -0700 (PDT) X-Received: by 2002:a05:620a:66f:: with SMTP id a15mr13069993qkh.341.1586719661279; Sun, 12 Apr 2020 12:27:41 -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: Sun, 12 Apr 2020 12:27:41 -0700 (PDT) In-Reply-To: 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 References: <32b39068-acc8-48ef-ac56-7f80bc2ee65b@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: How do I order the columns on a pivot table? From: John Hall Injection-Date: Sun, 12 Apr 2020 19:27:41 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 31 Xref: csiph.com microsoft.public.sqlserver.programming:31355 On Sunday, April 12, 2020 at 7:59:26 AM UTC-4, Erland Sommarskog wrote: > SELECT PE.TestID, > MIN(CASE PU.PresentOrder WHEN 1 THEN PE.TextValue END) AS Param1, > MIN(CASE PU.PresentOrder WHEN 2 THEN PE.TextValue END) AS Param2, > MIN(CASE PU.PresentOrder WHEN 3 THEN PE.TextValue END) AS Param3, > MIN(CASE PU.PresentOrder WHEN 4 THEN PE.TextValue END) AS Param4, > MIN(CASE PU.PresentOrder WHEN 5 THEN PE.TextValue END) AS Param5 > FROM ParametersEntries PE > JOIN ParametersUser PU ON PE.TestID = PU.TestID > GROUP BY PE.TestID SELECT PE.TestID, MIN(CASE PU.PresentOrder WHEN 1 THEN PE.TextValue END) AS Param1, MIN(CASE PU.PresentOrder WHEN 2 THEN PE.TextValue END) AS Param2, MIN(CASE PU.PresentOrder WHEN 3 THEN PE.TextValue END) AS Param3, MIN(CASE PU.PresentOrder WHEN 4 THEN PE.TextValue END) AS Param4, MIN(CASE PU.PresentOrder WHEN 5 THEN PE.TextValue END) AS Param5 FROM ParameterEntries PE JOIN ParametersUsed PU ON PE.ParameterID = PU.ParameterID WHERE TestID IN (SELECT TestID FROM Subgroups WHERE CharID = 502107 AND DateTime = '08/29/2017 10:29:25' ) GROUP BY PE.TestID TestID Param1 Param2 Param3 Param4 Param5 519802 201487332 201487332 2.20 1.54 1.80 SELECT ParameterID, TextValue FROM ParameterEntries WHERE TestID = 519802 154 201487332 502009 5w 502022 2.20 502023 1.54 502024 1.80 So, why is Param1 getting duplicated into Param2 but 3, 4, & 5 are fine?