Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic SQL Query with Pivot Chart

Posted on 2016-10-19
5
Medium Priority
?
54 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 14

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

704 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