This is a continuation of my original question on how to calculate the longest undefeated streak .
I modified the data table to add a “place” column to show whether the game was “home” or “away”:
date result venue
-----------------------------
1980-08-16 WH
1980-08-19 LA
1980-08-23 WA
1980-08-26 WH
1980-08-30 DH
and so on ...
I was able to calculate the longest common bands due to my previous question, but now I would like to calculate these bands at the meeting place, and I'm not sure how to do this.
For most home home victories, for example, I tried the following query - but the received “runs” are returned shorter than they actually are (based on manual data counting), and there is no obvious interruption
SQL:
SELECT result, venue, MIN(date) as StartDate, MAX(date) as EndDate, COUNT(*) as Games FROM ( SELECT result, venue, date, ( SELECT COUNT(*) FROM resultengine R WHERE R.result <> RE.result AND (R.venue = 'H') <> (RE.venue = 'H') AND R.date <= RE.date ) as RunGroup FROM resultengine RE ) A WHERE venue = 'H' AND result='W' GROUP BY result, RunGroup ORDER BY Games
PHP:
$result = mysql_query( ); while($row=mysql_fetch_assoc($result)) { $startrundate = date("d FY",strtotime($row['StartDate'])); $endrundate = date("d FY",strtotime($row['EndDate'])); echo "<tr>"; echo "<td>".$row['Games']."</td>"; echo "<td class='tableprofile' style='text-align:right;'>".$startrundate." - ".$endrundate."</td>"; echo "</tr>"; $rowCount += 1; }
It turned out to be more difficult than I imagined, but in the same way I do not think that I am too far from the solution. It’s just bridging the gap between what I have and what I’m not doing right now.
UPDATE
I seem to have solved this specific problem. And it was as simple as changing the sentence (R.venue = 'H') <> (RE.venue = 'H') to R.venue = RE.venue
sql php mysql
Pete hayman
source share