I have a MariaDB 10.2.21.
My table contains records that include a 'ChangeDate' field like this: '2019-09-18 10:57:26'.
I want to do a SELECT based on a timestamp, and get the nearest previous record to return.
This allows me to do a 'point-in-time' selection providing me with field values as they were at that moment.
I seeked StackOverflow but do no recognize a proper solution.
Any hints? Thanks!
Try following Query
SELECT *
FROM my_table
WHERE ChangeDate < '2019-09-18 10:57:26'
ORDER
BY ChangeDate DESC
LIMIT 1
Related
I want to select a latest record from MySQL table using MAX. Date column that I apply MAX to is selected properly, but second column the ID is not. Following screenshot expalins this best:
As you see 1_2018 is selected instead of 15_2018.
You could use your query as a sub query to pick the row(s) for max created value
select *
from business_trip
where created = (
select max(created )
from business_trip
where year_id LIKE '%_2018'
)
SELECT created, year_id
FROM business_trip
WHERE created = (SELECT MAX(created) FROM business_trip);
In this case I'm here using sub query to satisfy your requirement. Other answers used ORDER BY (which will not give the expected answer until the year_id is MAX) and other with MAX() in SELECT will not pull the related record with latest created value.
In my query I'm also avoided use of check over year_id so this will always return you the record with latest created value.
you can sort by the created and then LIMIT 1 to get only a single record. Something like this
SELECT created,year_id
FROM business_trip
WHERE year_id LIKE '%_2018'
ORDER BY created DESC LIMIT 1;
(question edited) My table has id,status,date,sequence
Situation: Get ids which satisfies:
date is max, not more than today's and status is A
if more than 1 status' with same date then get id only if it has max sequence
I am writing MYSQL in dbVisulizer.
edit: trying with query:
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
It sounds like I am just asking direct question without trying anything by myself, but for this situation I am completely stuck, I tried with case when but couldn't really accomplish any good, any starting point would be appriciated.
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
group_by id;
This should give you the desired results. Adapt table and field names to your table and fields.
select id from
table
where
table.date<now() and table.status='A'
order by date desc,sequence desc
limit 1;
You should check it for mistakes by yourself.
As above, I'm unsure on how to do it, although I am possibly overlooking something simple
I want to retrieve the timestamps and another column, just the most recent 7 which I have done using LIMIT and ordered them using ORDER BY timestamp DESC to get the most recent 7... But once retrieved, I'd like them oldest first rather than newest first
Anyone able to assist please?
Thanks!
two selects could work in this case. it would at least be one possible way to achieve what you want. i'm not sure if it would the best way.
i'm assuming your table has an id field.
select * from records
where id in(select id from records order by timestamp desc limit 7)
order by timestamp asc;
this lets you get the latest 7 rows in the inner select, then sort them in ascending order.
A nested query should take care of this. Something like
SELECT *
FROM (
SELECT timestamp, anotherColumn
FROM tableName
ORDER BY timestamp DESC
LIMIT 7 )
ORDER BY timestamp ASC;
This might be a simple one, but since I don't have much knowledge about MySQL I don't know how to do this, This is what I basically want,
I have a query like this
//time format "yyyy-MM-dd"
SELECT ID
FROM `id_table`
WHERE time > "2012-01-05 " AND time < "2012-01-10";
But in the id_table I have data only up to 2012-01-04 then it starts again from "2012-01-20", so above query would return null. Is there a any way where I can retrieve the last data record from the table, as for this example can I get the ID of 2012-01-04 date from the table when I query like this
SELECT ID
FROM `id_table`
WHERE time > "2012-01-05"
Are you looking for the one (i assume max ID) ID of the row with the nearest time to 2010-01-05?
SELECT MAX(ID) as LastId FROM id_table
WHERE time = (SELECT MAX(time)
FROM id_table WHERE time < '2012-01-05')
Try this:
SELECT ID FROM id_table
WHERE time between
least((select max(time) from id_table), "2012-01-05") AND "2012-01-10";
Note that between will get data from "2012-01-10" and ("2012-01-05" OR "2012-01-05)
To get the record closest to the given time ( you could easily tweak this if you care about dealing with duplicates, if that doesn't matter then this alone should suffice)
SELECT
ID
FROM
id_table
ORDER BY ABS(DATEDIFF(time, '2012-01-05'))
LIMIT 1
NOTE: If the time field is a time value yyyy-mm-dd hh:mm:ss then you can use TIMEDIFF for a more accurate comparison.
OP If you will explain more on how you would like to handle different cases I can tweak this example to suit them. Cheers.
This simple SQL problem is giving me a very hard time. Either because I'm seeing the problem the wrong way or because I'm not that familiar with SQL. Or both.
What I'm trying to do: I have a table with several columns and I only need two of them: the datetime when the entry was created and the id of the entry. Note that the hours/minutes/seconds part is important here.
However, I want to group my selection according to the DATE part only. Otherwise all groups will most likely have 1 element.
Here's my query:
SELECT MyDate as DateCr, COUNT(Id) as Occur
FROM MyTable tb WITH(NOLOCK)
GROUP BY CAST(tb.MyDate as Date)
ORDER BY DateCr ASC
However I get the following error from it:
Column "MyTable.MyDate" is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I don't do the cast in the GROUP BY, everything fine. If I cast MyDate to DATE in the SELECT and keep the CAST from GROUP BY, everything fine once more. Apparently it wants to keep the same DATE or DATETIME format in the GROUP BY as in the SELECT.
My approach can be completely wrong so I am not necessarily looking to fix the above query, but to find the proper way to do it.
LE: I get the above error on line 1.
LE2: On a second look, my question indeed is not very explicit. You can ignore the above approach if it is completely wrong. Below is a sample scenario
Let me tell you what I need: I want to retrieve (1) the DateTime when each entry was created. So if I have 20 entries, then I want to get 20 DateTimes. Then if I have multiple entries created on the same DAY, I want the number of those entries. For example, let's say I created 3 entries on Monday, 1 on Tuesday and 2 today. Then from my table I need the datetimes of these 6 entries + the number of entries which were created on each day (3 for 19/03/2012, 1 for 20/03/2012 and 2 for 21/03/2012).
I'm not sure why you're objecting to performing the CONVERT in both the SELECT and the GROUP BY. This seems like a perfectly logical way to do this:
SELECT
DateCr = CONVERT(DATE, MyDate),
Occur = COUNT(Id)
FROM dbo.MyTable
GROUP BY CONVERT(DATE, MyDate)
ORDER BY DateCr;
If you want to keep the time portion of MyDate in the SELECT list, why are you bothering to group? Or how do you expect the results to look? You'll have a row for every individual date/time value, where the grouping seems to indicate you want a row for each day. Maybe you could clarify what you want with some sample data and example desired results.
Also, why are you using NOLOCK? Are you willing to trade accuracy for a haphazard turbo button?
EDIT adding a version for the mixed requirements:
;WITH d(DateCr,d,Id) AS
(
SELECT MyDate, d = CONVERT(DATE, MyDate), Id
FROM dbo.MyTable)
SELECT DateCr, Occur = (SELECT COUNT(Id) FROM d AS d2 WHERE d2.d = d.d)
FROM d
ORDER BY DateCr;
Even though this is an old post, I thought I would answer it. The solution below will work with SQL Server 2008 and above. It uses the over clause, so that the individual lines will be returned, but will also count the rows grouped by the date (without time).
SELECT MyDate as DateCr,
COUNT(Id) OVER(PARTITION BY CAST(tb.MyDate as Date)) as Occur
FROM MyTable tb WITH(NOLOCK)
ORDER BY DateCr ASC
Darren White