Home arrow SQL arrow Page 4 - Creating SQL Reports Based on Date Criteria

Creating SQL Reports Based on Date Criteria

In this third part of a four-part series covering SQL date calculations, you'll learn how to generate a report based on various date criteria. This article is excerpted from chapter four of the book SQL Hacks, written by Andrew Cumming and Gordon Russell (O'Reilly, 2006; ISBN: 0596527993). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
December 20, 2007
  1. · Creating SQL Reports Based on Date Criteria
  2. · Monthly Totals
  3. · Current Month
  4. · Year-to-Date Totals

print this article

Creating SQL Reports Based on Date Criteria - Year-to-Date Totals
(Page 4 of 4 )

To calculate year-to-date totals you must make sure that the year matches the current date and that the records occur on or before the current date:

  mysql> SELECT COUNT(v), SUM(v) FROM t
      ->   WHERE whn <= CURRENT_DATE
  | COUNT(v) | SUM(v) |
  |       42 |   2239 |

Fiscal year to date.Suppose you are reporting over a 365-day period, but your year does not start on January 1. This is the case with reports over a fiscal year or tax year.

For instance, say that your fiscal year starts on April 6. Calculating which dates are in the current fiscal year is rather complicated; the easiest thing to do is to work with the number of days between January 1 and April 6. You can get SQL to do the calculation as follows:

  mysql> select DATEDIFF(DATE '2006-04-06',DATE '2006-01-01'); 



 DATEDIFF(DATE '2006-04-06',DATE '2006-01-01') 




In SQL Server, theDATEDIFFfunction needs another parameter. You use'd'to indicate that you want the result as the number of days:DATEDIFF('d', '2006-04-06','2006-01-01').

In Oracle, you can simply subtract dates to get the number of days between them as an integer:DATE '2006-04-06'-DATE '2006-01-01'.

Once you have this offset you can determine the relevant fiscal year by subtracting this from both the date to be tested and the current date. This means that you donít have to worry about the different cases. In this example, March 29, 2006 is in fiscal year 2005, but April 20, 2006 is in fiscal year 2006:

  mysql> SELECT whn,
      ->        YEAR(whn - INTERVAL '95' DAY)                  whnFiscalYear,
      ->        YEAR(CURRENT_DATE - INTERVAL '95' DAY)    currentFiscalYear
      ->   FROM t
      ->  WHERE whn IN (DATE '2006-03-29', DATE '2006-04-20');   




 whnFiscalYear  currentFiscalYear 


2006-03-29 |



2006-04-20 |




You can then use this as a condition to ensure that you are reporting on only the current fiscal year:

  mysql> SELECT MIN(whn),MAX(whn), COUNT(v), SUM(v) FROM t
      ->    WHERE whn <= CURRENT_DATE
      ->      AND YEAR(whn - INTERVAL '95' DAY)=
      ->          YEAR(CURRENT_DATE - INTERVAL '95' DAY);



 MIN(whn)  MAX(whn)COUNT(v) SUM(v)


2006-04-09 2006-06-17 28 1443


The minimum and maximum relevant dates are included in the output. This is a complicated expression and you might want to check by hand that theMIN(whn)value shown matches the first record following 2006-04-06 and that theMAX(whn)value is the last record to the current date.

In SQL Server, you can invoke theDATEADDfunction:DATEADD('d', whn, -95).

Perhaps your fiscal year is not a fixed number of days relative to January 1. In that case, you really have no alternative than to record the start-of-year dates in a table.

Suppose thetaxYeartable was created with the following format:

  mysql> SELECT * FROM taxYear;
  | strt       |
  | 2005-04-06 |
  | 2006-04-06 |
  | 2007-04-07 |

You can perform the same calculation as performed earlier:

  mysql> SELECT MIN(whn), MAX(whn), COUNT(v), SUM(v)
      ->   FROM t,
->     (SELECT MAX(strt) txStrt FROM taxYear
->       WHERE strt < CURRENT_DATE) tx
     -> WHERE whn >= txStrt AND whn <= CURRENT_DATE;



MIN(whn) MAX(whn)  COUNT(v) SUM(v) 


 2006-04-092006-06-17  28 1443


Please check back next week for the conclusion to this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Focusing SQL Queries
- Complex SQL Queries
- A Close Look at the SQL Query
- Generating Reports with SQL Date Handling
- Creating SQL Reports Based on Date Criteria
- SQL Date Handling and Data Trends
- Date Handling
- Introduction to SQL
- Lies, Damn Lies, Statistics, and SQL

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials