I use:
SELECT * FROM data WHERE date >= now() - INTERVAL 1 DAY
To fetch all records in the last 24h. Is there a way to "filter" from MySQL to fetch only one value of each hour in that 24h period? I dont really mind if its the 1st record or the last record of every hour.
date is in Mysql timestamp and is used as the primary key (CURRENT_TIMESTAMP).
Why? i insert a temperature record every minute and then i draw it using Google Chart. When i draw the chart of the past 24h it looks crazy!! Since its a home DIY project im fine with drawing the temperature chart in the last 24h with 1h interval.
Try this:
SELECT * FROM data WHERE date >= now() - INTERVAL 1 DAY group by hour(date)
You can group the data by the hour and select for instance the lowest id for every hour. If you join that subquery against your table - you will get the desired result
select d1.*
from data d1
join
(
SELECT min(id) as id
FROM data
group by date(date), hour(date)
) d2 on d1.id = d2.id
WHERE d1.date >= now() - INTERVAL 1 DAY
Related
My MySQL table stores records with a date/time stamp. I am wanting to find records from the table that were created yesterday (as in have a creation date of yesterday - regardless of what the timestamp portion is)
Below is what a db record looks like:
I have tried the following select (and a few other variations, but am not getting the rows with yesterday's date.
SELECT m.meeting_id, m.member_id, m.org_id, m.title
FROM meeting m
WHERE m.create_dtm = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Not exactly sure how I need to structure the where clause to get meeting ids that occurred yesterday. Any help would be greatly appreciated.
A naive approach would truncate the creation timestamp to date, then compare:
where date(m.create_dtm) = current_date - interval 1 day
But it is far more efficient to use half-open interval directly against the timestamp:
where m.create_dtm >= current_date - interval 1 day and m.create_dtm < current_date
You can try next queries:
SELECT
m.meeting_id, m.member_id, m.org_id, m.title, m.create_dtm
FROM meeting m
-- here we convert datetime to date for each row
-- it can be expensive for big table
WHERE date(create_dtm) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
SELECT
m.meeting_id, m.member_id, m.org_id, m.title, m.create_dtm
FROM meeting m
-- here we calculate border values once and use them
WHERE create_dtm BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_SUB(CURDATE(), INTERVAL 1 SECOND);
Here live fiddle: SQLize.online
I have a query like this.
SELECT IFNULL(SUM(price),0) as total FROM table1
WHERE table1.id= 33 AND start_time BETWEEN '2019-09-1' AND '2019-09-11'
This works fine. Now I want to SUM the data of a month so I tried to use the between feature and get first day and last day of the month.
SELECT IFNULL(SUM(price),0) as total FROM table1
WHERE table1.id = 33 AND start_time BETWEEN (SELECT DATEADD(month, DATEDIFF(month,
'2019-09-15'), 0) AND '2019-09-11'
I used this ref
You can get last_day() function without any argument to get the last day of month, and date_format(#mydate, '%Y-%m-01') to get the first day of the month. So, use :
set #mydate='2019-09-15';
select ifnull(sum(price),0) as total
from table1
where id = 33
and start_time between date_format(#mydate, '%Y-%m-01') and last_day(#mydate);
You can calculate the first day of the month by subtracting the day of the month and adding one day:
SELECT IFNULL(SUM(price), 0) as total
FROM table1
WHERE table1.id = 33 AND
start_time >= date('2019-09-15') + interval (1 - day(date('2019-09-15'))) day and
start_time < date('2019-09-15')
sql query showing data for today and yesterday but i want data for current date only
select * from branches
where NOT EXISTS
(
select *
from mytable
where mytable.branch_code = branches.Branch_Code
and mytable.date BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE()
)
getting data for current date and yestarday
I have query that counts the number of distinct values of a given field for a given week that where not recorder before. It counts the number of new values for the week.
SELECT
COUNT(DISTINCT (field))
FROM
table
WHERE
(creation BETWEEN CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY AND CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) - 2 DAY)
AND field NOT IN (SELECT DISTINCT
(field)
FROM
table
WHERE
creation < CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY);
I need to extend the query to be able to do the same computation for all the weeks in the past two months.
How can I do it?
Well, if I understand this correctly, a field occurs in some week for the first time. You want to count per week how many such first occurrences exist. So get the first date per field first. Then count per week.
select
yearweek(first_occurence),
count(*)
from
(
select field, min(creation) as first_occurence
from table
group by field
) fields
group by yearweek(first_occurence)
order by yearweek(first_occurence);
MY query looks like this:
SELECT COUNT(entryID)
FROM table
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Will this count the rows whose date values are within the day (starting at 12:00; not within 24 hours)? If not, how do I do so?
The following should be enough to get records within the current day:
SELECT COUNT(entryID)
FROM table
WHERE date >= CURDATE()
As Michael notes in the comments, it looks at all records within the last two days in its current form.
The >= operator is only necessary if date is actually a datetime - if it's just a date type, = should suffice.
Here's the solution:
SELECT COUNT(entryID)
FROM table
WHERE DATE(date) >= CURDATE()
Since my date column is type DATETIME, I use DATE(date) to just get the date part, not the time part.
CURDATE() returns a date like '2012-03-30', not a timestamp like '2012-03-30 21:38:17'. The subtraction of one day also returns just a date, not a timestamp. If you want to think of a date as a timestamp think of it as the beginning of that day, meaning a time of '00:00:00'.
And this is the reason, why this
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
and this
WHERE date > CURDATE()
do the same.
I have another hint: SELECT COUNT(entryID) and SELECT COUNT(*) give the same result. SELECT COUNT(*) gives the database-machine more posibilities to optimize counting, so COUNT(*) is often (not always) faster than COUNT(field).