Jump to content

Using MAX() and three tables, possibly four, need help with what goes where now lol


0o0o0

Recommended Posts

Ok i think im over my head, ive come to a brick wall, and usually when this happens a reply of the simpliest form comes back. I might be thinking to hard..   here it goes..

Table ONE

champshorses2014  stats will always stay the same.

 

Table TWO

champshorses is updated everyday from the source.

 

Table THREE

horsesintoday is updated daily three times a day.

 

 

basically I have created two SELECT statements and all im really trying to figure out is how to merge the two together properly

 

so the final out put would appear as so...

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  ( and joined here)  (same R1) Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  ( and joined here)  (same R1)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT  ( and joined here)  (same R1)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT  ( and joined here)  (same R1)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE5  STAT STAT STAT  ( and joined here)  (same R1)Maxstat maxstat Maxstat 

 

TRACK CHURCHILL RACE2 HORSE1  STAT STAT STAT  ( and joined here)  (same R2)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE2  STAT STAT STAT  ( and joined here)  (sameR2)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE3  STAT STAT STAT  ( and joined here)  (sameR2)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE4  STAT STAT STAT  ( and joined here)  (sameR2)Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE5  STAT STAT STAT  ( and joined here)  (sameR2)Maxstat maxstat Maxstat 

 

even tho all the MAX STATS at the ends are the same max numbers, of each Track, Race number group.. doesnt matter..

 

 

I need these merged to print out or return as above in single lines..

 

Basically im not knowledgeable of joining, left join, inner join or merge, or what is needed to mash these two select statements together as one.

 

I have

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT 

 

TRACK CHURCHILL RACE2 HORSE1  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE2  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE3  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE4  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE5  STAT STAT STAT  

 

with the first code..

 

and

 

TRACK CHURCHILL RACE1   Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE3  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE4  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE5  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE6  Maxstat maxstat Maxstat

 

with the second code..

 

 

need em mashed.. to appear like so...

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE5  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

 

TRACK CHURCHILL RACE2 HORSE1  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE2  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE3  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE4  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE5  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

 

So Maxstats at ends will be the numbers in select statement two I posted below..

 

Actually it should appear like this when done...

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  ( and joined here)  24 36 5000 

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  ( and joined here)  24 36 5000 

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT  ( and joined here)  24 36 5000 

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT  ( and joined here)  24 36 5000 

TRACK CHURCHILL RACE1 HORSE5  STAT STAT STAT  ( and joined here)  24 36 5000 

 

even tho they are the same numbers.. the 24 could be from horse 1 first stat as highest, and 36 from horse 5 second stat highest, and horse 3 has the hiighest last stat, doesnt matter theur added to all the ends.. whatever works..

 

 

 

 

then in php I will just simply do a php if statement..

 

if ($t1.Track = $t4.Track and $t1.Race = $4t.Race And $t2.rank = $t4.maxrank)
  $col14 = "#006600";

 

if ($t1.Track = $t4.Track and $t1.Race = $4t.Race And $t3.rank2014 = $t4.maxrank2014)
  $col14 = "#006600";

etc.. through out.

 

something like that.. but probably dont need the t2.t3.t4. as they will be rank rank2014 and maxrank maxrank2014 anyhow..

dont worry about this this I can do later.
 

just need the two mashed lol.

Code 1

appears something like this when executed..

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT 

 

TRACK CHURCHILL RACE2 HORSE1  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE2  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE3  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE4  STAT STAT STAT  

TRACK CHURCHILL RACE2 HORSE5  STAT STAT STAT  


Select statement 1:

SELECT t1.Horse, t1.Track, t1.Race, t1.Jockey, t1.Trainer, t1.Owner, t1.Date, t2.speedFigure, t2.rank, t2.perStart, t2.topThree, t2.topThreePercentage,  t2.winPercentage, t2.starts, t2.win, t2.place, t2.show, t2.earnings, t3.speedFigure2014, t3.rank2014, t3.perStart2014, t3.topThree2014, t3.topThreePercentage2014,  t3.winPercentage2014, t3.starts2014, t3.win2014, t3.place2014, t3.show2014, t3.earnings2014, (t2.speedFigure + t3.speedFigure2014) AS figtote, (t2.rank + t3.rank2014) AS ranktote, (t2.perStart + t3.perStart2014) AS avgtote, (t2.topThree + t3.topThree2014) AS pronum, (t2.topThreePercentage + t3.topThreePercentage2014) AS pronumpercent FROM champsintoday as t1 LEFT JOIN champshorses as t2 ON t1.HorseRef = t2.referenceNumber LEFT JOIN champshorses2014 as t3 ON t1.HorseRef = t3.referenceNumber2014 WHERE t1.Date = CURDATE() ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.speedFigure DESC

Statement 2 this grabs only the maxs figures of all horses in each race at each track, single lines

like so..

 

TRACK CHURCHILL RACE1   Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE3  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE4  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE5  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE6  Maxstat maxstat Maxstat

 

Select statement two:


SELECT t1.Track, t1.Race, MAX(t2.speedFigure) AS maxf2015, MAX(t3.speedFigure2014) AS maxf2014, MIN(t2.rank) AS maxr2015, MIN(t3.rank2014) AS maxr2014, MAX(t2.perStart) AS maxa2015, MAX(t3.perStart2014) AS maxa2014, MAX(t2.speedFigure + t3.speedFigure2014) AS maxtoprating, MIN(t2.rank + t3.rank2014) AS maxtoprank, MAX(t2.perStart + t3.perStart2014) AS maxtopavg, MAX(t2.starts) as maxstarts, MAX(t2.winPercentage) as maxwinPercentage, MAX(t2.topThreePercentage) as maxtopThreePercentage, MAX(t2.win) as maxwin, MAX(t2.place) as maxplace, MAX(t2.show) as maxshow,MAX(t2.topThree) as maxtopThree,MAX(t2.earnings) as maxearnings, MAX(t3.starts2014) as maxstarts2014, MAX(t3.winPercentage2014) as maxwinPercentage2014, MAX(t3.topThreePercentage2014) as maxtopThreePercentage2014, MAX(t3.win2014) as maxwin2014, MAX(t3.place2014) as maxplace2014,MAX(t3.show2014) as maxshow2014, MAX(t3.topThree2014) as maxtopThree2014,MAX(t3.earnings2014) as maxearnings2014, MAX(t2.topThree + t3.topThree2014) AS maxpronum, MAX(t2.topThreePercentage + t3.topThreePercentage2014) AS maxpronumpercent,(t2.speedFigure + t3.speedFigure2014) AS maxfigtote, (t2.rank + t3.rank2014) AS maxranktote, (t2.perStart + t3.perStart2014) AS maxavgtote FROM champsintoday as t1 LEFT JOIN champshorses as t2 ON t1.HorseRef = t2.referenceNumber LEFT JOIN champshorses2014 as t3 ON t1.HorseRef = t3.referenceNumber2014 WHERE t1.Date = CURDATE() GROUP BY t1.Date, t1.track, t1.Race ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.speedFigure DESC

im just clueless as to what I need... join? left join? where to put brackets, or whatever is needed to make these two codes spit out single full figure lines. If I told ya how many attempts and weeks hours its taken to get these two together in one statement youd probably laugh.. lets just say every day for a few hours since june 22nd. lol.. meanwhile working around it and adding to it, but cannot for the life of me place the two together into one properly.

This is a personal hobby, not a job or commercial project, just basically mine that opens once in a browser and stays on my laptop for a day during the weekend.. So loading time, efficency or spaghetti coding is not an issue, and help is great as long as it works. lol! thanks.

 

Edited by 0o0o0
Link to comment
Share on other sites

I guess the "ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.speedFigure DESC" wont be needed in both.  Is this simple? am i over looking it? Im worried about the replies already. Thanks guys.

Edited by 0o0o0
Link to comment
Share on other sites

Instead of two history data tables (champhorses and champhorses2014) which have identical structure, combine and extract this year and previous year stats based on the date. However, as you currently have it you need a union of the two table to get the max values

 

Something like this

SELECT 
  t1.Horse
, t1.Track
, t1.Race
, t1.Jockey
, t1.Trainer
, t1.Owner
, t1.Date
, t2.perStart
, t2.rank
, t2.speedFigure
, t3.perStart2014
, t3.rank2014
, t3.speedFigure2014
, maxps
, maxrank
, maxsf
FROM champsintoday t1
LEFT JOIN champshorses t2 ON t1.HorseRef = t2.referenceNumber
LEFT JOIN champshorses2014 t3 ON t1.HorseRef = t3.referenceNumber2014
INNER JOIN 
	(
	SELECT referenceNumber
    , MAX(perStart) as maxps
	, MAX(rank) as maxrank
	, MAX(speedFigure) as maxsf
	FROM 
		(
		SELECT referenceNumber
        , perStart
        , rank
        , speedFigure
        FROM champshorses
        UNION 
        SELECT referenceNumber2014
        , perStart2014
        , rank2014
        , speedFigure2014
        FROM champshorses2014
        ) as bothtbls
	GROUP BY referenceNumber
    ) as t4 ON t1.HorseRef = t4.referenceNumber
WHERE t1.Date = CURDATE()
ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.speedFigure DESC
Link to comment
Share on other sites

Just getting select statement 2 to attach to select statement 1 by Track and Race.. Select statement 2 will be the same numbers for all horses in each race, as they are the max figures of the entire race. I just need need em to join together lol.. its there! its soo close and I can figure out the structure of the code to do so.  

 

Select statement 1 ---> select statement 2 ..    joined..  by  CURDATE, Track, Race (basically the race number) 

 

and the desired result appears like..

 

TRACK CHURCHILL RACE1 HORSE1  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE2  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE3  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE4  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE1 HORSE5  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

 

TRACK CHURCHILL RACE2 HORSE1  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE2  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE3  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE4  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

TRACK CHURCHILL RACE2 HORSE5  STAT STAT STAT  ( and joined here)  Maxstat maxstat Maxstat 

 

 

 

how do ya do it? maybe if ripped down the code it wont be so confusing to read.

SELECT t1.Horse, t1.Track, t1.Race,  t1.Date,  t2.win,  t3.win2014 FROM champsintoday as t1 LEFT JOIN champshorses as t2 ON t1.HorseRef = t2.referenceNumber LEFT JOIN champshorses2014 as t3 ON t1.HorseRef = t3.referenceNumber2014 WHERE t1.Date = CURDATE() ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.win DESC

and put this on the end.. 

SELECT t1.Track, t1.Race, MAX(t2.win) AS maxwin2015, MAX(t3.win) AS maxwin2014MAX FROM champsintoday as t1  LEFT JOIN champshorses as t2 ON t1.HorseRef = t2.referenceNumber LEFT JOIN champshorses2014 as t3 ON t1.HorseRef = t3.referenceNumber2014  
WHERE t1.Date = CURDATE() GROUP BY t1.Date, t1.track, t1.Race 
ORDER BY t1.Date ASC, t1.track ASC, t1.Race ASC, t2.win DESC

TRACK CHURCHILL RACE1 HORSE1  1 (win) 1 (win2014) ( and joined here)  7 (maxwin2015 col)  6 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE2  3 (win) 1 (win2014) ( and joined here)  7 (maxwin2015 col)  6 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE3  1 (win) 6 (win2014) ( and joined here)  7 (maxwin2015 col)  6 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE4  7 (win) 1 (win2014) ( and joined here)  7 (maxwin2015 col)  6 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE5  1 (win) 1 (win2014) ( and joined here)  7 (maxwin2015 col)  6 (maxwin2014 col)

 

TRACK CHURCHILL RACE1 HORSE1  2 (win) 3 (win2014) ( and joined here)  9 (maxwin2015 col)  8 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE2  7 (win) 5 (win2014) ( and joined here)  9 (maxwin2015 col)  8 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE3  6 (win) 8 (win2014) ( and joined here)  9 (maxwin2015 col)  8 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE4  9 (win) 1 (win2014) ( and joined here)  9 (maxwin2015 col)  8 (maxwin2014 col)

TRACK CHURCHILL RACE1 HORSE5  1 (win) 3 (win2014) ( and joined here)  9 (maxwin2015 col)  8 (maxwin2014 col)

 

 

( dont worry if two max numbers are the same, in the highlighting I want to do later it will highlight both horses cause their stat for win or win2015

will equal the Maxwin number col)

 

anyone understand what im trying to achieve?? or am I still not clear? please lemme know its hard to talk sql when your not all that experienced at it.

UNION was advised to me, but im still thinking JOIN.. considering the columns are not the same for UNION. I really dont know.

 

thanks for everyones time involved so far.

Edited by 0o0o0
Link to comment
Share on other sites

You have two identical stat history tables (champhorses for this year and champhorses2014 for last year). To get the maximum stat value for each horse you need to combine this data into one table - which is what the subquery with the UNION is doing. Having got the maxima for each horse, this subquery is then JOINED to match with each horse

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.