Applying dual conditions in case when in MYSQL - mysql

(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.

Related

How to select Point In Time record

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

MySQL- How to select distinct data against a same record

Referring to the question asked. I have a table which includes multiple records for the same entry like below image
All I want is to select the single record against each entry. I have also set the DB and Query in DB Fiddle
Any help would be highly appreciated.
From discussion got you need convert datetime to date and group by clause column to date
for your table
SELECT p.`zdjh` AS MSN, max(p.`sjsj`) AS Date_Time,max( p.`xhqd`) AS Ping
FROM `tj_xhqd` p
WHERE p.`sjsj` <='2018-07-24 10:15:00' AND p.`sjsj` >='2018-07-23 10:15:00'
group by zdjh
ORDER BY MSN, Date_Time, Ping DESC

MySQL Order rows by date

I'm trying to order my rows by the date that each row has.
One row has 2016-09-15 15:36 and one has 2016-08-15 13:12 How would i order them so that the highest one is at top?
Their row is called th_activity and i know that in a normal query, you would do something like SELECT * FROM threads ORDER BY th_activity DESC but i believe i know the problem. I use varchar as the type for the row. I'm not really sure what the length/value should be for date if that's what i must use.
If someone can explain how i would order this properly, i'd appreciate it.
If your date is year-month-day hour:minute, you can order alphabetically without any problems, so ORDER BY th_activity DESC should work fine.
You could have set the type of column as timstamp instead of varchar, which doesn't care about the length of data (only that while inserting data take care about the format).
Check this question to ordery by timestamp:
MYSQL - Order timestamp values ascending in order, from newest to oldest?
SELECT th_activity
FROM tableName
ORDER BY timestamp desc;
could be you need a str_to_date conversion
select * from threads
ORDER BY str_to_date(th_activity, '%Y-%m-%d %h:%i') DESC
I would use a data type
datetime
and use the same select you wrote in the question :
order by th_activity DESC

MySQL - Retrieve 7 most recent timestamps, sorted oldest first

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;

Order By on date field starting in a middle point of the dates range

I have a table "A" with a "date" field. I want to make a select query and order the rows with previous dates in a descending order, and then, the rows with next dates in ascending order, all in the same query. Is it possible?
For example, table "A":
id date
---------------------
a march-20
b march-21
c march-22
d march-23
e march-24
I'd like to get, having as a starting date "march-22", this result:
id date
---------------------
c march-22
b march-21
a march-20
d march-23
e march-24
In one query, because I'm doing it with two of them and it's slow, because the only difference is the sorting, and the joins I have to do are a bit "heavy".
Thanks a lot.
You could use something like this -
SELECT *
FROM test
ORDER BY IF(
date <= '2012-03-22',
DATEDIFF('2000-01-01', date),
DATEDIFF(date, '2000-01-01')
);
Here is a link to a test on SQL Fiddle - http://sqlfiddle.com/#!2/31a3f/13
That's wrong, sorry :(
From documentation:
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.
This should do the trick. I'm not 100% sure about adding an order in a UNION...
SELECT * FROM A where date <= now() ORDER BY date DESC
UNION SELECT * FROM A where date > now() ORDER BY date ASC
I think the real question here is how to do the joining once. Create a temporary table with the result of joining, and make the 2 selects from that table. So it will be be time consuming only on creation (once) not on select query (twice).
CREATE TABLE tmp SELECT ... JOIN -- do the heavy duty here
With this you can make the two select statenets as you originally did.