How to keep the latest record of a table - mysql

I have a table with following structure
Date
train
time1
train1
time2
train2
time3
train1
time4
train2
I want to create a new table and keeping only the latest record of each distinct train
Date
train
time3
train1
time4
train2
How should I achieve so?

One method is for selecting the most recent rows is:
select t.*
from releng_retry_test_phases t
where t.date = (select max(t2.date) from releng_retry_test_phases t2 where t2.train = t.train);
If you actually want to modify the table and delete the older rows;
delete t
from releng_retry_test_phases t join
(select t2.train, max(date) as max_date
from releng_retry_test_phases t2
group by t2.train
) t2
using (train)
where t.date < t2.max_date;

You can use ROW_NUMBER() to identify the rows you want:
select date, train
from (
select *,
row_number() over(partition by train order by date desc) as rn
) x
where rn = 1

WITH temp As(
SELECT *, Row_Number() over (PARTITION BY train ORDER BY date DESC ) as
rowNumber FROM table
)
SELECT date, train FROM temp WHERE rowNumber = 1
You can use row_number() method.

Related

I need to get last created eligible rider ids and pinged rider ids accordeing to a orderId using a sql query

I need to get my data set as this table
I am trying to get eligible set like this, need to group_concat pinged set also
x.id IN (SELECT MAX(x.id) FROM x WHERE ping rider id IS NULL GROUP BY orderId)
You can assign a group based on the cumulative number of non-null values in eligible_riders. Then aggregate and take the last value:
select og.*
from (select order_id, grp, max(eligible_riders) as eligible_riders,
group_concat(rider_id) as riders,
row_number() over (partition by order_id order by min(id) desc) as seqnum
from (select t.*,
sum(eligible_riders <> '') over (partition by order_id order by id) as grp
from t
) t
group by order_id, grp
) og
where seqnum = 1;
Hmmm . . . You could also do this with a correlated subquery, which might look a bit simpler:
select order_id, max(eligible_riders) as eligible_riders,
group_concat(rider_id) as riders
from t
where t.id >= (select max(t2.id)
from t t2
where t2.order_id = t.order_id and
t2.eligible_riders <> ''
)
group by order_id;
For performance, you want an index on (order_id, eligible_riders).

Group by date and take the last one

This is my table :
What I'm trying to do, is to take the last disponibility of a user, by caserne. Example, I should have this result :
id id_user id_caserne id_dispo created_at
31 21 12 1 2019-10-24 01:21:46
33 21 13 1 2019-10-23 20:17:21
I've tried this sql, but it does not seems to work all the times :
SELECT * FROM
( SELECT id, id_dispo, id_user, id_caserne, MAX(created_at)
FROM disponibilites GROUP BY id_user, id_caserne, id_dispo
ORDER BY created_at desc ) AS sub
GROUP BY id_user, id_caserne
What am I doing wrong ?
I would simply use filtering in the where clause using a correlated subquery:
select d.*
from disponibilites d
where d.created_at = (select max(d2.created_at)
from disponibilites d2
where d2.id_user = d.id_user
);
EDIT:
Based on your comments:
select d.*
from disponibilites d
where d.created_at = (select max(d2.created_at)
from disponibilites d2
where d2.id_user = d.id_user and
d2.id_caserne = d.id_caserne
where date(d2.created_at) = date(d.created_at)
);
You can use a correlated subquery, as demonstrated by Gordon Linoff, or a window function if your RDBMS supports it:
select * from (
select
t.*,
rank() over(partition by id_caserne, id_user order by created_at desc) rn
from disponibilites t
) x
where rn = 1
Another option is to use a correlated subquery without aggregation, only with a sort and limit:
select *
from mytable t
where created_at = (
select created_at
from mytable t1
where t1.id_user = t.id_user and t1.id_caserne = t.id_caserne
order by created_at desc
limit 1
)
With an index on (id_user, id_caserne, created_at), this should be a very efficient option.
you can join your max(created_date) to your original table
select t1.* from disponibilites t1
inner join
(select max(created_at), id_caserne, id
from disponibilites
group by id_caserne, id) t2
on t2.id = t1.id

SQL get Max Date

I need to have the last price for each product for each client . I am not really good with SQL and I don't understand how I can do it.
Data :
What I want :
It is possible to have this data with a SQL request ?
Use window function ROW_NUMBER(), if available in your RDBMS:
SELECT product, price, date, client
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY product, client ORDER BY date DESC) rn
FROM mytable t
) x
WHERE rn = 1
In MySQL < 8.0:
SELECT product, price, date, client
FROM mytable t
WHERE NOT EXISTS (
SELECT 1
FROM mytable t1
WHERE t1.client = t.client AND t1.product = t.product AND t1.date > t.date
)
One option could be a correlated subquery
SELECT product, price, date, client
FROM tablename a where date =
(select max(date) from tablename b where a.product=b.product)

How to select all the rows having same and latest value in a particular column from database mysql

I want to select all the rows that are having the same and latest time in a particular column i.e., time1. The below image gives a brief idea about my question. Please guide me with this. Thank you in advance.
Here is a solution that relies on window function RANK(), available since MySQL 8.0:
SELECT *
FROM (SELECT id, name, time1, RANK() OVER(ORDER BY time1 DESC) rnk FROM mytable) x
WHERE rnk = 1
The inner query assignes a rank to each record, sorted by descending time; top ties get the same rank, 1. Then the outer query just filters in records having rank 1.
You could use a sunquery for max time
select * from my_table
where time1 = (
select max(time1)
from my_table
)
Please try this ,
select * from table1 where time1 =(select time1 from table1 order by time1 limit 0,1);
or
select * from table1 where time1 = ( select max(time1) from table1 )
or
select * from table1 where time1=(select TOP 1 time1 from table1 order by time1 desc )
DEMO

Get last data for contracts

I want to select last information about client's balance from MySQL's database. I wrote next script:
SELECT *
FROM
(SELECT
contract_balance.cid,
/*contract_balance.yy,
contract_balance.mm,*/
contract_balance.expenses,
contract_balance.revenues,
contract_balance.expenses + contract_balance.revenues AS total,
(CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE)) AS dt
FROM contract_balance
/*WHERE
CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE) < '2013-11-01'
LIMIT 100*/
) AS tmp
WHERE tmp.dt = (
SELECT MAX(b.dt)
FROM tmp AS b
WHERE tmp.cid = b.cid
)
But server return:
Table 'clientsdatabase.tmp' doesn't exist
How to change this code for get required data?
Try this one in your subquery you are trying to get the MAX of (CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE)) AS dt but in subquery your aliased table tmp doesn't exist so the simplest way you can do is to calculate the MAX of dt and use GROUP BY contract_balance.cid contractor id ,i guess it will fullfill your needs
SELECT
contract_balance.cid,
contract_balance.expenses,
contract_balance.revenues,
contract_balance.expenses + contract_balance.revenues AS total,
MAX((CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE))) AS dt
FROM contract_balance
GROUP BY contract_balance.cid
Try this:
SELECT *
FROM (SELECT cb.cid, cb.expenses, cb.revenues, cb.expenses + cb.revenues AS total,
(CAST(CAST(CONCAT(cb.yy,'-',cb.mm,'-01')AS CHAR) AS DATE)) AS dt
FROM contract_balance cb ORDER BY dt DESC
) AS A
GROUP BY A.cid