Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Erland Sommarskog Newsgroups: microsoft.public.sqlserver.programming Subject: Re: How do I order the columns on a pivot table? Date: Sun, 12 Apr 2020 22:31:03 +0200 Organization: Erland Sommarskog Lines: 22 Message-ID: References: <32b39068-acc8-48ef-ac56-7f80bc2ee65b@googlegroups.com> <7a25939e-1e2d-4de3-9315-edef91428a1a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="ff96d794c89c0239ea5bf45a2c7528e6"; logging-data="11280"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+aVOtL8Sgqp4I+dGiuzFhk" User-Agent: Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) Cancel-Lock: sha1:xe2jkczZ8ARMGkPvkp7IphuMY+Y= Xref: csiph.com microsoft.public.sqlserver.programming:31358 John Hall (foundarecord@gmail.com) writes: > 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 > > The SQL Management studio is saying this is a view not a pivot table and > can't have an ORDER BY. I simplified this for my question. I need to > order the results and JOIN it on TestID with yet another table. > Could explain more closely what you are doing? The above is not a view, it is a query, since, well, that seemed to be what you asked for. But obviously, you could make a it view by adding CREATE VIEW on top. You cannot have ORDER BY in a view definition.