Using a three column unique combination in SQL only once - mysql

So, I'm managing a table where it's stored the scores of a particular competition.
The table looks like this:
ENTRY_ID TEAM_ID DATE PLACE SCORE
1 1 2021-10-12 Ireland 64
2 2 2021-10-12 Ireland 31
3 3 2021-10-12 France 137
4 2 2021-10-12 France 61
5 5 2021-10-12 France 38
6 1 2021-10-12 France 66
7 2 2021-10-12 Italy 17
8 3 2021-10-12 Italy 61
9 1 2021-10-12 Italy 74
The competition is held at three different places at the same time, with technically all teams being able to have teams in all of them.
Each team however can only win one point so, in the example, it's possible to see that Team 1 would win both in Italy and Ireland, but it should be awarded only one point for the highest score, so only Italy. The point in Ireland should go to the second place.
The query I was trying to get the results is:
SELECT `TEAM_ID`, `PLACE`
FROM `COMPETITION`
WHERE `date` = "2021-10-12"
GROUP BY `PLACE`
ORDER BY `SCORE` DESC, `id` ASC
LIMIT 3
So I could retrieve all three winners with no further processing.
The results I'm trying to achieve should repeat neither the TEAM_ID nor PLACE, in this particular example it should output:
3 FRANCE (Since it has the highest score in France at 137)
1 ITALY (For the highest score in Italy at 74)
2 IRELAND (For the second-highest score in Ireland, since Team 1 already won in Italy)
The production model of this table has far more entries so it's unlikely there would be any clashes with too many second-places.
How can I achieve that?

Related

How to filter users that have multiple rows where row1="value1" and row2="condition2", ... in SQL?

A have the following data
id user_id visited_country
1 12 Spain
2 12 France
3 14 England
4 14 France
5 16 Canada
6 14 Spain
7 16 Mexico
I want to select all users who have visited both Spain and France, excluding those who have visited England. How can I do that in MySQL?
One aggregation approach might be:
SELECT user_id
FROM yourTable
GROUP BY user_id
HAVING SUM(visited_country = 'Spain') > 0 AND
SUM(visited_country = 'France') > 0 AND
SUM(visited_country = 'England') = 0;

Select limit result in mysql

I have table with these data:
Id City Amount
1 London 25000
2 New York 20000
3 London 23000
4 Paris 22000
5 Moscow 18000
6 London 21000
7 New York 19000
8 Moscow 26000
9 London 24000
10 Moscow 16000
11 London 15000
12 Moscow 23000
13 Paris 19000
14 New York 15000
15 London 26000
I must create SQL as what to get the results as this?
Id City Amount
1 London 25000
2 New York 20000
3 London 23000
4 Paris 22000
5 Moscow 18000
7 New York 19000
8 Moscow 26000
13 Paris 19000
That means I just want to get the maximum of one city only to appear 2 times.
I just want to get the first two records or the last two records
Thanks!
If your MySQL version < 8.0, you can simulate RowNumber functionality, using Session variables. To achieve Partition By functionality based on grouping, we will use two session variables, one for the row number and the other for storing the old City to compare it with the current one, and increment it by 1, if belonging to same City group, else reset to 1.
Following code will get your first two records. You can easily change it to get first n records, by changing 2 to n in the query.
SET #row_number = 0;
SET #city_var = '';
SELECT inner_nest.Id,
inner_nest.City,
inner_nest.Amount
FROM (
SELECT
#row_number:=CASE
WHEN #city_var = City THEN #row_number + 1
ELSE 1
END AS num,
Id,
#city_var:=City as City,
Amount
FROM
table_name
ORDER BY
City) AS inner_nest
WHERE inner_nest.num <= 2
ORDER BY inner_nest.Id ASC
**
SQL Fiddle
**

How to find most popular genre of music?

So I have two tables, table 1:Music Styles being with fields with StyleID and StyleName, with StyleName being a genre and table 2:entertainers with fields SyleID and EntertainerID.
I need help finding is:
1) What’s the most popular genre of music style in the database of entertainers?
This is what i have so far:
SELECT StyleID, StyleName
from EA_Music_Styles
WHERE StyleID = (SELECT StyleID
from EA_Entertainer_Styles
WHERE StyleID = (SELECT MAX(StyleID)
FROM EA_Entertainer_Styles));
But i got an error code of "Subquery returns more than 1 row"
I need help, don't know exactly what to do? First time beginner.
Here are the tables:
Table 1:
StyleID StyleN
1 40's Ballroom Music
2 50's Music
3 60's Music
4 70's Music
5 80's Music
6 Country
7 Classical
8 Classic Rock & Roll
9 Rap
10 Contemporary
11 Country Rock
12 Elvis
13 Folk
14 Chamber Music
15 Jazz
16 Karaoke
17 Motown
18 Modern Rock
19 Rhythm and Blues
20 Show Tunes
21 Standards
22 Top 40 Hits
23 Variety
24 Salsa
25 90's Music
Table 2:
StyleID EntertainerID
3 1003
3 1008
4 1010
6 1007
6 1008
7 1009
7 1011
7 1012
8 1003
10 1001
10 1013
11 1007
13 1004
13 1012
14 1009
14 1011
15 1005
15 1013
17 1002
19 1002
19 1005
20 1001
20 1011
21 1001
21 1009
21 1010
22 1006
22 1010
23 1002
23 1006
24 1005
24 1006
SO the result would output the two StyleID's, 7 & 21 and StyleName which would be Classical & Standards
You just need to join your two tables and get the max count for entertainers
SELECT ms.StyleName, count(*) most_popular
from EA_Music_Styles ms
INNER JOIN A_Entertainer_Styles es
ON ms.StyleID = es.StyleID
group by ms.StyleName
order by 2 desc
limit 1
This will give you the most popular (I take that it is the one that has more rows) StyleName. I count the ocurrences of StyleName and order it desc getting only the first result of this.
Here is how you can solve the problem.
In your entertainers table user group by StyleID while also selecting the count. This will give you the styleID count which you can sort by descending and limit 1 to find to most popular one
select StyleID, count(StyleID) c from EA_Entertainer_Styles
group by StyleID
order by c desc
limit 1;
Now use the styleID form the first step to get the StyleName from your table Music.
select StyleName from EA_Music_Styles
where StyleID = id_from_step1
You can use this two in a single query by joining them as in the answer shown by Jorge Campos
Max gives you the highest number in the database, not the most used. You should use count for that.
Next example should give correct result.
select StyleName from EA_Music_Styles
where StyleID = (
select StyleID
from EA_Entertainer_Styles
group by StyleID
order by count(StyleID) desc
limit 1)

MySQL - Elegant Sum

I am looking for an elegant solution to a seemingly simple MySQL problem:
I've got a table with soccer teams and game results from a soccer league.
league day team1 team2 points1 points2
bl0910 1 munich hamburg 3 0
bl0910 1 bremen stuttgart 1 1
...
Now I want to add a column, where all points of a team from all previous game days are summed up.
Like:
league day team1 team2 points1 points2 sum1 sum2
...
bl0910 8 bremen hamburg 1 1 11 15
bl0910 8 munich stuttgart 0 3 16 9
...
Is it possible with some elegant syntax, or do I have to use cursors and a temporary table?
Kind Regards
Theo

mysql query but two differant group by

If my Data is
Name - playerID - matchID - Innings - Runs
James 1 1 1 5
James 1 1 2 8
Darren 2 1 1 3
Darren 2 1 2 9
James 1 2 1 10
James 1 2 2 12
Darren 2 2 1 13
Darren 2 2 2 19
and my sql data is
$query = "SELECT playerID, name,
SUM(runs) AS runs_scored,
MAX(runs) AS highest_score
FROM matchPlayer GROUP BY playerID";
Then the output would read
James has scored 35 runs with a highest score of 18
Darren has scored 44 runs with a highest score of 19
Now I wish to get the highest total scored in one match (that is combining innings 1 & 2)?
I have no idea how to start on this query :(
EDIT
The exact info I require is the HIGHEST match total, so James has 13 combined runs from matchID 1 and 22 combined runs from matchID 2 - so the answer I am after is 22.
You need to do it in two stages:
SELECT ms.playerID, mp.name, SUM(ms.runs_by_match) AS runs_scored,
MAX(ms.runs_by_match) as highest_score
FROM
matchPlayer as mp
INNER JOIN (
SELECT playerID, matchID, SUM(runs) AS runs_by_match
FROM matchPlayer
GROUP BY playerID, matchID
) AS ms ON mp.playerID = ms.playerID
GROUP BY
ms.playerID, mp.name