How we get multiple column count(TOP) using MySQL single query? - mysql

I have a table structure like:
id | ex_name | att_name
10 | David | sam G&G
12 | John | mark hol
13 | John | john b
14 | Mark | john c
15 | David | mark hol
16 | David | mark hol
17 | Mark | sam G&G
18 | John | john b
19 | David | sam G&G
20 | John | sam G&G
When I'm using below query:
SELECT att_name
, count(att_name) as att_count
FROM `tablename`
group
by att_name
order
by att_count desc
Returns:
sam G&G = 4
mark hol = 3
john b = 2
john c = 1
I want top values of output i.e
sam G&G which is 4
Same with column ex_name it returns:
David = 4
john = 4
mark = 2
I want top values of the ex_name column which is David and John having count 4
What I want the final output like :
ex_name | att_name | ex_count | att_count
David Sam G&G 4 4
John 4
I'm also tried below query to fetch the output but in this case, I get ex_name and att_name is NULL.
SELECT a.att_name,b.att_name,max(a.ex_count),max(b.att_count)
FROM application_data
INNER JOIN (
SELECT ex_name,count(ex_name) as ex_count
FROM application_data
GROUP BY ex_name
) a
INNER JOIN (
SELECT att_name ,count(att_name) as att_count
FROM application_data
GROUP BY att_count
) b
It returns:
ex_name | att_name | ex_count | att_count
NULL NULL 4 4
Can you help me out?Thanks in advance

It appears that you want to aggregate your table by some column, and then retain all group records which share the highest count. One way to do this is to add a HAVING clause to your current query which asserts that the count for a group to be retained is the highest count from all groups.
SELECT att_name, COUNT(*) AS cnt
FROM tablename
GROUP BY att_name
HAVING COUNT(*) = (SELECT COUNT(*) FROM tablename
GROUP BY att_name ORDER BY COUNT(*) DESC LIMIT 1);
Demo

Related

Sum of Counted records that calculated using "group by" with condition and "group by"

I'm sorry for fuzzy title of this question.
I have 2 Tables in my database and want to count records of first_table using "group by" on a foreign key id that exists in a column of second_table (which stores ids like array "1,2,3,4,5").
id | name | fk_id
1 | john | 1
2 | mike | 1
3 | jane | 2
4 | tailor | 1
5 | jane | 3
6 | tailor | 5
7 | jane | 4
8 | tailor | 5
9 | jane | 5
10 | tailor | 5
id | name | fk_ids | s_fk_id
1 | xxx | 1,5,6 | 1
2 | yyy | 2,3 | 1
3 | zzz | 9 | 1
4 | www | 7,8 | 1
Now i wrote the following query but it not working properly and displays wrong numbers.
I WANT TO:
1-Count records in first_table group by "fk_id"
2-Sum the counted records which exists in "fk_ids"
3-Display the sum result (sum of related counts) grouped by id.
symbol ' ' means ``.
select sum(if(FIND_IN_SET('fk_id', 'fk_ids')>0,'count',0) 'sum', 'count', 'from'.'fk_id', 'second_table'.* FROM 'second_table'
LEFT JOIN
(
SELECT 'fk_id', count(*) 'count'
FROM 'first_table'
group BY 'fk_id'
) AS 'from'
ON FIND_IN_SET('fk_id', 'fk_ids')>0
WHERE 'second_table'.'s_fk_id'=1
GROUP BY 'id'
ORDER by 'count' DESC
This table has many data and we have no plan to change the structure.
Edit:
Desired output:
id | name | sum
1 | xxx | 7 (3+4+0)
2 | yyy | 2 (1+1)
3 | zzz | 0 (0)
4 | www | 0 (0+0)
After two holidays i came back to work and found out that the "FIND_IN_SET" function is not working properly with space contained string.
And the problem is that i was ignored the spaces too, (same as this question)
Finnaly this query worked:
select sum(`count`) `sum`, `count`, `from`.`fk_id`, `second_table`.* FROM `second_table`
LEFT JOIN
(
SELECT `fk_id`, count(*) `count`
FROM `first_table`
group BY `fk_id`
) AS `from`
ON FIND_IN_SET(`fk_id`, replace(`fk_ids`,' ',''))>0
WHERE `second_table`.`s_fk_id`=1
GROUP BY `id`
ORDER by `count` DESC
And the magic is replace(fk_ids,' ','')

An interesting SQL query CHALLENGE - list the AVERAGE value of the top 3 scores of each athlete

An interesting SQL query CHALLENGE:
A table named athelets consisting of id, ath_id, name, score, date.
+----+--------+-----------------+--------+------------+
| id | ath_id | name | record | r_date |
+----+--------+-----------------+--------+------------+
| 1 | 2 | John Wayne | 79 | 2010-07-08 |
| 2 | 7 | Ronald Regan | 51 | 2000-03-22 |
| 3 | 1 | Ford Harrison | 85 | 2009-11-13 |
| 4 | 2 | John Wayne | 69 | 2017-01-01 |
Please write a sql query to list the average value of the top three scores of each athlete, something like:
ath_id: 1, the arithmetic mean of his/her top 3 records: 77
ath_id: 2, the arithmetic mean of his/her top 3 records: 73
ath_id: 3, the arithmetic mean of his/her top 3 records: 47
select ath_id, avg(record)
from
(select ath_id, record
from atheletes as t1
where
(select count(*) from atheletes where t1.ath_id=ath_id and record > t1.record) < 3) as d
group by ath_id;
The above query should works as expected.
Assuming combinations of athletes and records are unique...
SELECT ath_id
, ROUND(AVG(record),2) top3_avg
FROM
( SELECT x.*
FROM athletes x
JOIN athletes y
ON y.ath_id = x.ath_id
AND y.record >= x.record
GROUP
BY x.id
HAVING COUNT(*) <=3
) a
GROUP
BY ath_id;

MySQL - How to select rows with max value of a field

I have a table of users with their scores for each level of a game:
id | user_id | level | score
1 | David | 1 | 20
2 | John | 1 | 40
3 | John | 2 | 30
4 | Mark | 1 | 60
5 | David | 2 | 10
6 | David | 3 | 80
7 | Mark | 2 | 20
8 | John | 3 | 70
9 | David | 4 | 50
10 | John | 4 | 30
What is the SQL query needed to get for each level, who has the highest score?
The result should be:
id | user_id | level | score
4 | Mark | 1 | 60
3 | John | 2 | 30
6 | David | 3 | 80
9 | David | 4 | 50
Thank you
If you want to get ties, then you can do something like this:
select s.*
from scores s
where s.score = (select max(s2.score) from scores s2 where s2.level = s.level);
You could get one row per level by aggregating this:
select s.level, s.score, group_concat(s.user_id)
from scores s
where s.score = (select max(s2.score) from scores s2 where s2.level = s.level)
group by s.level, s.score;
This combines the users (if there is more than one) into a single field.
order by score desc in sub query, then select max(score) group by level.
select id, user_id , level , max(score) as score
from
(select * from scores order by score desc)A
group by level
If you only want the user, who reached the highest score first (no ties per level):
select *
from users u1
where id = (
select id
from users u2
where u2.level = u1.level
order by score desc, id asc
limit 1
)
You should have indexes (id) and (level, score, id)

get sum for multiple blocks

I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo

Join two tables and show Columns of one table as Rows

There are two tables which I have to join and produce the expected result
Employee Table
EmpID | EmpName
1 | Adam
2 | Eve
3 | John
4 | Steve
EmployeeNationality Table
EmpID | Nationality
1 | US
1 | UK
1 | UKraine
2 | US
3 | Canada
4 | Spain
Result Expected
EmpID | EmpName | Nationality1 | Nationality2
1 | Adam | US | UK
2 | Eve | US |
3 | John | Canada |
4 | Steve | Spain |
Though there are three records for Employee ID 1 (Adam) I always have to show only two Nationality so the no of columns are fixed.
Thanks in advance
If you just need two nationalities:
WITH CTE AS(
SELECT e.EmpID,
e.EmpName,
en.Nationality,
RN = ROW_NUMBER() OVER (PARTITION BY e.EmpID,e.EmpName
ORDER BY e.EmpID)
FROM dbo.employee e
INNER JOIN employeenationality en
ON e.empid = en.empid
)
SELECT DISTINCT c1.EmpID, c1.EmpName,
Nationality1 = (SELECT Nationality FROM CTE Nationality1
WHERE c1.EmpID = Nationality1.EmpID
AND Nationality1.RN = 1),
Nationality2 = (SELECT Nationality FROM CTE Nationality2
WHERE c1.EmpID = Nationality2.EmpID
AND Nationality2.RN = 2)
FROM CTE c1
Sql-Fiddle
Otherwise use PIVOT.