This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
MySQL - How to select rows with max value of a field
(3 answers)
Closed 1 year ago.
I'm looking for a way to display a column in my table without aggregating it (column store)
SELECT id, name, max(cost), store
FROM test
group by 1,2,4
But in the table im using there are some row that shares the same id,name,license with different machine, and Ideally I'd like to get in return only a single row, with max(cost), if they share the same first 2 columns and only display the last column - without using it to group by.
actual outcome:
id
name
max(cost)
store
1
Joe
30
store1
1
Joe
50
store2
but my desired result will be:
id
name
max(cost)
store
1
Joe
50
store2
Can it even be done?
You seem to want the row with the maximum cost. A typical method uses row_number();
select t.*
from (select t.*,
row_number() over (partition by id, name order by cost desc) as seqnum
from test t
) t
where seqnum = 1;
No aggregation is needed.
You can also use a correlated subquery:
select t.*
from test t
where t.cost = (select max(t2.cost)
from test t2
where t2.id = t.id
);
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 months ago.
I have a table named Work_Items like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM Work_Items WHERE Name IN ('A','B','C','D') ORDER BY Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.
On MySQL 8+, we can use ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Date DESC) rn
FROM Work_Items
WHERE Name IN ('A', 'B', 'C', 'D')
)
SELECT Name, Date, ProducedItems
FROM cte
WHERE rn = 1
ORDER BY Name;
You can use inner join as follows, its working on any mysql version:
select w.name, w.`date`, w.ProducedItems
from _Work_Items w
inner join (
select name, max(date) as `date`
from _Work_Items
group by name
) as s on s.name = w.name and s.`date` = w.`date` ;
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 5 months ago.
SQL query for extracting last entered number field(if we enter 11 in 3 rows take last row) from a table.Consider the following table
I want to show result like this
On MySQL 8+, we can use ROW_NUMBER here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY NO ORDER BY created_date DESC) rn
FROM yourTable
)
SELECT NO, Letter
FROM cte
WHERE rn = 1;
The above would return the latest row for every NO group. If instead you want the earliest row, then change the sort order used by ROW_NUMBER.
Another option for you
SELECT a.NO,a.Letter FROM yourtable a
JOIN
(SELECT max(date) AS date,NO FROM yourtable GROUP BY NO) b ON a.NO=b.NO AND a.DATE=b.DATE
ORDER BY a.NO
This question already has answers here:
How to find the maximum count using mysql?
(9 answers)
Closed 1 year ago.
I've been trying to get the maximum number from a set of different values.
suppose,
i have table with values,
id val
A 1
A 2
A 3
B 4
B 5
C 6
so i want to get the maximum number of id and first i did get the count and tried to get maximum
my sql query is,
SELECT MAX (ID_COUNT) id_count
FROM (SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id)
I want an ouput 3 since A is repeated 3 times and is the maximum.
it was supposed to work. but now im getting an error saying derived table value should have an alias. but i did give an alias.
what should i do?
thanks in advance...
You need to specify an alias for the sub select
SELECT MAX (ID_COUNT) id_count
FROM (SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id) tab2
You can try the below -
SELECT id,COUNT(id) ID_COUNT
FROM tab1
GROUP BY id
order by ID_COUNT desc
limit 1
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
I have the following query.
SELECT MAX(activity_id), group_id, parent_group_id FROM dashboard_activity_detail WHERE account_id = 8997 AND parent_group_id IN (5118,5026,4522,3983,3586,3278,3227) AND activity_type_id = 18 GROUP BY parent_group_id;
My expectation is that the group_id will be returned associated with the largest activity_id. There are multiple rows with the same parent_group_id in the table. What I get back is a value from a different row. The activity_id is correct and the parent_group_id is correct but I get a value from a different row for the group_id.
What am I missing? I've tried order by and various other methods with the same result.
You expectation is wrong. You query is malformed because the SELECT columns are inconsistent with the GROUP BY columns.
Use window functions (available in MySQL 8+):
SELECT da.*
FROM (SELECT da.*,
ROW_NUMBER() OVER (PARTITION BY parent_group_id ORDER BY activity_id DESC) as seqnum
FROM dashboard_activity_detail da
WHERE account_id = 8997 AND
parent_group_id IN (5118,5026,4522,3983,3586,3278,3227) AND
activity_type_id = 18
) da
WHERE seqnum = 1;
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 4 years ago.
My table has the following structure:
sensor_id | timestamp | value
-----------|------------|-------
1 | 1516905900 | 100.1
4 | 1516916920 | 90.4
... | ... | ...
I need to select the columns with the max value for each day for a given sensor_id. I need the timestamp associated with each max value in the results.
I have had some success using MAX() and GROUP BY and DATE_FORMAT, but I cannot get my query to include the timestamp associated with the max value for each group.
This is what I have so far:
select DATE_FORMAT(from_unixtime(timestamp), '%m/%d/%Y') as x, max(value) from sensor_log where sensor_id=6 group by x;
The result only includes the x and max(value) columns. I think it might be possible to use some sort of JOIN to get the rest of the columns, but I haven't been able to figure it out.
Another issue I can foresee is the possibility of a sensor_id having multiple occurrences of the max value on the same day, in this case I would only like to keep the first occurrence of that value for that day.
Hmmm. This sounds like a good use of a correlated subquery with a twist:
select date(from_unixtime(sl.timestamp)) as dte, sl.sensor_id, sl.value, sl.timestamp
from sensor_log sl
where sl.timestamp = (select sl2.timestamp
from sensor_log sl2
where sl2.sensor_id = sl.sensor_id and
date(from_unixtime(sl2.timestamp)) = date(from_unixtime(sl.timestamp))
order by sl2.value desc, sl.timestamp asc
limit 1
);
Starting with MySQL version 8, this can be accomplished with a ranking function dense_rank.
select sensor_id,timestamp,value,DATE_FORMAT(from_unixtime(timestamp),'%m/%d/%Y') as dt
from (select t.*
,dense_rank() over(partition by DATE_FORMAT(from_unixtime(timestamp),'%m/%d/%Y') order by value desc) as rnk
from tbl t
) t
where rnk=1