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
Related
This question already has answers here:
SQL Order By Count
(8 answers)
Closed 1 year ago.
My table is:
device_name | device_category
---------------------------------
standard_spot | light
mobile_spot | light
tracker | light
smoking_machine| light
microphone1 | sound
microphone2 | sound
display1_hmdi | projector
display1_vga | projector
display1_sound | projector
I need a query that provides the device_names together with the device_categories ordered by the occurrence of a device_category.
My attempt was this query, which provides the occurrences of each category:
SELECT COUNT(*) AS num_dcats FROM `devices` GROUP BY device_category ORDER BY num_dcats DESC
Like this:
device_category | num_dcats
---------------------------
light | 4
projector | 3
sound | 2
You can do:
select d.*
from devices d
join (
select device_category, count(*) as cnt from devices group by device_category
) c on c.device_category = d.device_category
order by c.cnt desc
Use window functions:
select d.*, count(*) over (partition by device_category) as num_cats
from devices d
order by num_cats;
This question already has answers here:
Count number of unique values
(4 answers)
Closed 2 years ago.
I have following table 'tbl_pads' with one column named 'id_pad' with 6 rows:
pad_1 |
pad_1 |
pad_2 |
pad_3 |
pad_3 |
pad_3 |
With SELECT id_pad FROM tbl_pads GROUP BY id_pad
I get following rows:
pad_1 |
pad_2 |
pad_3 |
With SELECT COUNT(id_pad) AS COUNT_PADS FROM tbl_pads GROUP BY id_pad I get following rows
2 |
1 |
3 |
How can get the rows Count of the Grouped Statement? I expect the result 3
Another was of phrasing the question is that you want to get the number of different id_pad values. Once you phrase it like that, it becomes clearer that using a distinct count will work:
SELECT COUNT(DISTINCT id_pad) FROM tbl_pads
If you want to rows count of group statement in single row then you can use below query.
SELECT COUNT(id_pad) over() AS COUNT_PADS FROM tbl_pads group by id_pad limit 1;
OR
SELECT COUNT(DISTINCT id_pad) FROM tbl_pads;
And if you want multiple row based on group statement and rows count along with this then you can go with this.
SELECT COUNT(id_pad) over(), id_pad AS COUNT_PADS FROM tbl_pads group by id_pad;
See the db<>fiddle: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=97021f2b9a1cb360c1258a2bfb4e072a
If you want the number of individual id_pads.
CREATE TABLE tbl_pads
(`id_pad` varchar(5))
;
INSERT INTO tbl_pads
(`id_pad`)
VALUES
('pad_1'),
('pad_1'),
('pad_2'),
('pad_3'),
('pad_3'),
('pad_3')
;
SELECT COUNT(id_pad) AS COUNT_PADS FROM tbl_pads WHERE id_pad = 'pad_3'
| COUNT_PADS |
| ---------: |
| 3 |
db<>fiddle here
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);
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.