Solved

Dynamic SQL Query with Pivot Chart

Posted on 2016-10-19
5
17 Views
Last Modified: 2016-10-25
I'm having an issue.   This is what i'm seeing:

Must declare the scalar variable "@PivotWhen0"  i'm seeing this for all of the @PivotWhen0/1/2/3/4/5, etc.... can someone please help

SELECT DISTINCT TOP 100000
DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice]) AS 'Year', SUM([dbo].[v_rpt_Invoices].[Time_Amount]) AS 'Billed Time', SUM([dbo].[v_rpt_Invoices].[Misc_Amount]) AS 'Billed Product', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen0 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jan', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen1 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Feb', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen2 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Mar', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen3 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Apr', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen4 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'May', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen5 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jun', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen6 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jul', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen7 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Aug', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen8 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Sep', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen9 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Oct', SUM([dbo].[v_rpt_Invoices].[Invoice_Amount]) as 'Total'
FROM [dbo].[v_rpt_Invoices] WITH(NOLOCK)
WHERE ([dbo].[v_rpt_Invoices].[Status_Description] IN ('Closed','Closed - Emailed','New','Write Off')) AND ([dbo].[v_rpt_Invoices].[Date_Invoice] BETWEEN '2016-01-01T00:00:00.000' AND '2016-12-31T23:59:59.998')
GROUP BY DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice])
ORDER BY DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice]) ASC;
0
Comment
Question by:ITNC
  • 4
5 Comments
 
LVL 11

Expert Comment

by:Nakul Vachhrajani
ID: 41852045
Is this query run independently? The @PivotWhen* are variables and it appears that you do not have then in your batch.

How are you executing this query? Can you please share a code snippet if possible? (Do use the code blocks when submitting the code - pasting it directly as Rich Text makes it cumbersome to read).
0
 

Accepted Solution

by:
ITNC earned 0 total points
ID: 41852434
-- Detail
DECLARE @PivotWhen0 AS VARCHAR(10)
DECLARE @PivotWhen1 AS VARCHAR(10)
DECLARE @PivotWhen2 AS VARCHAR(10)
DECLARE @PivotWhen3 AS VARCHAR(10)
DECLARE @PivotWhen4 AS VARCHAR(10)
DECLARE @PivotWhen5 AS VARCHAR(10)
DECLARE @PivotWhen6 AS VARCHAR(10)
DECLARE @PivotWhen7 AS VARCHAR(10)
DECLARE @PivotWhen8 AS VARCHAR(10)
DECLARE @PivotWhen9 AS VARCHAR(10)
DECLARE @PivotWhen10 AS VARCHAR(10)
DECLARE @PivotWhen11 AS VARCHAR(10)
DECLARE @PivotWhen12 AS VARCHAR(10)
-- Value of 1 is January, Value of 2 is February, etc.
SET @PivotWhen0 = '1'
SET @PivotWhen1 = '2'
SET @PivotWhen2 = '3'
SET @PivotWhen3 = '4'
SET @PivotWhen4 = '5'
SET @PivotWhen5 = '6'
SET @PivotWhen6 = '7'
SET @PivotWhen7 = '8'
SET @PivotWhen8 = '9'
SET @PivotWhen9 = '10'
SET @PivotWhen10 = '11'
SET @PivotWhen11 = '12'
SET @PivotWhen12 = '13'
SELECT DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice]) AS 'Year', SUM([dbo].[v_rpt_Invoices].[Time_Amount]) AS 'Billed Time', SUM([dbo].[v_rpt_Invoices].[Misc_Amount]) AS 'Billed Product', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen0 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jan', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen1 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Feb', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen2 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Mar', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen3 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Apr', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen4 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'May', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen5 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jun', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen6 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Jul', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen7 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Aug', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen8 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Sep', SUM(CASE DATEPART(month, [dbo].[v_rpt_Invoices].[Date_Invoice]) WHEN @PivotWhen9 THEN [dbo].[v_rpt_Invoices].[Invoice_Amount] ELSE 0 END) as 'Oct', SUM([dbo].[v_rpt_Invoices].[Invoice_Amount]) as 'Total'
FROM [dbo].[v_rpt_Invoices] WITH(NOLOCK) 
WHERE ([dbo].[v_rpt_Invoices].[Status_Description] IN ('Closed','Closed - Emailed','New','Write Off')) AND ([dbo].[v_rpt_Invoices].[Date_Invoice] BETWEEN '2016-01-01T00:00:00.000' AND '2016-12-31T23:59:59.998')
GROUP BY DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice])
ORDER BY DATEPART(year, [dbo].[v_rpt_Invoices].[Date_Invoice]) ASC;

Open in new window

0
 

Author Comment

by:ITNC
ID: 41852439
This report shows the Total Invoiced Amount Per Month by Division.
I've got the initial message to go away and now i'm getting data but it's not separating by division.  How would I get it to separate the divisions properly.  The field name for division in our database is "BusGroup"
0
 

Author Comment

by:ITNC
ID: 41852563
I've actually got this resolved now. Closing request out.
0
 

Author Closing Comment

by:ITNC
ID: 41858261
Figured it out myself
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

16 Experts available now in Live!

Get 1:1 Help Now