SQL
  Home arrow SQL arrow Page 4 - Creating SQL Reports Based on Date Criteri...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SQL

Creating SQL Reports Based on Date Criteria
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2007-12-20

    Table of Contents:
  • Creating SQL Reports Based on Date Criteria
  • Monthly Totals
  • Current Month
  • Year-to-Date Totals

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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
        
    ->     
    AND YEAR(whn)=YEAR(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') 

     

    95

     


    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');   

     

     

    whn

     whnFiscalYear  currentFiscalYear 

     

    2006-03-29 |

    2005

    2006

    2006-04-20 |

    2006

    2006

     

    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.

       · This article is an excerpt from the book "SQL Hacks," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from chapter four of the book SQL Hacks, written by Andrew Cumming and Gordon Russell (O'Reilly, 2006; ISBN: 0596527993). Check it out today at your favorite bookstore. Buy this book now.

    SQL ARTICLES

    - 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







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway