SELECT MAX() and corresponding field in the same row - mysql

Here's my table A
orderID groupID nameID
1 grade A foo
2 grade A bar
3 grade A rain
1 grade B rain
2 grade B foo
3 grade B bar
1 grade C rain
2 grade C bar
3 grade C foo
Desired result:
rain
bar
foo
I need nameID of max(orderID) from each grade. I can get right orderID from each grade, but nameID always stays as the first.
Thanks a Lot!
Praveen gave the right query! Extra question under his answer

edit: I just fixed a mistake in my answer.
You are looking for something quite like:
select
orderID,
groupID,
nameID
from
A
where
concat(orderID,'-',groupId) in (select concat(max(orderID),'-',groupId) from A group by groupID)
edit: in regards to the extra question:
To put the list in order of nameId, just add to the query:
order by
nameID

Here is an appropriate query, using a correlated subquery:
select orderID, groupID, nameID
from A
where orderId = (select max(OrderId) from A a2 where A.groupId = a2.groupId);
If you want to do this with aggregation, you can use the group_concat()/substring_index() trick:
SELECT max(orderID) as orderID, groupId,
substring_index(group_concat(nameId order by orderId desc), ',', 1) as nameID
FROM A
GROUP BY groupID;

SELECT * FROM (select
max(`orderID`) as maxID, `groupID`, `nameID`
from
Table1
GROUP BY `groupID`, `nameID`
ORDER BY maxID desc) abc
GROUP BY GROUPID
Fiddle With my test data
Fiddle with your data

Related

MySQL - SELECT Most occuring value (with several same occurrences)

I have been looking for a way to do a MySQL select to get all the most occuring values. All the solutions i have found where with using LIMIT 1, but this doesn't help if there are more than one value that occur the same amount of times. E.g:
customer
ID
FirstName
1
Bob
2
Tom
3
Bob
4
Robert
5
Tom
6
Timothy
The Select for most occuring FirstNames should result in:
FirstName
Bob
Tom
Since both occur twice.
I have tried the following:
SELECT FirstName FROM (
SELECT FirstName, COUNT(FirstName) as counter FROM customer
GROUP BY FirstName
HAVING counter = MAX(counter)
) s
But this doesn't seem to work, i would really appreciate a nudge in the right direction.
There can be different ways for doing this
you can try
1st
SELECT FIRSTNAME
FROM customer
GROUP BY FirstName Having count(FirstName) = (
SELECT COUNT(FirstName) FROM customer GROUP BY FirstName ORDER BY 1
DESC LIMIT 1);
2nd
with cte as
(
SELECT COUNT(FirstName) MaxCounter
FROM customer
Group By FirstName ORDER BY COUNT(FirstName) DESC LIMIT 1
)
SELECT c.FirstName
From customer c
Group BY FirstName
HAVING COUNT(FirstName) = (SELECT MaxCounter FROM cte)

MySQL Select max() from every Value

I there a way to get the max() Value of every Value?
I have a table like this:
id primary key
name foreign key
age
and I need the highes age of every Name. For example:
ID NAME AGE
1, Marco, 12
2, Jason, 23
3, Tom, 5
4, Marco, 16
5, Jason, 22
The output should be:
ID NAME AGE
2, Jason, 23
3, Tom, 5
4, Marco, 16
Is this possible and how?
Thank you.
You can get the max value of each column using aggregation:
select max(id), name, max(age)
from t
group by name;
But if you want the complete row with the max age, then that would be:
select t.*
from t
where t.age = (select max(t2.age) from t t2 where t2.name = t.name);
Try this:
select id,name,max(age) over(partition by name) as max_age from table group by id,name;
You can use aggregation:
select min(id) id, name, max(age) age from mytable group by name
You can get the max age and name from sub query then left join to obtain its ID.
SELECT b.id, a.name, a.maxage
FROM (SELECT name, MAX(age) AS maxage
FROM table
GROUP BY NAME
) a
LEFT JOIN table b ON a.NAME = b.NAME AND a.maxage= b.AGE

Getting the second max value of col2 while being grouped by col1

I'm facing a corner here...
The background:
TABLE myrecord (
id int # primary key
name varchar(32) # test name
d_when varchar(8) # date in yyyymmdd string format
)
Content:
id name d_when
100 Alan 20110201
101 Dave 20110304
102 Alan 20121123
103 Alan 20131001
104 Dave 20131002
105 Bob 20131004
106 Mike 20131101
In layman terms, I want to figure out who is a "returner" and when was his last (i.e., 'penultimate') visit.
something like the over enthusiastic:
SELECT SECOND_MAX(id), CORRESPONDING(d_when)
FROM myrecord
GROUP BY name
HAVING count(name)>1;
result expected:
101 Dave 20110304
102 Alan 20121123
I tried the following so far.
SELECT T1.id, t1.name, T1.d_when
FROM myrecord t1
WHERE id IN (SELECT MAX(id),
COUNT(id) cn
WHERE cn>1
ORDER BY d_when DESC)
but something is clearly not right here.
Here's one way...
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.name=x.name
AND y.d_when >= x.d_when
GROUP
BY x.name
, x.d_when
HAVING COUNT(*) = 2;
why is the second last id necessary?
if it's not:
SELECT MAX(id), name, MAX(d_when)
FROM myrecord
GROUP BY name
HAVING count(name)>1
if it is:
SELECT name, max(id), max(d_when)
FROM myrecord
WHERE
-- get only the names that have more then one occurrence
name in (
SELECT name
FROM myrecord
GROUP BY name
HAVING COUNT(*) > 1
)
-- filter out the max id's
AND id NOT IN(
SELECT max(id)
FROM myrecord
GROUP BY name
)
GROUP BY name
or even better (thanks to #Andomar for the mention):
SELECT name, max(id), max(d_when)
FROM myrecord
WHERE
-- filter out the max id's, this will also filter out those with one record
AND id NOT IN(
SELECT max(id)
FROM myrecord
GROUP BY name
)
GROUP BY name
In MySQL, for retrieving all those who have made second visit and their second visited date.
Query:
SELECT *
FROM
(
SELECT
#ID:=CASE WHEN #Name <> Name THEN 1 ELSE #ID+1 END AS rn,
#Name:=Name AS Name,
ID, d_when
FROM
(SELECT ID, Name, d_when
FROM myrecord
ORDER BY Name asc, d_when asc
) rec1, (SELECT #ID:= 0) rec1_id, (SELECT #Name:= 0) rec1_nm
) rec
where rec.rn=2
Output:
rn Name ID d_when
2 Dave 104 20131002
2 Alan 102 20121123
Assuming the id column ascends over time, you can select the second-highest d_when per name like:
select name
, d_when
from YourTable yt1
where id in
(
select max(id)
from YourTable yt2
where id not in
(
select max(id)
from YourTable yt3
group by
name
)
group by
name
)
select * from
myrecord
where id in (
SELECT max(id)
FROM myrecord
WHERE id not in (SELECT MAX(id)
FROM myrecord
GROUP BY name
HAVING count(name)>1)
group by name )

MySQL count duplicate row

I have a table like this:
jobid, orderid
And with some data inside:
jobid, orderid
1245, 6767
1235, 9058
6783, 6767
4991, 6767
9512, 9058
5123, 1234
Now I want the following output:
jobid, orderid, orderid(total)
1245, 6767, 3
1235, 9058, 2
6783, 6767, 3
4991, 6767, 3
9512, 9058, 2
5123, 1234, 1
Now, the COUNT() doesn't work the way I want to, and I probably need some group by but I don't know how.
Thanks in advance.
It looks like you're trying to get rows which look like jobid, orderid, number of times that orderid appears. For that, you could use a subquery:
SELECT jobid, orderid,
(SELECT COUNT(*) FROM
MY_TABLE INR
WHERE INR.orderid = OTR.orderid) as "orderid(total)"
FROM MY_TABLE OTR
Why are doing it this way? You will be doing a lot of redundant countings and put a lot of unnecessary pressure on your server. I would do this with two queries:
SELECT jobid, orderid FROM my_table
to get the complete list, and then:
SELECT orderid, COUNT(*) FROM my_table GROUP BY orderid
to get the total count for each orderid. Then combine these two results in your application. This will be much faster than your solution.
SELECT jobid, orderid, count(orderid)
FROM sometable
GROUP BY orderid, jobid
SELECT t.jobid
, t.orderid
, grp.orderid_total
FROM
tableX AS t
JOIN
( SELECT orderid
, COUNT(*) AS orderid_total
FROM tableX
GROUP BY orderid
) AS grp
ON grp.orderid = t.orderid
select jobid, orderid, count(*) from table group by orderid;

MySQL row number

Say I have a MySQL query, for example:
SELECT id, name, surname FROM employees ORDER BY id
The result woud be:
id name surname
1 Peter Smith
2 John Banjo
...
1384 Will Levenstein
While this is an ordered query, I can always assume (as long as I don't change the table) that John Banjo will come out second.
Now what if my query was
SELECT id, name, surname FROM employees WHERE name = 'John' AND surname = 'Banjo'
Could I somehow get what the row number would be in the first query?
I'm trying to do this in a much more complicated, but always ordered query, is there any way to archieve this?
SELECT x.id, x.name, x.surname, x.rownum
FROM (
SELECT #rownum:=#rownum+1 rownum, t.*
FROM (SELECT #rownum:=0) r, employees t
ORDER BY Id
) x
WHERE x.name = 'John'
AND x.surname = 'Banjo'