PHP_Idiot Posted August 12, 2013 Share Posted August 12, 2013 Ok so I have this query: SELECT Venue.VenueName, Venue.VenueID, COUNT( Results.MembershipNo ) AS 'total', COUNT( DISTINCT Results.Date ) AS 'dates', CAST( COALESCE( COUNT( Results.MembershipNo ) / COUNT( DISTINCT Results.Date ) ) AS DECIMAL( 10, 1 ) ) AS 'Average' FROM Position, Player, Results, Venue WHERE Player.MembershipNo = Results.MembershipNo AND Venue.active = 'Y' AND Results.Position = Position.Position AND Venue.VenueID = Results.VenueID AND Results.Date BETWEEN '2013-07-01' AND CURDATE( ) GROUP BY Venue.VenueName ORDER BY Average DESC Which gives this result: VenueNameVenueIDtotaldatesAverage The Globe (Weds)22141623.5The Globe (Weds)22 141 6 23.5The Tawny Owl 16 118 6 19.7The Globe (Mon) 30 110 6 18.3The Duke Of Wellington 28 90 6 15.0The Mother Redcap 26 57 4 14.3The Jolly Milliner 23 79 6 13.2The Old Red Lion 29 545 10.8The Enigma Tavern 24 65 6 10.8The Crown 11 62 6 10.3The Sportsman 31 18 2 9.0The Heath Inn 33 27 3 9.0The Nags Head 32 31 4 7.8 What I really need now is the total of the 'total' column and the total of the 'dates' column so I can divide 'total' by 'dates' to get an average. But everything I try fails, I've used the Sum() function but I can't get it to work properly. Any help would be really appreciated. Cheers Quote Link to comment Share on other sites More sharing options...
Solution Barrikor Posted August 13, 2013 Solution Share Posted August 13, 2013 (edited) Have a $totalOfTotals variable that you add to on each fetch. (Assuming that you need to also display the data at the same time) Edited August 13, 2013 by Barrikor Quote Link to comment Share on other sites More sharing options...
Zane Posted August 13, 2013 Share Posted August 13, 2013 (edited) Try GROUP BY Venue.VenueName WITH ROLLUP Edited August 13, 2013 by Zane Quote Link to comment Share on other sites More sharing options...
PHP_Idiot Posted August 13, 2013 Author Share Posted August 13, 2013 Try GROUP BY Venue.VenueName WITH ROLLUP I gave this a go but the results are wrong, as you can see from the table section the 'total' total is correct but the 'dates' total is still only adding the Distinct dates together, rather than the subtotal of each row. Is there a way around this? total dates Average 860 32 26.9 Quote Link to comment Share on other sites More sharing options...
PHP_Idiot Posted August 13, 2013 Author Share Posted August 13, 2013 (edited) Have a $totalOfTotals variable that you add to on each fetch. (Assuming that you need to also display the data at the same time) I'm not entirely sure what you mean by this or how I would implement it. I don't have time right now but I will play about with it later this afternoon. Edited August 13, 2013 by PHP_Idiot Quote Link to comment Share on other sites More sharing options...
PHP_Idiot Posted August 13, 2013 Author Share Posted August 13, 2013 Have a $totalOfTotals variable that you add to on each fetch. (Assuming that you need to also display the data at the same time) Had time to play about with it now, and this has worked: $totalPlayers=$totalPlayers+$r['total']; $totalDates=$totalDates+$r['COUNT( DISTINCT Results.Date )']; $AVE=ROUND($totalPlayers/$totalDates,1); I've now getting exactly the figures I should be, thanks a lot Barrikor Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.