?
Solved

SQL variable variables???

Posted on 2011-09-22
4
Medium Priority
?
439 Views
Last Modified: 2012-06-27
DECLARE
@beginDate date = convert(varchar,DATEADD(MONTH,DATEDIFF(MONTH,0,getdate()),0),101),
@endDate Date = convert(varchar,DATEADD(MONTH,DATEDIFF(MONTH,-1,getdate()),-1),101);

with aggregate as

(select a, b, c
from Table
where processingDate between @beginDate and @endDate
union all
select a, b, c
from Table2
where processingDate between @beginDate and @endDate)

Select  sum(a), sum(b), sum(c)
from aggregate

What I would like to know is this.  How can I work with the variables properly so that I can do this as a monthly thing?  I want 6 months of data, meaning.... last months, then the month before that, then the month before that, etc.

What is the best way to approach this?  If I declare the variable as something fixed, it doesn't work.  I need to make the variable... variable... but how?
0
Comment
Question by:simplyfemales
[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
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36584433
Hi.

Is this is a rolling six months, i.e., when you run the report it should always give you the last full six months. For example running this today should give you August back to March. I would just get rid of the variables and put that into code; however, if you want the variables, note that you do not need to convert to US English format only to store as a Date which could care less about format. i.e., SQL does not store dates in a given format. That is strictly a display issue.

So these are the values you need:
DECLARE @beginDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0),
@endDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);

You would then use then in your query as:
WHERE processingDate >= @beginDate AND  processingDate < @endDate

The @endDate is the first day of the current month at midnight, so the < is intentional. This helps with data that has timestamps as it will include all timestamps from the previous day up to but not including midnight. I guess if this is a date column, the between is fine, just make it:
WHERE processingDate BETWEEN @beginDate AND DATEADD(DAY, -1, @endDate)

Then I would GROUP BY DATEDIFF(MONTH, 0, processingDate), so that you get your sums for each of the six months in one report that you can convert back to a DATE for display (i.e., first day of each month). Another option is to show as month's ago, by doing your DATEDIFF(MONTH, processingDate, GETDATE()). Then you can consistently report on 1, 2, 3, 4, 5, or 6 months ago without worrying about actual date name.

Hope that helps!
0
 

Author Comment

by:simplyfemales
ID: 36584504
so I need to include ProcessingDate as a part of the select statements correct?  
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36584622
Correct, except you want to use one of the variations I showed using DATEDIFF() or else you will get sums by day instead of by month.
0
 

Author Closing Comment

by:simplyfemales
ID: 36588375
This is excellent.  Exactly what I need.  It reminded me of how basic the queries need to be sometimes.... just go back to what you know rather than trying to overthink it.
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

719 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