Solved

SSRS SQL PIVOT Multiple Rows Required for Result

Posted on 2014-12-20
3
293 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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