Display order of columns in pivot sql query

Hi all.

I have the following sql query in my Visual Studio application the data is displayed the following way:

YEAR--TOTAL SALES---ABC---DEF---GHI

but I want to change the order of the columns to be:

YEAR---ABC---DEF---GHI---TOTAL SALES

Any idea how I can do this? Thank you in advance!

SELECT *
FROM (
    SELECT 
        Year,ProductLine, Sales,TotalYearSales AS [Total Sales]
           FROM CRMProductLineSales
           WHERE PMID = '55655'
          
) as s
PIVOT
(	SUM(Sales)
    FOR ProductLine IN (ABC, DEF, GHI)
)AS pvt
ORDER BY Year

Open in new window

printmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Instead of "select *", specify the columns?

I'm not a SQL Server Expert but in another database the PIVOT clause pre-pends the alias to the columns
so something like (unable to test):
SELECT year, abc_sales_sum, def_sales_sum, ghi_sales_sum, [Total Sales]
FROM (
    SELECT 
        Year,ProductLine, Sales,TotalYearSales AS [Total Sales]
           FROM CRMProductLineSales
           WHERE PMID = '55655'
          
) as s
PIVOT
(	SUM(Sales) sales_sum
    FOR ProductLine IN (ABC, DEF, GHI)
)AS pvt
ORDER BY Year

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
printmediaAuthor Commented:
That was it. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.