Include DateTime Even If No Corresponding Data

  • I need to create a running count of LOGINS based upon the Year, Month, Day and Hour.  If there are no logins for particular Hour, I still want to display the respective date, just with a zero value for logins. 

    What is the most efficient way to do this?

    EXAMPLE:

    2007 | 01 | 01 | 12:00 | 5 Logins

    2007 | 01 | 01 | 1:00   | 0 Logins

    2007 | 01 | 01 | 2:00   | 10 Logins

     

    My dates currently are based upon the Date of the Login so that obviously will not work since only rows 1 and 3 from above will be returned in my result set. 

    As always, I appreciate any help!

  • You can build a table that contains a record for each date and hour for the time period you want to query and left join it to your login table.


  • You may want a Calendar table in your database.  Do a search of thetopics and articles on this site, and I am sure you will find something to help you.

  • Got it.  I'll run through it.  Thanks!

  • Lynn -- I wonder what kind of rounding the forum used to make your post show up first?


  • Okay, now that was funny.

  • Yes, that was a true act of wit!

  • mrpolecat wins a piece of PIE for that one.....

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • And I bet is had nothing to do with 2/3

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Hi,

     Did u get the query, if so can you please give the same to me . i also need this.

     

    thanks in advance.

    regards,

    Hari.

  • No... no query... the recommendation was to use a "Calendar Table"... there are many to choose from if you search this site.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply