I have this table:
A,B,C
2,5,6
2,8,5
3,4,7
3,4,8
I want to group the results by A, and select the max C value. The query for it is something like
SELECT A, MAX(C) FROM table GROUP BY A
My Question is, how can I retrieve the B value that is next to the max(c) value? Can this be done in the same quesry, or do I need a join query and look for the B field after running the first query?
Thanks!
try this:
select A,(select B from Table1 t1 where t1.A=t2.A and T1.C=T2.C) B ,MAX(c) C
from Table1 T2 group by A;
FIDDLE DEMO
This is known as the groupwise maximum. To obtain it, you must join the result to your table again:
SELECT * FROM table NATURAL JOIN (
SELECT A, MAX(C) AS C FROM table GROUP BY A
) t
You can add it to your selected columns, but must group by it as well. Otherwise the result is indeterminate.
SELECT A, B, MAX(C)
FROM table
GROUP BY A, B
select *
from table1 t
join
(SELECT A, MAX(C) as C
FROM table1 GROUP BY A )a
on t.A=a.A
and t.C=a.C
Related
I have two similar SELECT queries that retrieve data from the same table "my_table".
-- 1st select
SELECT
my_table.id,
a,
b
FROM my_table
JOIN table2 ON u = v
JOIN table3 ON x = y
UNION ALL
-- 2st select
SELECT
my_table.id,
a,
b
FROM my_table
JOIN table2 ON r = s
JOIN table3 ON t = u
Duplicates are to be filtered out under the following conditions:
If the second select returns an id that is already present in the 1st select, it should be discarded.
Is there an easy solution without using a common table expression?
Note: The SQL does not have to be a UNION and can also be changed.
UNION filters out duplicate rows by default. UNION ALL does not remove duplicates.
But the duplicates are based on all columns being identical, not just the id column. If a given id value occurs in both queries, but any of the other two columns are different, then it counts as a distinct row.
If you want to reduce the result to a single row per id, the use a GROUP BY:
SELECT id, ...aggregate expressions...
FROM (
SELECT my_table.id, a, b ...
UNION
SELECT my_table.id, a, b ...
) AS t
GROUP BY id;
When you GROUP BY id, then any other expressions of the outer select-list must be in aggregate functions like MAX() or SUM(), etc.
The reason it is important to use an aggregate function is that when there are multiple rows with the same id value which you want to reduce to one row, what value should be displayed for a and b?
Example:
id
a
b
4
12
24
4
18
28
If you group by id, you would get one row for id=4, but what value for the other two columns?
id
a
b
4
?
?
Read https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html for more details on this. Or my answer to Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
You must use an aggregate function, which includes GROUP_CONCAT() to append all the values from that column in a comma-separated list. Or you can use ANY_VALUE() which picks one of the values from that column arbitrarily.
I think this should do it:
-- 1st select
SELECT
my_table.id,
a,
b
FROM my_table
JOIN table2 ON u = v
JOIN table3 ON x = y
WHERE id NOT IN (
SELECT
my_table.id,
FROM my_table
JOIN table2 ON r = s
JOIN table3 ON t = u
)
UNION ALL
-- 2st select
SELECT
my_table.id,
a,
b
FROM my_table
JOIN table2 ON r = s
JOIN table3 ON t = u
I have the following table ("Have").
What would be a proper way to SELECT a table where you sum the value of C where A and B are equal. So that you get the result displayed in "Want".
use group by then sum.
select A, B, sum(c) from your_table group by A, B
I am using a simple query:
select A,B from Table1 where id in ('');
which gives me output like:
A B
1 X
2 V
3 R
Now i want to know count of value B in whole database:
i.e
A B CountB
1 X 3
2 V 1
3 R 2
This ought to to it:
SELECT *
FROM (SELECT A,
B
FROM Table1
WHERE id in ('')) t1 LEFT JOIN
(SELECT B,
COUNT(B)
FROM Table1
GROUP BY B) t2 ON t1.B = t2.B
May I didn't understand the question but it seems the following query should do:
SELECT A,
B,
COUNT(*)
FROM Table1
WHERE .....
GROUP BY A,
B
ORDER BY A,
B;
You need to make use of group_by if you want certain column, and then add a count the id of it to get count. So I'd do
SELECT A,B,COUNT(B.ID) as CountB FROM Table1 WHERE id IN ('') GROUP BY B;
I don't know if my title is understandable or not, may be someone can help edit my title?
All I want to do is, for example:
I have a table like this
Engineering appears 5 times with different article_category_abbr, and I want to select only one row with the biggest value of num.
Here, it will be Engineering-ENG-192, and Geriatrics&Gerontology will be Geriatrics&Gerontology-CLM-26
But I don't know how to do it on the whole table using mysql
Join your table to a subquery which finds the greatest num value for each sc group.
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT sc, MAX(num) AS max_num
FROM yourTable
GROUP BY sc
) t2
ON t1.sc = t2.sc AND
t1.num = t2.max_num;
You can have a subquery that gets the largest value for each sc and the resulting rows will then be joined with the table itself based from two columns - sc and num.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT sc, MAX(num) AS Num
FROM tableName
GROUP BY sc
) b ON a.sc = b.sc
AND a.num = b.num
Here's a Demo
USE MAX function and GROUP BY like this. Here is more information.
SELECT myID, classTitle, subField, MAX(score) FROM myTable GROUP BY myID, classTitle, subField
So here's how I select distinct rows by a combination of multiple columns (a, b and c):
select distinct a,b,c from my_table
This is good, but I need yet another column retrieved for these rows (d) which I can't add to the select part, because then it also plays a role in determining row uniqueness which I don't want.
How can I retrieve an additional column without it affecting row uniqueness?
You can do this using a group by. In MySQL, you can do:
select a, b, c, d
from my_table
group by a, b, c
This chooses an arbitrary value for "d", which would typically (but not guanteed!) be the first value encountered. This uses a feature of MySQL called Hidden Columns.
For code that works in MySQL and other databases, you need to be more explicit:
select a, b, c, min(d)
from my_table
group by a, b, c
Getting an actual random value for d in MySQL is a bit trickier and requires more work. Here is one way:
select distinct a, b, c,
(select d from my_table mt2
where mt.a = mt2.a and mt.b = mt2.b and mt.c = mt2.c
order by rand()
limit 1
) d
from my_table mt
Looks like a job for a join... probably a LEFT JOIN. Something like this:
SELECT DISTINCT L.a, L.b, L.c FROM `my_table` L
LEFT JOIN (
SELECT a, b, c, d FROM `my_table`
) R ON L.a=R.a AND L.b=R.b AND L.c=R.c