This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have a table that has multiple values for an identity, and I want to find just one row for each identity where the date field is the newest and then if there is still duplicates just select the one with the lowest id.
My table looks kine of like this:
UniqueID | CustomerId | Name | Address | InspDate
1 1 Bob 123 2013/08/05 00:00:00
2 1 Bob 123 2013/08/05 00:00:00
3 1 Bob 123 2013/03/01 00:00:00
So I only want the row with uniqueid of 1 to show up to show up in this example.
Also I want to limit it to only Customers with inspdates done within the last year if that is possible.
SELECT t.*
FROM
yourtable t INNER JOIN (
SELECT MIN(UniqueID) as Min_ID
FROM
yourtable t1 INNER JOIN (SELECT CustomerID, MAX(InspDate) Max_date
FROM yourtable
GROUP BY CustomerID) m
ON t1.CustomerID=m.CustomerID AND t1.InspDate=m.Max_date
GROUP BY t1.CustomerID) mi
ON t.UniqueID = mi.Min_ID
Please see fiddle here.
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
I am using Mysql 8.0 and trying to execute the following query, but I am not achieving the desired result.I need to get the max updated_at grouped by companies.
The table:
id | updated_at | company
-----------------------------------
5 | 2011-04-14 01:06:06 | 1
3 | 2011-04-14 01:05:41 | 2
7 | 2011-04-15 01:14:14 | 2
The query:
select id, MAX(updated_at), company
from signatures
group by company
I am having an error because id could not be in a group.
Could someone help me with a query that can do the job, please?
Thanks in advance
Use window functions:
select distinct
first_value(id) over (partition by company order by updated_at desc) id,
max(updated_at) over (partition by company) updated_at,
company
from signatures
This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Closed 3 years ago.
Disclaimer: I have searched for similar questions but I haven't found a clear answer to my issue.
I have a table like this:
id | catid | views | text
1 100 2000 "sometext"
2 200 2000 "sometext"
3 200 3000 "longertext"
For each catid (that in this case are just 2: 100 and 200) i need to get the record with the most views and the longer texts... In this case the result would be:
id | catid | views | text
1 100 2000 "sometext"
3 200 3000 "longertext"
With priority on the number of views.
I have tried some queries with inner joins but none seems clear and working...
Any thoughts ?
A method uses a correlated subquery with order by and limit for filtering. Assuming that id is primary or unique key:
select t.*
from mytable t
where t.id = (
select id
from mytable t1
where t1.catid = t.catid
order by t1.views desc, char_length(t1.text) desc
limit 1
)
This question already has answers here:
MySQL - Join & Count rows from another table
(3 answers)
Closed 3 years ago.
In MySQL, I have a table news_archive and topics. For topics, I have a rowid column that acts as the PK, and a FK that references the column filed in news_archive.
So for news_archive:
row_id | filed
1 | 3
2 | 3
3 | 1
4 | 2
and for topics:
rowid | title
1 | foo
2 | bar
3 | foobar
I would like to have a select MySQL query where I can get the title value and the number of the rowid of the rowid. So I would like something along the lines of:
SELECT title, (number of times rowid was referenced) FROM topics WHERE rowid = 3;
Thanks.
select title, (select count(*) tot from news_archive na WHERE na.filled = tc.rowid) total from topics tc WHERE rowid = 3;
Inner Join Solution:
SELECT title, count(na.filed) as total from topics tc INNER JOIN news_archive na
on na.filed = tc.rowid
group by filed, title
https://www.db-fiddle.com/f/nzvUH87rLEdreem2YAxowj/1
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
This is my current data in database.
========================================
id | IC |date |time |
1 | test |2017-07-27 |14:19:26 |
2 | test |2017-07-27 |14:20:26 |
3 | second |2017-07-28 |06:58:55 |
========================================
I want to get the maxdate and maxtime for each IC.
I tried:
SELECT id,pass_no,time_in,ic,date_in FROM `check_in`
WHERE date_in = (SELECT MAX(date_in) FROM check_in)
AND
time_in = (SELECT MAX(time_in) FROM check_in) GROUP BY IC
But it only return the last row data for me. The result I wanted is like
========================================
id | IC |date |time |
2 | test |2017-07-27 |14:20:26 |
3 | second |2017-07-28 |06:58:55 |
========================================
This will return the max date & time per ic:
select ic, max(date), max(time)
from check_in
group by ic
You can use a tuple and group by ic
SELECT id,pass_no,time_in,ic,date_in
FROM `check_in`
WHERE (date_in, time_in, ic) in (
SELECT MAX(date_in), max(time_id), ic
FROM check_in
GROUP BY id)
This should work
select max(time),date,id
from (select max(date) as date,time,id from check-in group by time,id )
group by date,id;
Did you try with "OR" condition insteads "AND"?
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 6 years ago.
I have a table that has a non-unique id, id, a status, status (which I'm trying to get), and a timestamp of when it was inserted, inserted. I need to select the most recent occurrence of each id ordered by the id. I have the following table ordered by inserted:
id | status | inserted
------------------------------------
4 | approved | 2016-08-09 15:51:52
5 | denied | 2016-08-09 15:52:36
5 | pending | 2016-08-09 15:55:05
The results I need are:
id | status | inserted
------------------------------------
4 | approved | 2016-08-09 15:51:52
5 | pending | 2016-08-09 15:55:05
I have the following SELECT:
SELECT * FROM table
GROUP BY id
ORDER BY inserted
and I'm getting these results:
id | status | inserted
------------------------------------
4 | approved | 2016-08-09 15:51:52
5 | denied | 2016-08-09 15:52:36
The solution is probably an easy one, but I've racked my brain on this long enough trying things such as inner selects and whatnot. Any help would be appreciated.
EDIT:
I had to use the third option from the linked duplicate question to get the results I expected (the one with the LEFT JOIN). I assume it was because I was using a DATETIME type, but I'm unsure.
select t.*
from
<T> t inner join
(select id, max(inserted) as max_inserted from <T> group by id) as m
on m.id = t.id and m.max_inserted = t.inserted
or
select
id,
(
select status from <T> t2
where t2.id = t.id and t2.inserted = max_inserted
) as status,
max(inserted) as max_inserted
from <T>
group by id
You can try searching for "mysql latest per group" or something like that for alternatives more specific to MySQL.
If you want the most recent record for each id, then don't use group by. Instead:
select t.*
from table t
where t.inserted = (select max(t2.inserted) from table t2 where t2.id = t.id);