SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    group by date even if null

    hi all

    just a quick query
    say i have a query ->
    Code:
     select date, count(something) from table group by date
    if count is nothing or null, it doesn't seem to return anything
    how can i get it to print null or zero and the date so that there are no gaps in the report

    eg.
    12-03-05 1
    13-03-05 2
    15-03-05 3
    16-03-05 3

    where 14-03-05 was zero or null, so didn't return anything

    thanks
    dave

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    generate your dates using an integers table

    let's say you wanted all 31 days in march --
    Code:
    create table integers (i integer);
    insert into integers (i) values
    (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
    
    select date_add('2005-03-01', interval 10t.i+u.i day) 
                     as thedate
         , count(something) as thecount 
      from integers as t
    cross
      join integers as u  
    left outer
      join yourtable
        on date_add('2005-03-01', interval 10t.i+u.i day)
         = yourdatefield
     where 10t.i+u.i between 0 and 30
    group
        by thedate
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another (not so nice, but perhaps more practical) solution would be to print zeroes when fetching / outputting data at application level, something like:

    Code:
    for i = 1 to 31
       result[i] = 0
    qry = SELECT .... GROUP BY day
    while(record = fetch(qry))
       result[record[day]] = record[value]

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks guys
    stereofrog, possible to do that i guess, but the query is just for use with phpmyadmin


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
  •