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 7772565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:55:05+00:00 2026-06-01T16:55:05+00:00

I have a table with 3 columns: id , updated_at , click_sum . Many

  • 0

I have a table with 3 columns: id, updated_at, click_sum.

Many rows have the exact same updated_at value which makes it hard to simply retrieve the data, order by updated_at and display the sums in a chart.
Since there are multiple sums for the same dates which screws the chart.

What I try to achieve is to get the following output:

 update_at | click_sum
-----------+-----------
   date1   |    100
   date2   |     3
   date3   |    235
   date4   |    231

Optionally only those dates which are form the last month, week or day AND not simply the dates which are NOW() - 1 month.

The current query I build is very large and doesn’t work that well.
It groups by dates (no duplicated dates appear) and SUM()s the clicks correctly but defining from when (last month, week, day) the dates are doesn’t seem to work properly.

Query: ($interval stands for MONTH or DAY or SECOND or WEEK)

SELECT d.updated_at, SUM(d.clicks_sum) AS click_sum
FROM aggregated_clicks d
JOIN 
(
     SELECT c.id, MAX(StartOfChains.updated_at) AS ChainStartTime
     FROM aggregated_clicks c
     JOIN 
     (
         SELECT DISTINCT a.updated_at
         FROM aggregated_clicks a
         LEFT JOIN aggregated_clicks b ON (b.updated_at >= a.updated_at - INTERVAL 1 DAY AND b.updated_at < a.updated_at)
         WHERE b.updated_at IS NULL
      ) StartOfChains  ON c.updated_at >= StartOfChains.updated_at
     GROUP BY c.id
) GroupingQuery
ON d.id = GroupingQuery.id
WHERE GroupingQuery.ChainStartTime >= DATE_SUB(NOW(), INTERVAL 1 $interval)
GROUP BY GroupingQuery.ChainStartTime
ORDER BY GroupingQuery.ChainStartTime ASC
  • 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-06-01T16:55:06+00:00Added an answer on June 1, 2026 at 4:55 pm

    maybe I’m assuming too much about the nature of your question (and the table it refers to), but I think this can be done much more simply than the query you’ve shown.

    figuring the latest completed month isn’t very hard.

    it starts with knowing the first date of this current month — use this:

    date_sub(curdate(), interval (extract(day from curdate())-1) day)
    

    and to know the first day of that previous month, use this:

    date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
    

    so if you want to get the sums for just the days in between — i.e. the latest completed month, use this:

    select updated_at, sum(click_sum) from aggregated_clicks
      where updated_at >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
        and updated_at < date_sub(curdate(), interval (extract(day from curdate())-1) day)
      group by updated_at;
    

    figuring the lastest completed week is just as easy. this example will assume a Sunday-Saturday week.

    because of the way the ODBC standard defines date numbers, it’s easy to find the end (Saturday) of the previous week:

    date_sub(curdate(), interval dayofweek(curdate()) day)
    

    and the beginning (Sunday) of that week is six days before that:

    date_sub(curdate(), interval (dayofweek(curdate())+6) day)
    

    so if you want to get the sums for just the days in between — i.e. the latest completed week, use this:

    select updated_at, sum(click_sum) from aggregated_clicks
      where updated_at >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
        and updated_at <= date_sub(curdate(), interval dayofweek(curdate()) day)
      group by updated_at;
    

    and of course figuring based on the latest completed day is super easy.

    to get the date of the previous day, use this:

    date_sub(curdate(), interval 1 day)
    

    so if you want the sums just for yesterday, use this:

    select updated_at, sum(click_sum) from aggregated_clicks
      where updated_at = date_sub(curdate(), interval 1 day)
      group by updated_at;
    

    NOTE: I’ve tested these queries using MySQL 5.1, YMMV.

    ———-

    UPDATE: since the date column is a datetime, simply change all references to updated_at in my queries to date(updated_at) like so:

    month case:

    select date(updated_at), sum(click_sum) from aggregated_clicks
      where date(updated_at) >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
        and date(updated_at) < date_sub(curdate(), interval (extract(day from curdate())-1) day)
      group by date(updated_at);
    

    week case:

    select date(updated_at), sum(click_sum) from aggregated_clicks
      where date(updated_at) >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
        and date(updated_at) <= date_sub(curdate(), interval dayofweek(curdate()) day)
      group by date(updated_at);
    

    yesterday case:

    select date(updated_at), sum(click_sum) from aggregated_clicks
      where date(updated_at) = date_sub(curdate(), interval 1 day)
      group by date(updated_at);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table with columns ID,SUBJECT,BRANCH i have select the rows which satisfies
I have a table with columns 'id', 'name', 'value', 'uniqueConst' in both databases. Now
I have a Rails model called Person which has database table columns for first_name
The table columns have the data type BLOB and CLOB. What are the corresponding
I have table with 3 columns A B C. I want to select *
I have table with two columns (startdate & enddate) and I have two submission
I have a table with columns ID, DateStamp and the ID need not be
I have a table with columns Index, Date where an Index may have multiple
I have a table whose columns are varchar(50) and a float . I need
If I have a table with columns id , name , score , date

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.