Solved

SQL variable variables???

Posted on 2011-09-22
4
436 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 500 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

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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: …

734 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