MySQL How to calculate Partnership Scores from Cricket ball by ball database [Solved]
I'm trying to create a simple cricket application, where i will store all cricket records from a database. I collect a sample database with data from online and i successfully did some query but I'm unable to write the query for finding Partnership Scored
for a match. I have no clue how can i do this.
Below is the three main Table which i will used to get the data:
1. ball_by_ball table, this table holds full information by ball by ball:
2. batsman_scored, this table holds batsman run scores by ball by ball:
3. wicket_taken, this table hold wicket fallen information by ball by ball:
I tried below query but I'm unable to group those properly
select P.Player_Name, P1.Player_Name, sum(b.runs_scored) as Runs
from ball_by_ball a
inner join batsman_scored b using (match_id, over_id, ball_id, innings_no)
inner join Player P on a.striker = P.Player_Id
inner join Player P1 on a.non_striker = P1.Player_Id
where a.match_id = 981018
and a.innings_no = 1 group by P.Player_Name, P1.Player_Name;
So this my output: (this is not what i want)
If you look above screenshot, there Row 1 and 2 will be same,
AJ Finch BB McCullum and BB McCullum AJ Finch
will be same row and the
Runs column will be sum. That's what i want but i'm unable to do it.
UPDATED- This is the finally query which gives the partnership information:
select P.Player_Name, Runs + Extra as Partnership, P1.Player_Name
from (select if(striker > non_striker,
concat(striker),
concat(non_striker)) as Bat1,
sum(b.runs_scored) as Runs,
ifnull(sum(e.extra_runs), 0) as Extra,
if(striker > non_striker,
concat(non_striker),
concat(striker)) as Bat2
from ball_by_ball a
left join batsman_scored b using (match_id, over_id, ball_id, innings_no)
left join extra_runs e using (match_id, over_id, ball_id, innings_no)
where a.match_id = 981020
and a.innings_no = 1
group by if(striker > non_striker,
concat(striker, '-', non_striker),
concat(non_striker, '-', striker)), Bat1, Bat2) as t
inner join Player P on t.Bat1 = P.Player_Id
inner join Player P1 on t.Bat2 = P1.Player_Id;
This is the final output which i wanted:
mysql mysql-8.0
New contributor
add a comment |
I'm trying to create a simple cricket application, where i will store all cricket records from a database. I collect a sample database with data from online and i successfully did some query but I'm unable to write the query for finding Partnership Scored
for a match. I have no clue how can i do this.
Below is the three main Table which i will used to get the data:
1. ball_by_ball table, this table holds full information by ball by ball:
2. batsman_scored, this table holds batsman run scores by ball by ball:
3. wicket_taken, this table hold wicket fallen information by ball by ball:
I tried below query but I'm unable to group those properly
select P.Player_Name, P1.Player_Name, sum(b.runs_scored) as Runs
from ball_by_ball a
inner join batsman_scored b using (match_id, over_id, ball_id, innings_no)
inner join Player P on a.striker = P.Player_Id
inner join Player P1 on a.non_striker = P1.Player_Id
where a.match_id = 981018
and a.innings_no = 1 group by P.Player_Name, P1.Player_Name;
So this my output: (this is not what i want)
If you look above screenshot, there Row 1 and 2 will be same,
AJ Finch BB McCullum and BB McCullum AJ Finch
will be same row and the
Runs column will be sum. That's what i want but i'm unable to do it.
UPDATED- This is the finally query which gives the partnership information:
select P.Player_Name, Runs + Extra as Partnership, P1.Player_Name
from (select if(striker > non_striker,
concat(striker),
concat(non_striker)) as Bat1,
sum(b.runs_scored) as Runs,
ifnull(sum(e.extra_runs), 0) as Extra,
if(striker > non_striker,
concat(non_striker),
concat(striker)) as Bat2
from ball_by_ball a
left join batsman_scored b using (match_id, over_id, ball_id, innings_no)
left join extra_runs e using (match_id, over_id, ball_id, innings_no)
where a.match_id = 981020
and a.innings_no = 1
group by if(striker > non_striker,
concat(striker, '-', non_striker),
concat(non_striker, '-', striker)), Bat1, Bat2) as t
inner join Player P on t.Bat1 = P.Player_Id
inner join Player P1 on t.Bat2 = P1.Player_Id;
This is the final output which i wanted:
mysql mysql-8.0
New contributor
add a comment |
I'm trying to create a simple cricket application, where i will store all cricket records from a database. I collect a sample database with data from online and i successfully did some query but I'm unable to write the query for finding Partnership Scored
for a match. I have no clue how can i do this.
Below is the three main Table which i will used to get the data:
1. ball_by_ball table, this table holds full information by ball by ball:
2. batsman_scored, this table holds batsman run scores by ball by ball:
3. wicket_taken, this table hold wicket fallen information by ball by ball:
I tried below query but I'm unable to group those properly
select P.Player_Name, P1.Player_Name, sum(b.runs_scored) as Runs
from ball_by_ball a
inner join batsman_scored b using (match_id, over_id, ball_id, innings_no)
inner join Player P on a.striker = P.Player_Id
inner join Player P1 on a.non_striker = P1.Player_Id
where a.match_id = 981018
and a.innings_no = 1 group by P.Player_Name, P1.Player_Name;
So this my output: (this is not what i want)
If you look above screenshot, there Row 1 and 2 will be same,
AJ Finch BB McCullum and BB McCullum AJ Finch
will be same row and the
Runs column will be sum. That's what i want but i'm unable to do it.
UPDATED- This is the finally query which gives the partnership information:
select P.Player_Name, Runs + Extra as Partnership, P1.Player_Name
from (select if(striker > non_striker,
concat(striker),
concat(non_striker)) as Bat1,
sum(b.runs_scored) as Runs,
ifnull(sum(e.extra_runs), 0) as Extra,
if(striker > non_striker,
concat(non_striker),
concat(striker)) as Bat2
from ball_by_ball a
left join batsman_scored b using (match_id, over_id, ball_id, innings_no)
left join extra_runs e using (match_id, over_id, ball_id, innings_no)
where a.match_id = 981020
and a.innings_no = 1
group by if(striker > non_striker,
concat(striker, '-', non_striker),
concat(non_striker, '-', striker)), Bat1, Bat2) as t
inner join Player P on t.Bat1 = P.Player_Id
inner join Player P1 on t.Bat2 = P1.Player_Id;
This is the final output which i wanted:
mysql mysql-8.0
New contributor
I'm trying to create a simple cricket application, where i will store all cricket records from a database. I collect a sample database with data from online and i successfully did some query but I'm unable to write the query for finding Partnership Scored
for a match. I have no clue how can i do this.
Below is the three main Table which i will used to get the data:
1. ball_by_ball table, this table holds full information by ball by ball:
2. batsman_scored, this table holds batsman run scores by ball by ball:
3. wicket_taken, this table hold wicket fallen information by ball by ball:
I tried below query but I'm unable to group those properly
select P.Player_Name, P1.Player_Name, sum(b.runs_scored) as Runs
from ball_by_ball a
inner join batsman_scored b using (match_id, over_id, ball_id, innings_no)
inner join Player P on a.striker = P.Player_Id
inner join Player P1 on a.non_striker = P1.Player_Id
where a.match_id = 981018
and a.innings_no = 1 group by P.Player_Name, P1.Player_Name;
So this my output: (this is not what i want)
If you look above screenshot, there Row 1 and 2 will be same,
AJ Finch BB McCullum and BB McCullum AJ Finch
will be same row and the
Runs column will be sum. That's what i want but i'm unable to do it.
UPDATED- This is the finally query which gives the partnership information:
select P.Player_Name, Runs + Extra as Partnership, P1.Player_Name
from (select if(striker > non_striker,
concat(striker),
concat(non_striker)) as Bat1,
sum(b.runs_scored) as Runs,
ifnull(sum(e.extra_runs), 0) as Extra,
if(striker > non_striker,
concat(non_striker),
concat(striker)) as Bat2
from ball_by_ball a
left join batsman_scored b using (match_id, over_id, ball_id, innings_no)
left join extra_runs e using (match_id, over_id, ball_id, innings_no)
where a.match_id = 981020
and a.innings_no = 1
group by if(striker > non_striker,
concat(striker, '-', non_striker),
concat(non_striker, '-', striker)), Bat1, Bat2) as t
inner join Player P on t.Bat1 = P.Player_Id
inner join Player P1 on t.Bat2 = P1.Player_Id;
This is the final output which i wanted:
mysql mysql-8.0
mysql mysql-8.0
New contributor
New contributor
edited 9 mins ago
Rhidoy
New contributor
asked 17 hours ago
RhidoyRhidoy
33
33
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your grouping is a player pair so your GROUP BY
expression needs to reflect this. The striker
/ non_striker
must combined form a unique value. Start with an IF
statement to avoid duplicates. I've used a mathematical expression that won't cause duplicates.
So with your query use the following group by:
GROUP BY IF(striker > non_striker,
striker * 30 + non_striker,
non_striker * 30 + striker)
This works because assuming all player ids are < 30. If this isn't the case it can be replaced by a higher value.
Alternately you could use a string based mechanism to get 1-2
as unique identifier. Note a separator -
is needed (and can be anything not in the result set), otherwise striker,non_striker
value 1,234
would be grouped the same as 12,34
GROUP BY IF(striker > non_striker,
CONCAT(striker, '-', non_striker),
CONCAT(non_striker, '-', striker)
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solutionselect sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add theSELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.
– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Rhidoy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231237%2fmysql-how-to-calculate-partnership-scores-from-cricket-ball-by-ball-database-so%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your grouping is a player pair so your GROUP BY
expression needs to reflect this. The striker
/ non_striker
must combined form a unique value. Start with an IF
statement to avoid duplicates. I've used a mathematical expression that won't cause duplicates.
So with your query use the following group by:
GROUP BY IF(striker > non_striker,
striker * 30 + non_striker,
non_striker * 30 + striker)
This works because assuming all player ids are < 30. If this isn't the case it can be replaced by a higher value.
Alternately you could use a string based mechanism to get 1-2
as unique identifier. Note a separator -
is needed (and can be anything not in the result set), otherwise striker,non_striker
value 1,234
would be grouped the same as 12,34
GROUP BY IF(striker > non_striker,
CONCAT(striker, '-', non_striker),
CONCAT(non_striker, '-', striker)
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solutionselect sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add theSELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.
– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
add a comment |
Your grouping is a player pair so your GROUP BY
expression needs to reflect this. The striker
/ non_striker
must combined form a unique value. Start with an IF
statement to avoid duplicates. I've used a mathematical expression that won't cause duplicates.
So with your query use the following group by:
GROUP BY IF(striker > non_striker,
striker * 30 + non_striker,
non_striker * 30 + striker)
This works because assuming all player ids are < 30. If this isn't the case it can be replaced by a higher value.
Alternately you could use a string based mechanism to get 1-2
as unique identifier. Note a separator -
is needed (and can be anything not in the result set), otherwise striker,non_striker
value 1,234
would be grouped the same as 12,34
GROUP BY IF(striker > non_striker,
CONCAT(striker, '-', non_striker),
CONCAT(non_striker, '-', striker)
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solutionselect sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add theSELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.
– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
add a comment |
Your grouping is a player pair so your GROUP BY
expression needs to reflect this. The striker
/ non_striker
must combined form a unique value. Start with an IF
statement to avoid duplicates. I've used a mathematical expression that won't cause duplicates.
So with your query use the following group by:
GROUP BY IF(striker > non_striker,
striker * 30 + non_striker,
non_striker * 30 + striker)
This works because assuming all player ids are < 30. If this isn't the case it can be replaced by a higher value.
Alternately you could use a string based mechanism to get 1-2
as unique identifier. Note a separator -
is needed (and can be anything not in the result set), otherwise striker,non_striker
value 1,234
would be grouped the same as 12,34
GROUP BY IF(striker > non_striker,
CONCAT(striker, '-', non_striker),
CONCAT(non_striker, '-', striker)
Your grouping is a player pair so your GROUP BY
expression needs to reflect this. The striker
/ non_striker
must combined form a unique value. Start with an IF
statement to avoid duplicates. I've used a mathematical expression that won't cause duplicates.
So with your query use the following group by:
GROUP BY IF(striker > non_striker,
striker * 30 + non_striker,
non_striker * 30 + striker)
This works because assuming all player ids are < 30. If this isn't the case it can be replaced by a higher value.
Alternately you could use a string based mechanism to get 1-2
as unique identifier. Note a separator -
is needed (and can be anything not in the result set), otherwise striker,non_striker
value 1,234
would be grouped the same as 12,34
GROUP BY IF(striker > non_striker,
CONCAT(striker, '-', non_striker),
CONCAT(non_striker, '-', striker)
edited 7 hours ago
answered 8 hours ago
danblackdanblack
1,9391213
1,9391213
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solutionselect sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add theSELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.
– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
add a comment |
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solutionselect sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add theSELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.
– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
well this works only when i don't select striker or non_striker in output, when i select those i have to add those in group by clause thus my result grouped by striker or non_striker and result be the same as my question showing result. so how can i get striker and no_striker by using your solution? or is my query is wrong?
– Rhidoy
1 hour ago
this is my query by your solution
select sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
this is my query by your solution
select sum(b.runs_scored) as Runs from ball_by_ball a left join batsman_scored b using (match_id, over_id, ball_id, innings_no) inner join Player P on a.striker = P.Player_Id inner join Player P1 on a.non_striker = P1.Player_Id where a.match_id = 981018 and a.innings_no = 1 group by if(striker > non_striker, concat(striker, '-', non_striker), concat(non_striker, '-', striker));
– Rhidoy
1 hour ago
You could add the
SELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.– danblack
1 hour ago
You could add the
SELECT IF(....) as bater_ids ... GROUP BY bater_ids
but, no grouping or listing them individually doesn't make sense.– danblack
1 hour ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
thank you so much for your time, i make it as answer. without those ids its not clear who is batting. finally the problem solve and i update final query in my question, please have a look at it.
– Rhidoy
8 mins ago
add a comment |
Rhidoy is a new contributor. Be nice, and check out our Code of Conduct.
Rhidoy is a new contributor. Be nice, and check out our Code of Conduct.
Rhidoy is a new contributor. Be nice, and check out our Code of Conduct.
Rhidoy is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231237%2fmysql-how-to-calculate-partnership-scores-from-cricket-ball-by-ball-database-so%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown