Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL query to MS pivot table

Posted on 2014-02-14
5
Medium Priority
?
470 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 1500 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

963 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