SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot in SSMS porting to SSRS


Pivot in SSMS porting to SSRS

Author
Message
jeffshelix
jeffshelix
Mr or Mrs. 500
Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)

Group: General Forum Members
Points: 590 Visits: 193
I wrote this, and it works fine in SSMS. But i would prefer it in SSRS so the user can just run it and then open in Excel. I am wondering if i can port this to SSRS. I tried, it didn't work for me.
Thoughts?

USE GTM_ODS
SELECT
*
From
(SELECT
ISNULL(Line_item_Duty,0) as [DUTY],
[Supplier] as [SUPPLIER],
Datepart(MM,Entry_Summary_Date) as [MONTH]
from [ADHOC].[ATS_ESH] H
INNER JOIN ADHOC.ATS_ESL L
ON H.TRANS_SK = L.trans_Sk
WHERE
H.Importer = 'FORD'
AND Entry_Summary_Date > '1/1/2018')
as Data
PIVOT
(
Sum([DUTY])
FOR
[Month]
IN ("01","02","03","04","05","06","07","08","09","10","11","12")
) AS JEFF
order by [01] Desc

Thom A
Thom A
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60274 Visits: 17745
"it didn't work for me"? What do you mean? Did you get an error, unexpected results? You didn't like what you saw?

Rather than using a SQL query to PIVOT your data and using that data as your dataset in SSRS, consider doing the pivoting in SSRS using a Matrix.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)SSC Guru (172K reputation)

Group: General Forum Members
Points: 172378 Visits: 23542
jeffshelix - Friday, February 9, 2018 7:32 AM
I wrote this, and it works fine in SSMS. But i would prefer it in SSRS so the user can just run it and then open in Excel. I am wondering if i can port this to SSRS. I tried, it didn't work for me.
Thoughts?

USE GTM_ODS
SELECT
*
From
(SELECT
ISNULL(Line_item_Duty,0) as [DUTY],
[Supplier] as [SUPPLIER],
Datepart(MM,Entry_Summary_Date) as [MONTH]
from [ADHOC].[ATS_ESH] H
INNER JOIN ADHOC.ATS_ESL L
ON H.TRANS_SK = L.trans_Sk
WHERE
H.Importer = 'FORD'
AND Entry_Summary_Date > '1/1/2018')
as Data
PIVOT
(
Sum([DUTY])
FOR
[Month]
IN ("01","02","03","04","05","06","07","08","09","10","11","12")
) AS JEFF
order by [01] Desc

You could always put the SQL into a stored proc and use that as your SSRS source.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
pietlinden
pietlinden
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42736 Visits: 16085
Thom A - Friday, February 9, 2018 9:00 AM
"it didn't work for me"? What do you mean? Did you get an error, unexpected results? You didn't like what you saw?

Rather than using a SQL query to PIVOT your data and using that data as your dataset in SSRS, consider doing the pivoting in SSRS using a Matrix.

Without the IN() clause in the pivot, how do you force the existence of columns in the pivot? Use an outer join to a Tally table? Otherwise, if there are no results for a month, then the column will drop out of the pivot instead of showing zeroes.

Thom A
Thom A
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60274 Visits: 17745
pietlinden - Friday, February 9, 2018 9:17 AM
Thom A - Friday, February 9, 2018 9:00 AM
"it didn't work for me"? What do you mean? Did you get an error, unexpected results? You didn't like what you saw?

Rather than using a SQL query to PIVOT your data and using that data as your dataset in SSRS, consider doing the pivoting in SSRS using a Matrix.

Without the IN() clause in the pivot, how do you force the existence of columns in the pivot? Use an outer join to a Tally table? Otherwise, if there are no results for a month, then the column will drop out of the pivot instead of showing zeroes.

Yes, you'll need something to determine your groupings if there are no results, certainly. Using a Tally table, or Calendar Table (considering the OP's Pivot is on Month), would certainly solve that issue.



Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
drew.allen
drew.allen
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49793 Visits: 14720
jeffshelix - Friday, February 9, 2018 7:32 AM
I wrote this, and it works fine in SSMS. But i would prefer it in SSRS so the user can just run it and then open in Excel. I am wondering if i can port this to SSRS. I tried, it didn't work for me.
Thoughts?

Yes, I'm wondering why people think that "it didn't work for me" is a suitable description of their problem. You're going to have to be a LOT more specific if you want help troubleshooting this problem.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Chris Harshman
Chris Harshman
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29405 Visits: 6373
[quote]
jeffshelix - Friday, February 9, 2018 7:32 AM
I wrote this, and it works fine in SSMS. But i would prefer it in SSRS so the user can just run it and then open in Excel. I am wondering if i can port this to SSRS. I tried, it didn't work for me.
Thoughts?
If the end result is going to be used in an Excel spreadsheet anyway, would it make sense to not pivot the data in SQL, not use SSRS, and just have a query and pivot table in Excel that they can refresh the data on?

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search