Diff value last two record by datetime - mysql

I have table with id, item_id, value (int), run (datetime) and i need select value diff betwen last two run per *item_id*.

SELECT item_id, ABS(value1 - value2) AS diff
FROM ( SELECT h.item_id, h.value AS value1, h2.value AS value2
FROM ( SELECT id, item_id, value
FROM table_name
GROUP BY item_id
ORDER BY run DESC) AS h
INNER JOIN ( SELECT id, item_id, value
FROM table_name
ORDER BY run DESC) AS h2
ON h.item_id = h2.item_id AND h.id != h2.id
GROUP BY item_id) AS h3
I believe this should do the trick for you. Just replace table_name to correct name.
Explanation:
Basicly I join the table with itself in a run DESC order, JOIN them based on item_id but also on id. Then I GROUP BY them again to remove potential 3rd and so on cases. Lastly I calculate the difference between them through ABS(value1 - value2).

SELECT t2.id, t2.item_id, (t2.value- t1.value) valueDiff, t2.run
FROM ( table_name AS t1
INNER JOIN
table_name AS t2
ON t1.run = (SELECT MAX(run) FROM table_name where run < t2.run)
and t1.item_id = t2.item_id)
This is assuming you want the diff between a record and the record with the previous run

Related

Mysql - Return only first row having the same unique ids

Consider the following table:
As shown in image, I want to return all the data from only first distinct id. How can I achieve that in MySQL ?
You can filter with a subquery. Assuming that by first you mean the row with the earlier start_time, that would be:
select t.*
from mytable t
where t.start_time = (
select min(t1.start_time) from mytable t1 where t1.call_unique_id = t.call_unique_id
)
from your_table t1
join
(
select min(call_unique_id) as id
from your_table
group by start_time
) t2 on t1.id = t2.id
group by should also do the job. so try
select * from your_table group by call_unique_id

Get latest records with id for each group with latest date-time not id - mysql

My requirement is to get id based on latest frame (based on date-time) is inserted with group by vehicle id.
I have tried with below query
select a.id,a.vehicle_id,a.info_datetime
from table_name a
join (select id, max(info_datetime) as maxdt from table_name group by vehicle_id) as b
on a.info_datetime = b.maxdt
The output will not give the proper id.
So, I want the id for latest frame inserted with group by vehicle_id.
Max(id) or Max(info_datetime) query will not give the accurate result.
SELECT id, vehicle_id FROM table_name as a WHERE info_datetime =
(SELECT MAX(info_datetime) FROM table_name as b WHERE a.vehicle_id = b.vehicle_id)
GROUP BY vehicle_id
Your attempt is almost there, but you need to join by id as well:
select a.id,a.vehicle_id,a.info_datetime
from table_name a
join (
select vehicle_id, max(info_datetime) as maxdt from table_name group by vehicle_id
) as b on a.info_datetime = b.maxdt and a.vehicle_id = b.vehicle_id
Another way to do it is to filter with a correlated subquery:
select a.*
from table_name a
where a.info_datetime = (
select max(b.info_datetime) from table_name b where b.vehicle_id = a.vehicle_id
)
Finally got the solution for above.
SELECT id, vehicle_id, info_datetime
FROM table_name
WHERE id in (
select max(m2.id) from table_name m2 group by m2.vehicle_id)
ORDER BY vehicle_id asc,info_datetime DESC
It will take approx 2 to 3 seconds to execute in around 25 lac records.

using the same field within a subquery of mysql

how can i make something like this work?
INSERT INTO age.page(domain,title_count,youtube_count,ipaddress,updated)
SELECT * FROM
(
SELECT domain,
COUNT(domain) AS titlecount,
(SELECT COUNT(*) FROM table2 WHERE title = table1.title) AS YoutubeCount, ipaddress
NOW() AS timeNow
FROM table1
GROUP BY domain
ORDER BY title DESC
) a;
I want to use a subquery to get a count of a different table but use the same field from the main query.
the reason i want to do this is so i dont have to run two queries instead its only one.
You can do this COUNT in a subquery and then JOIN it with the first table:
INSERT INTO age.page(domain, title_count, youtube_count, ipaddress, updated)
SELECT * FROM
(
SELECT
domain,
COUNT(domain) AS titlecount,
t2.titlecount AS YoutubeCount,
ipaddress,
NOW() AS timeNow
FROM table1
INNER JOIN
(
SELECT title, COUNT(*) Titlecount
FROM table2
GROUP BY title
) AS t2 ON t2.title = table1.title
GROUP BY domain
ORDER BY table1.title DESC
) a;

MySQL - Fetching lowest value

My database structure contains columns: id, name, value, dealer. I want to retrieve row with lowest value for each dealer. I've been trying to mess up with MIN() and GROUP BY, still - no solution.
Solution1:
SELECT t1.* FROM your_table t1
JOIN (
SELECT MIN(value) AS min_value, dealer
FROM your_table
GROUP BY dealer
) AS t2 ON t1.dealer = t2.dealer AND t1.value = t2.min_value
Solution2 (recommended, much faster than solution1):
SELECT t1.* FROM your_table t1
LEFT JOIN your_table t2
ON t1.dealer = t2.dealer AND t1.value > t2.value
WHERE t2.value IS NULL
This problem is very famous, so there is a special page for this in Mysql's manual.
Check this: Rows Holding the Group-wise Maximum/Minimum of a Certain Column
select id,name,MIN(value) as pkvalue,dealer from TABLENAME
group by id,name,dealer;
here you group all rows by id,name,dealer and then you will get min value as pkvalue.
SELECT MIN(value),dealer FROM table_name GROUP BY dealer;
First you need to resolve the lowest value for each dealer, and then retrieve rows having that value for a particular dealer. I would do this that way:
SELECT a.*
FROM your_table AS a
JOIN (SELECT dealer,
Min(value) AS m
FROM your_table
GROUP BY dealer) AS b
ON ( a.dealer= b.dealer
AND a.value = b.m )
Try following:
SELECT dealer, MIN(value) as "Lowest value"
FROM value
GROUP BY dealer;
select id, name, value, dealer from yourtable where dealer
in(select min(dealer) from yourtable group by name, value)
These answers seem to miss the edge case of having multiple minimum values for a dealer and only wanting to return one row.
If you want to only want one value for each dealer you can use row_number partition - group - the table by dealer then order the data by value and id. we have to make the assumption that you will want the row with the smallest id.
SELECT ord_tbl.id,
ord_tbl.name,
ord_tbl.value,
ord_tbl.dealer
FROM (SELECT your_table.*,
ROW_NUMBER() over (PARTITION BY dealer ORDER BY value ASC, ID ASC)
FROM your_table
) AS ord_tbl
WHERE ord_tbl.ROW_NUMBER = 1;
Be careful though that value, id and dealer are indexed. If not this will do a full table scan and can get pretty slow...

How to use distinct and limit together

I have a mysql query. I need to get last value from columns Lat,Lng from my table but serial_number column needs to be distinct.
How to make such a query?
This is needed as I am using this coordinates to load it to Google map. So when the Google maps loads I need to have a marker on each last coordinates where vehicle is.
SELECT m.*
FROM (
SELECT DISTINCT serial_number
FROM mytable
) md
JOIN mytable m
ON m.id =
(
SELECT id
FROM mytable mi
WHERE mi.serial_number = md.serial_number
ORDER BY
mi.time DESC, mi.id DESC
LIMIT 1
)
Create an index on (serial_number, time, id) for this to work fast.
If you want to retrieve the last record for a certain serial_number, just use this:
SELECT *
FROM mytable
WHERE serial_number = :my_serial_number
ORDER BY
time DESC, id DESC
LIMIT 1
1#
Assuming that max ID will always give you last lat and lon, the query becomes quite simple -
SELECT t2.*
FROM table t2
where t2.id IN
(
SELECT max(t1.id)
FROM table t1
GROUP BY t1.serial_number
)
2#
If you need to consider time also, then you will need to do it this way. Here, in the inner query, max_time of each serial_number is obtained. Then this max_time and serial_number is joined with the outer table time and serial_number respectively, to get distinct records with last lat and lon.
SELECT *
FROM table t2,
(
SELECT max(t1.time) max_time, t1.serial_number
FROM table t1
GROUP BY t1.serial_number
) new_table
WHERE t2.time=new_table.max_time
AND t2.serial_number=new_table.serial_number
Try this
select distinct serial_number, *
from table t
inner join table t1 on t1.serial_number = t.serial_number and t1.id = (select max id from table t2 where t2.serial_number = t1.serial_number)