Sorting mysql by number of occurrences - mysql

How do you order a mysql table by the number of occurrences in a spesific column?
Example table:
ID Name
1 Alfred
2 Alfred
3 Burt
4 Alfred
5 Jill
6 Jill
7 Jill
8 Jill
9 Burt
The sorted table should be like below, since "Jill" is the name occurring most, it should be sorted first, and so on:
ID Name
5 Jill
6 Jill
7 Jill
8 Jill
1 Alfred
2 Alfred
4 Alfred
3 Burt
9 Burt

You have to bring in the information into the query. This is typically done using a join:
select e.*
from example e join
(select name, count(*) as cnt
from example
group by name
) en
on e.name = en.name
order by cnt desc, e.name, e.id;
Note that the order by not only orders by the count. It also orders by the name. If two names have the same count, then it will keep them together.

Related

How to merge same Ids data with mysql?

I have records like this after applying joins
id name item
1 bil Books
2 mike Table
2 mike chair
3 josh pen
3 josh pencil
4 peter copy
But, I want them to look like this
id name item
1 bil Books
2 mike Table,chair
3 josh pen, pencil
4 peter copy
How to achieve this?
Use group by and group_concat():
select id, name, group_concat(item) items
from mytable
group by id, name

Multi-event tournament standings

I am trying to grab a participants rankings in a multi-event tournament.
I can do a ranking for a single event pretty easily. Is there a way to find ALL in one go?
Given input: "Bob"
Data example: Desired output:
Name | Event | Score Name | Event | Score | Rank
-------------------- ----------------------------
Bob 1 100 Bob 1 100 1
Bob 2 75 Bob 2 75 3
Bob 3 80 Bob 3 80 2
Jill 2 90
Jill 3 60
Chris 1 70
Chris 2 50
Chris 3 100
Amy 1 85
Amy 2 95
Amy 3 65
The catch: I do not have access to the Rank()
function with my version of SQL, and updating is not possible in this scenario.
Clearly I could just do the score per event separately in a loop,
but I'd like to try to do it all in one go.
You can emulate a ranking function in MySQL using a self-join to values with a higher score in the same Event, and then counting the number of higher scores for each participant:
SELECT s1.Name, s1.Event, s1.Score, COUNT(s2.Name)+1 AS Rank
FROM scores s1
LEFT JOIN scores s2 ON s2.Event = s1.Event AND s2.Score > s1.Score
WHERE s1.Name = 'Bob'
GROUP BY s1.Name, s1.Event, s1.Score
ORDER BY s1.Name, s1.Event
Output:
Name Event Score Rank
Bob 1 100 1
Bob 2 75 3
Bob 3 80 2
Demo on dbfiddle

SQL query that finds all the column, that is in multiple rows, but have different value in another column?

If I have a "SALES" table, with columns of SaleID, Product#, and CustomerName. and a PRODUCTS table with two columns product_ID and Name. The contains 5 differnt products. In the SALES table populates when a sale is made.
How would I query customer_name with only Product_ID of 1 and 2?
sales table
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 DAVE
2 2 DAVE
3 3 DAVE
4 1 TOM
5 2 TOM
6 1 JANE
7 1 MIKE
8 1 MIKE
9 3 MIKE
10 4 MARY
I would like a table result to be
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 TOM
2 2 TOM
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =1
INTERSECT
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =2

How to group this table column in MySQL?

I have a following data below:
Table:
Name Number
Sam 1
Sam 2
Sam 3
John 4
Lani 5
Mera 6
Now I want as a result like this format below
Result:
Name Number
Sam 1,2,3
John 4
Lani 5
Mera 6
How can I possibly do this? What I must going to use?
You can do this with group_concat():
select name, group_concat(number order by number) as numbers
from t
group by name;

multiple subquery result into main query, select in select

OK have 2 tables
user_id login_history
1 2011-01-01
1 2011-01-02
1 2011-03-05
1 2011-04-05
1 2011-06-07
2 2011-01-01
2 2011-01-02
3 2011-03-05
3 2011-04-05
3 2011-06-07
user_id user_details
1 Jack
2 Jeff
3 Irin
What kind of query can I use to get a result like
1. Jack 2011-01-01 2011-01-02 2011-03-05
2. Jeff 2011-01-01 2011-01-02
3. Irin 2011-03-05 2011-04-05 2011-06-07
Basically I want latest 3 records from table one and be joint with table 2
The query I used will get me a list of below, which is vertical records
Jack ,2011-01-01
Jack ,2011-01-02
Jack ,2011-03-05
Jeff ,2011-01-01
Jeff ,2011-01-02
Irin ,2011-03-05
Irin ,2011-04-05
Irin ,2011-06-07
Please help
select t2.user_details,
substring_index(group_concat(login_history order by login_history separator ' '),' ',3) as recents
from table_2 as t2
left join table_1 as t1
on t1.user_id = t2.user_id
group by t2.user_id
in your example you list first three records, not the last three. By the way you would have just to add desc to the order clause within group_concat if you need it.