Solved

Dynamic SQL Query with Pivot Chart

Posted on 2016-10-19
5
37 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 58
T-SQL to copy a database 37 65
Need SSIS project 2 23
install sql server management studio 2008 express error 3 21
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

680 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