Solved

SQL query to MS pivot table

Posted on 2014-02-14
5
412 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
Comment Utility
Would you please post the SQL for the new query so we have a starting point?

Thanks!
0
 
LVL 1

Author Comment

by:mjburgard
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now