Solved

SSRS SQL PIVOT Multiple Rows Required for Result

Posted on 2014-12-20
3
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

622 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