Solved

Dynamic SQL Query with Pivot Chart

Posted on 2016-10-19
5
40 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
[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
  • 4
5 Comments
 
LVL 13

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server for XML PATH giving wrong results. 6 56
efficient backup report for SQL Server 13 79
export sql server dbs 2 24
t-sql left join 2 27
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 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