MySQL - Elegant Sum - mysql

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

Related

Gaming Database Query for multiple players

I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.

Using a three column unique combination in SQL only once

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?

Show different count of different remarks for distinct users

Please I need solution for a project I am currently working on
Users are assigned a task every week and they submit an answer. They are scored either Won or Lost for that week on the Rating Table.
I need a way to view all Users from the Rating Tables to see the number of tasks they have Won and Lost side by side
Below is the RATINGS Table of 10 Entries for 5 Users
**id** **username** **date** **remarks**
-------------------------------------------------------------------------------
1 John 2017-09-02 Won
2 Kyle 2017-09-02 Lost
3 Danny 2017-09-02 Won
4 Mike 2017-09-02 Won
5 Alli 2017-09-02 Won
6 Kyle 2017-09-09 Lost
7 John 2017-09-09 Won
8 Danny 2017-09-09 Lost
9 Mike 2017-09-09 Lost
10 Alli 2017-09-09 Won
I need to pull out the Result to look this way:
Username Total_tasks Won Lost
john 2 2 0
kyle 2 0 2
danny 2 1 1
mike 2 1 1
alli 2 2 0
Since aggregate functions, including count ignore nulls, a simple trick you can use to to count a bunch of case expressions:
SELECT username,
COUNT(*) AS total_tasks
COUNT(CASE remarks WHEN 'Won' THEN 1 END) AS won,
COUNT(CASE remarks WHEN 'Lost' THEN 1 END) AS lost
FROM mytable
GROUP BY username

SQLite COUNT and selecting from multiple tables

Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.

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