Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3967002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:40:07+00:00 2026-05-20T03:40:07+00:00

I have this query: SELECT COUNT(*) as clicks, DATE_FORMAT(FROM_UNIXTIME(click_date), ‘%w %M %Y’) as point

  • 0

I have this query:

SELECT COUNT(*) as clicks, DATE_FORMAT(FROM_UNIXTIME(click_date), '%w %M %Y') as point 
FROM tracking 
WHERE click_date < $end_date AND click_date > $start_date 
GROUP BY DAY(FROM_UNIXTIME(click_date))

Where $start_date is two weeks ago and $end_date is today’s date.

I am trying find all clicks made each day for a particular date range. I also want to include days where there has been no clicks. Since naturally there isn’t an entry for these in my database I need to include them some how, how can I best do this whilst showing all dates from start date to end date. This what I currently have, lots of gaps for this two week date range.

Array
(
    [0] => Array
        (
            [clicks] => 17
            [point] => 0 February 2011
        )

    [1] => Array
        (
            [clicks] => 3
            [point] => 1 February 2011
        )

    [2] => Array
        (
            [clicks] => 14
            [point] => 5 February 2011
        )

    [3] => Array
        (
            [clicks] => 1
            [point] => 1 February 2011
        )

    [4] => Array
        (
            [clicks] => 8
            [point] => 2 February 2011
        )

)

Can this possibly be done via a pure SQL query or do I have to use some php logic?

Btw, why do I have 0 February 2011 as my first date! Hmm, I also seem to have duplicate dates, that shouldn’t happen, maybe my GROUP BY isn’t working correctly?

Thanks all for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:40:08+00:00Added an answer on May 20, 2026 at 3:40 am

    Can this possibly be done via a pure SQL query or do I have to use some php logic?

    Yes, it is better to create a Numbers table (single column N) that contains nothing but the numbers 0 to 999. It can be used for many things, not least a query like the below:

    SELECT COUNT(t.click_date) as clicks,
        DATE_FORMAT(adddate($start_date, interval N day), '%d %M %Y') as point 
    FROM Numbers
    LEFT JOIN tracking t
        ON t.click_date >= adddate($start_date, interval N day)
        and t.click_date < adddate($start_date, interval (N+1) day)
    WHERE N between 0 and datediff($start_date, $end_date)
    GROUP BY N
    

    Btw, why do I have 0 February 2011 as my first date

    You’re using the wrong format. It’s UPPER case W not lower for day-of-week, so ‘%W %M %Y’ or ‘%d %M %Y’ for day-of-month.
    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

    maybe my GROUP BY isn’t working correctly?

    You are using GROUP BY DAY(FROM_UNIXTIME(click_date)) note “day” not weekday, but you are displaying (or trying to) “%W” (weekday) – pick one, don’t mix them.


    EDIT: If you prefer not to materialize (create as a real table) a Numbers sequence table, you can construct one on the fly. It won’t be pretty.

    Note: N1, N2 and N3 below combine to give a possible range of 0-999

    SELECT COUNT(t.click_date) as clicks,
        DATE_FORMAT(adddate($start_date, interval N day), '%d %M %Y') as point 
    FROM (
        select N1 * 100 + N2 * 10 + N3 as N
        from (
        select 0 N1 union all select 1 union all select 2 union all
        select 3 union all select 4 union all select 5 union all
        select 6 union all select 7 union all
        select 8 union all select 9) N1
        cross join (
        select 0 N2 union all select 1 union all select 2 union all
        select 3 union all select 4 union all select 5 union all
        select 6 union all select 7 union all
        select 8 union all select 9) N2
        cross join (
        select 0 N3 union all select 1 union all select 2 union all
        select 3 union all select 4 union all select 5 union all
        select 6 union all select 7 union all
        select 8 union all select 9) N3
        ) Numbers
    LEFT JOIN tracking t
        ON t.click_date >= adddate($start_date, interval N day)
        and t.click_date < adddate($start_date, interval (N+1) day)
    WHERE N between 0 and datediff($start_date, $end_date)
    GROUP BY N
    

    EDIT #2: A straight Dates table

    Put this in a new window in phpMyAdmin or run it as a batch. It creates a table named Dates, with every single date from day 1900-01-01 (or change in the script) to 2300-01-01 (or change).

    DROP PROCEDURE IF EXISTS FillDateTable;
    
    delimiter //
    CREATE PROCEDURE FillDateTable()
        LANGUAGE SQL
        NOT DETERMINISTIC
        CONTAINS SQL
        SQL SECURITY DEFINER
        COMMENT ''
    BEGIN
      drop table if exists datetable;
      create table datetable (thedate datetime primary key, isweekday smallint);
    
      SET @x := date('1900-01-01');
      REPEAT 
        insert into datetable (thedate, isweekday) SELECT @x, case when dayofweek(@x) in (1,7) then 0 else 1 end;
        SET @x := date_add(@x, interval 1 day);
        UNTIL @x > date('2300-01-01') END REPEAT;
    END//
    delimiter ;
    
    CALL FillDateTable;
    

    With such a utility table, your query can be just

    SELECT COUNT(t.click_date) as clicks,
        DATE_FORMAT(thedate, '%d %M %Y') as point 
    FROM Dates
    LEFT JOIN tracking t
        ON t.click_date >= thedate
        and t.click_date < adddate(thedate, interval 1 day)
    WHERE thedate between $start_date and $end_date
    GROUP BY thedate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.