Show different count of different remarks for distinct users - mysql

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

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.

How to check for one value in multiple rows

Whats the best way to check if different groups of rows in a table with the same GroupID such as different teams have a SINGLE captain? Captain Identifier for example could be '10', so its crucial that it goes through multiple records with the same groupID and checks to see if theres ONLY ONE record with the positionID as '10'. I need it to do it for all teams, i.e all groupID's
-------------------------------------------------
ID | Group ID | Name | Position|
-------------------------------------------------
1 1 John 3
2 1 jim 3
3 1 Hahn 4
4 1 Mary 4
5 1 Moe 4
6 1 Charlie 10
7 2 taylor 4
8 2 Geoff 4
9 2 adam 4
10 2 cam 10
11 3 sharon 2
12 3 tony 4
13 3 steve 3
14 3 eve 4
15 3 gwen 10
--------------------------------
So what I need it to do is check that every groupID only had ONE 10 as the position.
Thanks in advance guys. Check out the image link at the bottom.
im using mysql btw
Sorry if this is badly described
If I understood you - I think you need something like :
select groupId, sum(case when positionID='10' then 1 else 0 end) as captains
from tbl_name
group by groupID
having captains = 1
am I close???
If I understood you correctly, you want an indication that will tell you whether the groupID had more then one captain.
So what you need is this:
select groupID,case when cnt = 1 then 1 else 0 end as IND_ONE_CAPTAIN from (
select groupID, sum(case when positionid = '10' then 1 else 0 end) as cnt
from players
group by groupID)
Now IND_ONE_CAPTAIN columns consist 1 or 0, 1 is for the group id only had 1 captain, and 0 is when they had more.

MySQL sum column and display last row in column

I have a table of users,subscription packages and various user subscriptions.
I need to fetch a sum of all subscription cost and display the latest/last subscription. The latest subscription is the subscription
with the highest subscription_id. How can I write my query? My tables are listed as below.
Users table
user_id name
1 John
2 Jane
3 Matthew
Subscription Packages table
package_id package_name
1 Basic
2 Advanced
3 Premium
User Subscriptions
subscription_id user_id package_id subscription_cost date
1 1 1 2 2014-04-01
2 2 1 2 2014-04-01
3 3 1 2 2014-04-01
4 1 1 2 2014-05-01
5 1 2 3.5 2014-06-01
6 2 2 3.5 2014-06-01
7 2 2 3.5 2014-07-01
8 1 3 5 2014-07-01
9 3 2 5 2014-07-01
10 2 2 3.5 2014-08-01
11 1 1 2 2014-08-01
My results should be like so
name total_costs latest_package
John 14.5 Basic
Jane 12.5 Advanced
Matthew 7 Premium
Because you need to do an aggregation anyway, I would go for the group_concat()/substring_index() trick:
select u.user_id, u.name, sum(subscription_cost) as total_costs,
substring_index(group_concat(p.package_name order by us.date desc), ',', 1) as latest_package
from usersubscriptions us join
users u
on us.user_id = u.user_id join
packages p
on us.package_id = p.package_id
group by u.user_id;
This assumes that no package names have commas. It is also subject to default limits on the length of the result of group_concat() -- but this method often works in practice.

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