/    Sign up×
Community /Pin to ProfileBookmark

Winning / Losing Streak

I have a simple mySQL table which has team1 and team2 along with team1Pts and team2Pts.

I have heaps of teams. But I’ll be doing the follow when considering a specific team ID. Say ID: 1.

Team ID 1, could be team1 or team2 in the database.

I need to find out the most number of times my TeamID 1 has had more points than any other team. AKA a winning streak?

I can list all the winning games with the use of CASE etc.. But not idea if this can be done.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmOct 06.2021 — Are you saying the relationship of team id and team is not a unique one? Very confusing description. How about trying over?
Copy linkTweet thisAlerts:
@kiwisauthorOct 06.2021 — @ginerjm#1637920

No, I'm saying the teamID which I'm using to filter my results could be either Team1 or Team2. Example Rows

Team1ID: 1, Team2ID: 4, Team1Points: 6, Team2Points: 3

Team1ID: 8, Team2ID: 1, Team1Points: 11, Team2Points: 7

Team1ID: 3, Team2ID: 1, Team1Points: 1, Team2Points: 4

Team1ID: 1, Team2ID: 14, Team1Points: 9, Team2Points: 1
Copy linkTweet thisAlerts:
@ginerjmOct 06.2021 — No dates? You say a 'winning streak' which means wins without a loss. Without a game order you can't do that.
Copy linkTweet thisAlerts:
@kiwisauthorOct 06.2021 — @ginerjm#1637924

Yes, there's dates and other columns too. Cutting down the columns to simply explain the confusion.
Copy linkTweet thisAlerts:
@ginerjmOct 06.2021 — So you need a query that selects the team id and the scores from each game in date sequence. Then loop thru those results counting the winning games and stopping when you see a loss.
Copy linkTweet thisAlerts:
@ginerjmOct 06.2021 — This might work and it might not. No data to work with so made it up.
[code]
//******
$sel_team = 1;
//******
$q = 'select game_dt, team1_id, team1_scr, team2_id, team2_scr
from games_tbl
where team1_id = :sel_team1 or team2_id = :sel_team2
order by game_dt';
$parms = array(
'sel_team1'=>$sel_team,
'sel_team2'=>$sel_team);
$qst = $pdo->prepare($q);
$qst->execute($parms);
$streaks = array();
$streak_cnt = 0;
$hit_loss = 0;
$last_dt = '';
while(list($dt, $id1, $scr1, $id2, $scr2) = $qst->fetch(PDO::FETCH_NUM))
{
if ($id1 == $sel_team)
{
if ($scr1 > $scr2) // a win
{
$streak_cnt++;
$last_dt = $dt;
}
else // a loss
{
if ($streak_cnt <> 0)
{
// hit a loss so save the current cnt & end date
$streaks[] = array($streak_cnt, $last_dt);
$streak_cnt = 0;
$last_dt = '';
}
}
}
elseif ($id2 == $sel_team)
{
if ($scr2 > $scr1) // a win
{
$streak_cnt++;
$last_dt = $dt;
}
else // a loss
{
if ($streak_cnt <> 0)
{
// hit a loss so save the current cnt & end date
$streaks[] = array($streak_cnt, $last_dt);
$streak_cnt = 0;
$last_dt = '';
}
}
}
}
echo "For $sel_team there were winning streaks of:<br>";
foreach($streaks as $k=>$v)
echo "{$v[0]} games ending on {$v[1]}<br>";
/code]
This will keep track of all the select team's streaks with their end dates. If you wan to add the start date, I'll let you play with that.
Copy linkTweet thisAlerts:
@NogDogOct 07.2021 — Just wondering if (assuming PDO style place-holder for the team of interest):
[code=php]
$sql = <<<EOD
select
gameDate,
case
when Team1Score > Team2Score then true
else false
end as win
from (
(
select gameDate, Team1Points as Team1Score, Team2Points as Team2Score
from Games where Team1ID = :team_id
) union (
select gameDate, Team2Points as Team1Score, Team1Points as Team2Score
from Games where Team2ID = :team_id
)
) as games
order by gameDate
EOD;
[/code]

Assuming that works, I might try something like this to get the longest winning streak (until I think of a better way):
[code=php]
$longest = $current = 0;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if($row['win']) {
$current++;
if($current > $longest) {
$longest = $current;
}
} else {
$current = 0;
}
}
[/code]
Copy linkTweet thisAlerts:
@ginerjmOct 07.2021 — My approach is not like yours, but it did not need the extra parm that you suggest.
Copy linkTweet thisAlerts:
@NogDogOct 07.2021 — @ginerjm#1637952

Definitely more than one way to skin that cat. (I was more interested in of thinking up a "clever" -- if not necessarily "better" -- SQL method. ;) )
Copy linkTweet thisAlerts:
@ginerjmOct 07.2021 — I chose what I thought was the easy way since this was a gratis problem solving task.
Copy linkTweet thisAlerts:
@kiwisauthorOct 08.2021 — @NogDog#1637930

Cheers NogDog, I went down this path but then the situation changed and we wanted a list. I decided to follow a simuilar path but put streaks into an array outside of SQL

So In my SQL I work out if the game was a win and indicate it via a ''W' and a loss of draw with a ''-'.

Then as I loop my results I do this

``<i>
</i> while ($row = $result-&gt;fetch_assoc()) {
if($row['OutCome'] == '-'){ // If outcome was not a win
if ($flush == 1){ // counter has been rolling and we've stopped it. End of results load into array
$masterArray[] = array('First'=&gt; $firstID, 'Last' =&gt; $Last, 'Count' =&gt; $counter, 'IDs' =&gt; $IDs);
}
// Reset settings
$firstID = 0;
$flush = 0;
$counter = 0;
$IDs = array();
} else { // Outcome is a win
if($counter == 0){ // First one. Saved ID
$firstID = $row['gameId'];
}
$Last = $row['gameId']; // Update as last
$IDs[] = $row['gameId']; // Save ID into array
$flush = 1; // We're off and counting
$counter++; // Streak count
}
}
// If exited on last loop, load what we have
$masterArray[] = array('First'=&gt; $firstID, 'Last' =&gt; $Last, 'Count' =&gt; $counter, 'IDs' =&gt; $IDs);<i>
</i>
``

I then order my master array and slice it.

It works, but is there a tidier way of doing this code?
Copy linkTweet thisAlerts:
@konstantineaquaviOct 14.2021 — Without a game order you can't do that.

[url=https://testmyspeed.onl/][color=#000000]Speed Test[/color][/url]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...