简体   繁体   中英

SQL Statement to get daily totals

I'm storing some data in a table and I want to be able to display the total data points per day on the graph. So the first graph point might be 7 for Monday, Jan 1, 2013, and then 3 for Tuesday, Jan 2, 2013...etc.

I have full time/date stamps for each of my data points in my table of SQL type datetime .

My pseudo statement looks like this, but I'm concerned since I'm using the datetime data type:

SELECT 
    DATE(created_at) AS create_date
    COUNT(id) AS total
FROM
    data_table
GROUP BY 
    create_date

How can I get the total data points per day, regardless of the timestamp?

Try this

SELECT 
    DATE(created_at) AS create_date,
    COUNT(id) AS total
FROM
    data_table
GROUP BY 
    DATE(created_at)

Best would be to start a daily cron job that stores the number of the data points for every day. So you can every day count the number between let's say 24.00.00 to 23.59.59.

If you want to count them on the fly you might have slow requests on huge data amounts, since the grouping query cannot use table index.

But maybe you can add a new table column where you store just the date additionally to the timestamp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM