Solved

SQL variable variables???

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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