SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Query database for missing dates (not sunday)

  1. #1
    SitePoint Zealot maffp's Avatar
    Join Date
    Jun 2005
    Location
    UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query database for missing dates (not sunday)

    Hi!

    I have a database of shop takings for each date in it's history which I update daily. However, sometimes I forget a day and don't pick it up till much later. I was wondering if there was a way to query a database for missing dates so that I can complete an entry for it... excluding sundays (we never open on sundays).

    Cheers!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    The easiest way is probably to select all shop, dates, where takings = 0 then in your php logic test the condition that day of week is not 7.

    PHP Code:
    // emulating your results set from your SQL
    $rows[] = array( "shop" => "City centre""takings" => 0"day"=>"2010-05-02" );
    $rows[] = array( "shop" => "City centre""takings" => 0"day"=>"2010-05-03" );

    foreach( 
    $rows as $row){
    if( 
    // check the day is not number 7
    date'N' strtotime$row['day']) ) != 
    )
    {
    echo 
    'oops, missed one ... ' $row['shop'] . ' on ' $row['day'] ;
    }
    }

    // oops, missed one ... City centre on 2010-05-03 
    There may be a way to do it in your SQL select, but ask that question on one of the dedicated sql forums.

    Best way though would be to automate the inputting of takings somehow ...

  3. #3
    SitePoint Zealot maffp's Avatar
    Join Date
    Jun 2005
    Location
    UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups and cheers for the reply!

    I should probably have given a little more detail before. Uncompleted dates will currently not exist in the table at all as they are added on a daily basis. The table layout is like this:

    [CODE]
    date | nettax | tax | cash | cheque | card
    [CODE]

    Would love to automate it but we only use an old cash register... not an epos system.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    Right, so I think I misunderstood, you only have one shop?

    You just want to know the dates where there is no row for takings (except sunday).

    Now that does sound like it is begging an SQL solution ... which I am not qualified to answer.

    You could probably cook up something with PHP to get say, missing days for a particular month.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    http://www.sitepoint.com/forums/showthread.php?t=430007

    Theres an old reply about a similar question ...

    Then you'd need still need to filter out Sundays.

  6. #6
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,455
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    thanks cups
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •