Solved

Get especific dates from database

Posted on 2009-07-14
6
293 Views
Last Modified: 2012-05-07
Hello all!
I need to make a function that gets the past 12 records of the last day avaiable of each month from a date.
I need to sum them then divide it for 12 to get the avarage of the period.

I have a table with 3 colums (dt_date, vl_value, id_prod) which records a day by day historical (except weekends and holidays), so the last day of a month is not necessarily the last day (eg: Last day for May/2009 was not 31 but 29)

Example: Today is 7/14/2009. The values I need to get are:
(vl_value in 6/30/2009 + vl_value in 5/29/2009 + vl_value in 4/30/2009 + vl_value in 3/31/2009 + vl_value in 2/27/2009 + vl_value in 1/30/2009 + vl_value in 12/31/2008 + vl_value in 11/28/2008 + vl_value in 10/30/2008 + vl_value in 9/30/2009 + vl_value in 8/29/2009)/12

I may have that function for 12, 6 and 24 periods...

Please help!!!
Thanks in advance!
0
Comment
Question by:drix76
[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
6 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 24851204
First calculate the date of the first day in this month. Then you get the value for the first record before that date. Then you move back a month and repeat until you have got the number of values that you want:
create procedure GetLastDays
  @cnt int
as
 
set nocount on
 
declare
  @date datetime,
  @sum int
 
set @date = convert(datetime, convert(varchar(8),getdate(),120) + '01', 120)
set @sum = 0
 
while (@cnt > 0) begin
 
  select top 1 @sum = @sum + vl_value
  from TheTableWhateverItIsCalled
  where dt_date < @date
  order by dt_date desc
 
  set @date = dateadd(month, -1, @date)
  set @cnt = @cnt - 1
end
 
select Total = @sum

Open in new window

0
 

Author Comment

by:drix76
ID: 24853624
Thank you for your answer, but, I very newbee to SQL, so how do I implement this to try?
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 25549325
Just take the code that I posted, substitute the table name, and run it in a query window in Management Studio. It will create a stored procedure, which you then can call using for example:

GetLastDays 12

or

GetLastDays @cnt = 12
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

717 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