Solved

SSRS SQL PIVOT Multiple Rows Required for Result

Posted on 2014-12-20
3
284 Views
Last Modified: 2014-12-24
This query gets the result, below
SELECT *
FROM
(
	SELECT
	SalesLineItems.[Item Class Code],
	  SalesLineItems.[Item Number]
	  ,SalesLineItems.[Customer Name]
	  ,(SELECT CASE ISNULL ([Tax Exempt 1 from Customer Master], '')
           WHEN ''
           THEN NULL
           ELSE CAST ([Tax Exempt 1 from Customer Master] AS DECIMAL (10,2))
         END) as [Dispenser Cost]
	  ,SalesLineItems.[GL Posting Date]
	  ,SalesLineItems.[SOP Number]
	  ,CASE WHEN LEFT (SalesLineItems.[SOP Number],2)='CR' THEN sum(SalesLineItems.[Extended Price]*-1) ELSE sum(SalesLineItems.[Extended Price])END as [Extended Price]
	  ,datename(month,SalesLineItems.[GL Posting Date]) as FMonth
	  ,datename(year,SalesLineItems.[GL Posting Date]) as FYear
	  ,CASE WHEN LEFT (SalesLineItems.[SOP Number],2)='CR' THEN sum(SalesLineItems.[QTY]*-1) ELSE sum(SalesLineItems.[QTY])END as [QTY]
	  
	  
	  FROM
	  SalesLineItems
	  
	  WHERE
	  SalesLineItems.[SOP Type] IN ('Invoice','Return')
	  AND SalesLineItems.[Void Status] <> 'Voided'
	  AND SalesLineItems.[Document Status] = N'Posted'
	  AND SalesLineItems.[Extended Price] != 0
	  AND SalesLineItems.[Item Class Code] IN (@ItemClassID)
	AND datename(year,SalesLineItems.[GL Posting Date]) = @FYear
	GROUP BY 
	  SalesLineItems.[Item Class Code]
	  ,SalesLineItems.[Item Number]
	  ,SalesLineItems.[Customer Name]
	  ,SalesLineItems.[Tax Exempt 1 from Customer Master]
	  ,SalesLineItems.[GL Posting Date]
	  ,SalesLineItems.[SOP Number]
	  ) as DerivedTable

PIVOT (sum([Extended Price]) for FMonth in ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December]))
AS NEWMONTH

Open in new window

Current Result
I need help with how to modify the query to also be able to show quantities below each revenue line in my report.  For example:
Desired ResultDoing that within a pivot is throwing me off.  How to do?
0
Comment
Question by:CPA_MCSE
  • 2
3 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40512806
You need to do it as two separate pivots, and then UNION ALL between them.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40512808
Either that, or don't do it as a PIVOT, and do it as a Matrix instead - no need for a pivot at all in that case.
0
 

Author Closing Comment

by:CPA_MCSE
ID: 40516874
I was unable to get the query to parse correctly for the Union All because of the derived tables.  So, I chucked the idea of using that query (which was from another report I created for the same customer).

I used the core query (sans derived tables and pivot) and added the QTY field.  Then, in SRS, I created a new tablix and added column groups as adjacent for each month, filtered by month, and sorted by Item Number.  I added QTY as another row within the row group.

Results are as desired.

I rated the solution as "C" because the solution offered was "do it as a Matrix instead" without any further explanation.  It's a good thing I know my way around SRS.  Expert-to-"expert" (but I'm improving with every snafu!) that advice was sufficient to warrant full points and a sincere "Thank you!" for the clear-eyed suggestion.

Happy holidays!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

759 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

18 Experts available now in Live!

Get 1:1 Help Now