Solved

Producing Monthly Totals with SQL

Posted on 2011-09-15
11
237 Views
Last Modified: 2012-05-12
I've never tried this with SQL, but I'm sure it's possible.
I have data prepared nicely with the following self-explanatory fields:

ID, DAILY_AMOUNT, TERM_START, TERM_END

I want to turn this into a report that yields total DAILY_AMOUNT where the selection criteria specify a start and end date.
So, for example, this could be used to answer the question, "How much did I spend on widgets in July of 2010?".
So If I spent $5 every day on widgets from, say, 2010/6/12 to 2010/7/5, the answer would be $25 ($5 x 5 Days in July).

What's the smart way to do this?

Mike
0
Comment
Question by:shacho
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 25

Expert Comment

by:jogos
Comment Utility
select year(TERM_END),month(TERM_END), sum(DAILY_AMOUNT)
from tabelname
where TERM_END between   .. and ..
group by year(TERM_END),month(TERM_END)
0
 

Author Comment

by:shacho
Comment Utility
That would work if I had individual records for each day.  I don't I only have the "daily amount".  So I need to calculate how many days the expense term and the extraction term have in common.  If I have that number, I can multiply by the daily amount to get the sum.
0
 
LVL 4

Expert Comment

by:AnuTiji
Comment Utility
Hi

Please find the TSQL statement below:

DECLARE @TERM_STARTDATE datetime
DECLARE @TERM_ENDDATE datetime

SET @TERM_STARTDATE  = '05/05/2010' (Input: Start date of the period)
SET @TERM_ENDDATE ='05/08/2010' (Input: End date of the period)

SELECT sum(ISNULL(DAILY_AMOUNT,0) * DATEDIFF(day,TERM_START, TERM_END)) as amount FROM table_name WHERE DATEDIFF(day,@TERM_STARTDATE, TERM_START) >=0  AND DATEDIFF(day,@TERM_ENDDATE, TERM_END) <=0  
0
 

Author Comment

by:shacho
Comment Utility
I can't quite see how that would work.  There are several scenarios that need to be considered, e.g. ranges that don't overlap, partially overlap, are sub or super sets of each other, etc.  This is how I do it (in Access with VBA) right now.  This may clarify what the actual data look like.  This is a fine solution, but SQL would be much faster.

Public Function OverLap(TermStart As Date, TermEnd As Date, RangeStart As Date, RangeEnd As Date) As Integer
    Dim StartDate As Date, EndDate As Date
    OverLap = 0
    If TermEnd < RangeStart Then Exit Function
    If TermStart > RangeEnd Then Exit Function
    If RangeEnd < TermEnd Then EndDate = RangeEnd Else EndDate = TermEnd
    If RangeStart > TermStart Then StartDate = RangeStart Else StartDate = TermStart
    OverLap = EndDate - StartDate + 1
End Function
0
 
LVL 4

Expert Comment

by:AnuTiji
Comment Utility
hi

I believe the above conditions are satisfied by sql query pasted below. Can you test the sql query for a date range?

DECLARE @TERM_STARTDATE datetime
DECLARE @TERM_ENDDATE datetime

SET @TERM_STARTDATE  = '05/05/2010' (Input: Start date of the period)
SET @TERM_ENDDATE ='05/08/2010' (Input: End date of the period)

SELECT sum(ISNULL(DAILY_AMOUNT,0) * DATEDIFF(day,TERM_START, TERM_END)) as amount FROM table_name WHERE DATEDIFF(day,@TERM_STARTDATE, TERM_START) >=0  AND DATEDIFF(day,@TERM_ENDDATE, TERM_END) <=0  
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
Comment Utility

SELECT SUM(DAILY_AMOUNT
           * DATEDIFF(day, CASE WHEN TERM_START>@RangeStart
                                THEN TERM_START
                                ELSE @RangeStart
                            END
                          , CASE WHEN TERM_END>@RangeEnd
                                THEN TERM_END
                                ELSE @RangeEnd
                            END
                     )
          ) As [Amount]
FROM tablename
WHERE ID=@ID
      AND TERM_START<=@RangeEnd
      AND TERM_END>=@RangeStart

Open in new window

0
 

Author Comment

by:shacho
Comment Utility
There we go.  Essentially an SQL version of my VB function.  Nifty.
0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
In calculation that must be datediff + 1
with case you can select the apropriate date
DECLARE @TERM_STARTDATE datetime
DECLARE @TERM_ENDDATE datetime

SET @TERM_STARTDATE  = '05/05/2010' -- (Input: Start date of the period)
SET @TERM_ENDDATE ='05/08/2010' --(Input: End date of the period)

select year(TERM_END) as y,month(TERM_END) as m
, sum(ISNULL(DAILY_AMOUNT,0) 
* (DATEDIFF(day,
  (case when TERM_START > @TERM_STARTDATE then TERM_START else @TERM_STARTDATE end) 
  , (case when TERM_END < @TERM_ENDDATE then TERM_END else @TERM_ENDDATE end))
  +1) as amount 
from tablename
where TERM_END >= @TERM_STARTDATE 
 and TERM_START <= @TERM_ENDDATE 
group by year(TERM_END),month(TERM_END)

Open in new window

0
 

Author Comment

by:shacho
Comment Utility
indeed
0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
sorry for duplicate, aswer was waiting for submit when I got a long call :)
0
 

Author Comment

by:shacho
Comment Utility
np - thanks for commenting.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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.
Viewers will learn how the fundamental information of how to create a table.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now