Solved

SQL query to MS pivot table

Posted on 2014-02-14
5
423 Views
Last Modified: 2014-02-17
The point of this query was to group up sales from a customer by year (2013,2012,2011,2010). After a lot of struggle I was able to get this to work in sql query almost the same as it was done before. The problem is that I cannot get it in a pivot table in excel, and the years 2013,2012, ect are listed as the rows when they should be column titles. I will attach a screen shot of what i have now, and what it needs to look like. I either need a way to import this as a pivot table with the years as the column titles, or a way to recreate this using MS query.
new-query.JPG
old-query.JPG
0
Comment
Question by:mjburgard
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 39860422
Would you please post the SQL for the new query so we have a starting point?

Thanks!
0
 
LVL 1

Author Comment

by:mjburgard
ID: 39860490
SELECT
      
      (Customer.CustomerName) as 'Customer',
      SUM(ARTransaction.SalesAnalysis) as 'Sales',
      YEAR(postingdate)as 'Year'
      
FROM
      ARTransaction, Customer
WHERE
      -- Change dates to desired timeframe
      PostingDate BETWEEN {d '2013-01-01'} AND {d '2013-12-31'}
      
      and salesAnalysis <> 0
      and ARTransaction.CustomerN = Customer.CustomerN
GROUP BY
    Customer.CustomerName,
    YEAR(postingdate)
      
ORDER BY
      SUM(ARTransaction.SalesAnalysis),
      Customer.CustomerName
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 39864809
Ah, you're doing the PIVOT operation in Excel rather than in SQL, right?

I'll try to come up with a solution in SQL.  Excel's pivot options are powerful as far as they go, but do seem a little limited.
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 39864855
OK, the TSQL PIVOT keyword isn't my greatest strength, but based on this documentation:
http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx

SELECT [Customer], [2010], [2011], [2012], [2013]

FROM (
SELECT
      
      (Customer.CustomerName) as 'Customer',
      SUM(ARTransaction.SalesAnalysis) as 'Sales',
      YEAR(postingdate)as 'Year'
      
FROM
      ARTransaction, Customer
WHERE
      -- Change dates to desired timeframe
      PostingDate BETWEEN {d '2013-01-01'} AND {d '2013-12-31'}
      
      and salesAnalysis <> 0
      and ARTransaction.CustomerN = Customer.CustomerN
GROUP BY
    Customer.CustomerName,
    YEAR(postingdate)
) X
PIVOT (
  Sum([Sales])
 For [Year] in ( [2010], [2011], [2012], [2013])
) as pvt 
Order by Sum([Sales], CustomerName

Open in new window

0
 
LVL 1

Author Comment

by:mjburgard
ID: 39865027
I was actually able to make some changes to the original finally and get it to work.. I did try your query up there and I was getting an error for compatibility error even when I used "set compatibility_level." Oh well, I got it to work a different way but thanks for your help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question