how can i show only max rows grouped by id in mysql? - mysql

I want to show only the biggest value from my table GROUPED BY NAME
I have the table:
ID name money
1 jim 100
2 aura 150
3 mike 200
4 jim 300
5 aura 450
6 mike 1000
mysql query:
SELECT *
FROM table
GROUP BY name
and result are only first 3 id and i want to show only the biggest value in money: id 4, 5 and 6

You have to use aggregate function MAX:
SELECT name, MAX(money) FROM table GROUP BY name

Use belowquery,might be you get your solution.
SELECT name,max([money]) as MaxSalary
FROM table
GROUP BY name

Related

how can I write a query in mySQL which shows the average trips in a day?

I have a list of IDs and the detail of the trips they've taken. I want to see how many trips each ID takes in a day on average but I don't know how to write this query. The data I have in my table is something like this:
ID
Ride_id
Date
1
123
2022-3-4
1
124
2022-3-4
1
111
2021-2-8
2
584
2019-4-18
2
256
2019-4-18
2
805
2020-5-8
2
127
2020-5-8
2
457
2020-5-8
3
100
2021-4-7
3
101
2021-4-7
3
202
2021-5-17
3
741
2021-5-17
So basically, the average rides ID=1 takes is 1.5 and the average rides ID=2 takes is 2.5 and so on. I need a query to calculate and show the result like this:
ID
Average_of_daily_trips
1
1.5
2
2.5
3
2
My current query uses only one condition: WHERE ID in ()
First count the trips on each day for each id, then make the average over those counts.
select id, avg(trips)
from (select id, count(*) as trips
from trips
-- where id in(1,2,3)
group by id, date) t
group by id
If you need to, you can uncomment the where clause in the subquery to filter for particular ids ...

Query to get distinct count of column with MYSQL

I have a table name POEMS with columns poemID and userID. I want to make the poemID distinct and show only the count in the userID column. There are 3 poemID's with 1. I want to make it show poemID 1 and userID would be 2. poemID 2 and userID 3, for the 3 rows and poemID 3 with userID 1 for the 1 row.
|poemID | userID|
1 1
1 5
2 2
2 5
2 4
3 2
I want the above table to look like the table below.
|poemID | userID |
1 2
2 3
3 1
My SQL query im trying is below but its not working. Please help.
SELECT DISTINCT(poemID), COUNT(userID) FROM POEMS GROUP BY poemID;
This looks like a straight aggregation query:
SELECT poemID, COUNT(*) no_users
FROM POEMS
GROUP BY poemID;
Or, if the same user may appear multiple times for a given poem and you want to count it only once:
SELECT poemID, COUNT(DISTINCT userID) no_distinct_users
FROM POEMS
GROUP BY poemID;

How to calculate count of records by 2 columns

In MySQL I have a table.
Example:
id name type
1 Thomas 2
2 Thomas 2
3 Thomas 1
4 Paul 3
5 Paul 4
6 Paul 4
I need calculate same records by 2 columns.
Result for this example should be:
name type countOfRecords
Thomas 2 2
Thomas 1 1
Paul 3 1
Paul 4 2
Could you help me with this request?
Since you want records in your result set for each name and type <name,type> pair
you need to group by name and type.
SELECT
name,
type,
COUNT(*) countOfRecords
FROM your_table
GROUP BY name,type;
Note:
Group BY <some column> would generate a result set where number of rows = number of distinct / different / unique <some column>.
Same holds for multiple columns in GROUP BY clause.
This may be right solution:
SELECT name, type, COUNT(*) as countOfRecords
FROM your_table
GROUP BY name,type;

How to filter rows from an ordered table?

Here's the table. It's ordered by points (desc) and id
id name points
1 ed 10
1 ed 9
2 jim 14
2 jim 8
2 jim 4
3 mike 11
Here's the results i'm looking for:
id name points
1 ed 10
2 jim 14
3 mike 11
How can this be done? basically, i want to list only the highest point row for each name and filter other rows away.
You can try something like this: use the MAX() function
SELECT id, name, MAX(points)
FROM your_table
GROUP BY id, name
ORDER BY points desc
Try this:
select id,name,max(points) from table1 group by id

Effective way to count all categories total number of one table in one query in MySQL?

E.g: data table with these records:
data_id data category
1 hello 1
2 world 1
3 john 1
4 kevien 2
5 apple 2
6 bannana 3
7 desk 4
And how can I count the categories total number in one query in MySQL!?
[UPDATE]
How about count total number in special categories.Such as only category 1 and 3??
Thank you very much!!
In response to the question in the comment, include the where clause to restrict it:
SELECT category, count(*) from table
where category in (1, 3)
group by category
select category, count(*)
from theTable
group by category