I need a query that returns the count every hour for the last 48 hours.
Until now I tried either:
SELECT count(*), date_format(created, '%H:%i - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 24 HOUR
group by floor(hour(created))
order by created ASC
and:
SELECT count(*), date_format(created, '%H:%i - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 48 HOUR
group by floor(hour(created))
order by created ASC
But neither of those give the wanted result.
Do you have any idea on how I could do this?
Thanks
If you want every hour, don't you want this:
SELECT count(*), date_format(created, '%H - %d/%m/%y') as datecreated
FROM mimesi_indexer.served_clips
where created > NOW() - INTERVAL 24 HOUR
group by date_format(created, '%H - %d/%m/%y')
order by min(created) ASC;
That is, you need to include the day as well as the hour -- and remove the minutes from datecreated.
Related
I have an sql query and I need to get the count from the last 48 hours.
sql query
SELECT count(*), created
AS countMeta
FROM mimesi_indexer.meta_served_clips
GROUP BY created > DATE(NOW() - INTERVAL 48 HOUR)
I tried this query but it's not working, can someone tell me how to fix it or how to do it from scratch please?
Thanks
If created is a timestamp field and you want to count all rows created in the last 48 hours, your query should be written as this:
select
count(*) AS countMeta
from
mimesi_indexer.meta_served_clips
where
created > NOW() - INTERVAL 48 HOUR
If you want to show the count of every hour in the last 48 hours, one way is this:
select
date_format(created, '%Y/%m/%d %h') as day_hour_created,
count(*)
from
mimesi_indexer.meta_served_clips
where
created > NOW() - INTERVAL 48 HOUR
group by
date_format(created, '%Y/%m/%d %h')
using the string function date_format we can extract only the day and hour information from the created field, and group by this string. Then you might also want to change the condition
created > NOW() - INTERVAL 48 HOUR
to this:
created > date_format(now()- interval 48 hour, '%Y/%m/%d %h')
to skip the minute and second part, in order to make the first hour complete.
you should use mysql DATE_SUB() instead of DATE() function to get the records of last 48 hours
SELECT count(*), created
AS countMeta
FROM mimesi_indexer.meta_served_clips
GROUP BY created > DATE_SUB(now(), INTERVAL 48 HOUR)
I have a table name transactions where data rows are stored with a date
Like
`trans_id` `amount` `tdate`
I want to filter that data like last 30 days, last 31st 60 days, last 61-90 days calculate the overall amount also
My queries are
For last 30 days
SELECT SUM(amount) AS amt FROM transactions GROUP BY DATE(tdate) ORDER BY DATE(tdate) DESC LIMIT 30
Working fine and show SUM of amount (last 30days)
But for last 31-60 days not working
SELECT SUM(amount) AS amt FROM transactions GROUP BY DATE(tdate) ORDER BY DATE(tdate) DESC LIMIT 60,31
How to solve it ? I do want to include only 31 to 60 days amount only
Use the following: (It returns data between today and last 30 days)
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y')
FROM Table
WHERE DateCol BETWEEN CURDATE() - INTERVAL
30 DAY AND CURDATE()
Or more precisely, this will do the trick:
DateCol BETWEEN (NOW() - INTERVAL 60 DAY)
AND (NOW() - INTERVAL 30 DAY)
CURDATE() works for only date portion. If you have DateTime column, then NOW() will do.
I have a query that I need to modify to use data variables to search counts for the last 30 days from today and group by date. My SQL server's /tmp/ directory (15GB) keeps filling up and the query fails.
SELECT DATE(`date_time`) AS DAY
, COUNT(DISTINCT(rcid)) AS COUNT
, COUNT(DISTINCT(tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE"%VOE%"
AND DATE_SUB(NOW(), INTERVAL 30 DAY) and NOW()
GROUP BY DAY;
Can you run the query manually in a shell? Looks like you're almost there. It may be failing on the group by. At this point in the execution it doesn't know what column DAY is.
Try: GROUP BY DATE(date_time)
Instead of: GROUP BY DAY.
Your date check is missing the column name to test, and also missing the BETWEEN keyword between the two dates.
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%"
AND date_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY DAY;
Try this:
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE
"%VOE%" AND `date_time` BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY `date_time`;
Thanks all. I got t to work using CURDATE()
AND `date_time` BETWEEN CURDATE() - INTERVAL 30
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%" AND `date_time` BETWEEN CURDATE() - INTERVAL 30 DAY AND NOW() GROUP BY DAY;
I am trying to count how many hours of the day there is data in the database.
I use this query:
SELECT
HOUR(date) AS `hour`, COUNT(date)
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
I got the query from here
example:
hour COUNT(date)
00 55
01 2
02 33
Now I want calculate how many hours there was data?
Above example should output value 3, because there was data at hour 00, 01 and 02
something would like to add COUNT(hour)like this:
SELECT
HOUR(date) AS `hour`, COUNT(date)**,COUNT(hour)**
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
select count(*)
from
(
SELECT HOUR(date) AS hour
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
) tmp
you are looking for count(distinct column) https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count-distinct
SELECT count(distinct HOUR(date)) AS distinct_hour_count
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
I want to get the value of users visiting my page for 10 days in a chart. I need to COUNT() all the values from the last ten days.
The best layout would be
Day|COUNT(ip)
1 - 10
2 - 12
3 - 52
......
I hope you understand what I mean.
Can MySQL do this directly or need I to do this in PHP in 10 seperate querys?
Regards,
Moritz
Update with Tablestructure:
Id (Auto Increment)|Time (Unix Timestamp)|Ip|Referer
This should run fast for you
SELECT COUNT(ip) ipcount,dt FROM
(
SELECT ip,DATE(FROM_UNIXTIME(`Time`)) as dt FROM mytable
WHERE `Time` > TO_UNIXTIME(NOW() - INTERVAL 10 DAY)
) A GROUP BY dt;
Make sure you have an index on Time
ALTER TABLE mytable ADD INDEX TimeIndex (`Time`);
This will give you results with actual date values:
SELECT
COUNT(DISTINCT ip),
FROM_UNIXTIME(Time, '%m/%d/%Y') AS Day
FROM
tbl
WHERE
Time >= UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -10 DAY))
GROUP BY
FROM_UNIXTIME(Time, '%m/%d/%Y')
try this:
SELECT CAST(DATE(FROM_UNIXTIME(`Time`)) AS CHAR) as dateoftime, COUNT(Ip) as cnt
FROM tablename
WHERE DATE(FROM_UNIXTIME(`Time`)) > DATE_SUB(current_timestamp, INTERVAL 10 DAY)
GROUP BY CAST(DATE(FROM_UNIXTIME(`Time`)) AS CHAR)