1

On the surface, the top n per group query is a fairly common one and I have used a few variations on other projects. However, I really cannot get my head around this one and I think it is due to the number of joins.

My query (at the bottom of the post) is trying to find the highest 3 scoring players per week. The score field is PPP.

So, in theory, I am looking at grouping by week, then by player (to get the sum of his scores), find the 3 players with the highest PPP per week and them sort them by PPP desc along with, ideally, giving them a rank number.

I know I would be able to do something that works with php but as I am sure doing it all in a query would be more server-friendly, I am determined to get this right.! The query below is not the only attempt I have had but it is the closest I have got to it working. The most often found solution LEFT OUTER JOIN table t2 ON (t1.id = t2.id AND t1.date < t2.date) did not come anywhere near working.

The parts of the output are correct are the grouping by YearWeek and User and the sum of that users PPP. It also does correctly select 3 users from each week (as long as there are 3 available) but not the highest scoring 3 and not in correct rank order.

So, in my head, I feel like I am close to getting it right but obviously may be a mile away! I have attached an SQL dump of the tables and data in case they will help.

Thanks in advance for any suggestions.
Steve

THE QUERY

Select
x.PLYR, x.PPP, x.WST, x.WND, x.rank, x.YWK
From
(Select
U.username PLYR,
Sum(Case When P.home = G.hgoals And P.away = G.agoals Then 3
When (P.home > P.away And G.hgoals > G.agoals) Or
(P.home < P.away And G.hgoals < G.agoals) Or
(P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
Else 0 End) PTS,
Sum(Case
When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
And G.agoals Is Not Null Then 1 Else 0 End) TOT,
Sum(Case When P.home = G.hgoals And P.away = G.agoals Then 3
When (P.home > P.away And G.hgoals > G.agoals) Or
(P.home < P.away And G.hgoals < G.agoals) Or
(P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
Else 0 End) / Sum(Case
When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
And G.agoals Is Not Null Then 1 Else 0 End) PPP,
Date_Add(G.date, Interval (0 - WeekDay(G.date)) Day) WST,
Date_Add(G.date, Interval (6 - WeekDay(G.date)) Day) WND,
Week(G.date) WK,
CASE
WHEN @currweek != YearWeek(G.date, 1) THEN @rank := 1
ELSE @rank := @rank + 1
END AS rank,
@currweek := YearWeek(G.date, 1) YWK
From
compuser CU Inner Join
comps C On C.compid = CU.comp Left Join
users U On U.userid = CU.user Inner Join
compgame CG On C.compid = CG.comp Inner Join
games G On G.gamesid = CG.game Left Join
predictions P On CU.user = P.user And CG.game = P.game
JOIN (SELECT @rank := NULL, @currweek := '') r
Where
G.date < Now()
Group By
YearWeek(G.date, 1), U.userid
Order By
YearWeek(G.date, 1), PPP Desc) x
Where
x.rank <= 3
Attachments
-- phpMyAdmin SQL Dump
-- version 3.4.11.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 18, 2013 at 07:00 PM
-- Server version: 5.5.23
-- PHP Version: 5.2.17

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `mfcsteve_preds`
--

-- --------------------------------------------------------

--
-- Table structure for table `compgame`
--

CREATE TABLE IF NOT EXISTS `compgame` (
  `compgameid` int(11) NOT NULL AUTO_INCREMENT,
  `comp` int(11) NOT NULL,
  `game` int(11) NOT NULL,
  PRIMARY KEY (`compgameid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1033 ;

--
-- Dumping data for table `compgame`
--

INSERT INTO `compgame` (`compgameid`, `comp`, `game`) VALUES
(54, 5, 41),
(53, 2, 40),
(52, 2, 39),
(51, 2, 38),
(280, 4, 283),
(279, 4, 282),
(277, 4, 280),
(15, 1, 2),
(278, 4, 279),
(11, 3, 4),
(466, 4, 473),
(16, 3, 8),
(17, 3, 9),
(18, 3, 10),
(19, 2, 10),
(20, 2, 11),
(21, 2, 12),
(22, 2, 13),
(23, 2, 14),
(24, 2, 15),
(25, 2, 16),
(26, 2, 17),
(27, 2, 18),
(28, 2, 19),
(29, 2, 20),
(30, 2, 21),
(31, 2, 22),
(32, 2, 23),
(33, 2, 24),
(34, 2, 25),
(35, 2, 26),
(36, 2, 27),
(37, 2, 28),
(38, 2, 29),
(39, 2, 30),
(40, 2, 31),
(41, 2, 32),
(42, 2, 33),
(43, 2, 34),
(44, 2, 35),
(45, 2, 36),
(46, 2, 37),
(47, 1, 34),
(55, 4, 94),
(49, 1, 35),
(50, 1, 36),
(56, 4, 93),
(57, 4, 92),
(58, 4, 91),
(59, 4, 90),
(60, 4, 89),
(61, 4, 95),
(62, 4, 96),
(63, 4, 97),
(64, 4, 98),
(108, 6, 95),
(66, 4, 100),
(67, 4, 101),
(68, 4, 102),
(69, 4, 103),
(70, 4, 104),
(71, 4, 105),
(72, 4, 106),
(73, 4, 107),
(74, 4, 108),
(75, 4, 109),
(76, 4, 110),
(77, 4, 111),
(78, 4, 112),
(79, 4, 113),
(80, 4, 114),
(81, 4, 115),
(82, 4, 116),
(83, 4, 117),
(84, 4, 118),
(85, 4, 119),
(86, 4, 120),
(87, 4, 121),
(88, 4, 122),
(89, 5, 88),
(90, 5, 87),
(91, 5, 86),
(92, 5, 85),
(93, 5, 84),
(94, 5, 83),
(95, 5, 82),
(96, 5, 81),
(97, 5, 80),
(98, 5, 79),
(99, 5, 76),
(100, 5, 75),
(101, 5, 74),
(102, 5, 73),
(103, 5, 72),
(104, 5, 71),
(105, 5, 70),
(106, 5, 69),
(107, 5, 68),
(109, 6, 107),
(110, 6, 119),
(111, 5, 124),
(112, 5, 123),
(113, 5, 125),
(114, 5, 126),
(115, 5, 127),
(116, 5, 128),
(117, 5, 129),
(118, 5, 130),
(119, 5, 131),
(120, 5, 132),
(121, 5, 133),
(122, 5, 134),
(123, 5, 135),
(124, 5, 136),
(125, 5, 137),
(126, 5, 138),
(127, 5, 139),
(128, 5, 140),
(129, 5, 141),
(130, 5, 142),
(131, 5, 143),
(132, 6, 144),
(133, 6, 162),
(134, 4, 144),
(135, 4, 145),
(136, 4, 146),
(137, 4, 147),
(138, 4, 148),
(139, 4, 149),
(140, 4, 150),
(141, 4, 151),
(142, 4, 152),
(143, 4, 153),
(144, 4, 154),
(145, 4, 155),
(146, 4, 156),
(147, 4, 157),
(148, 4, 158),
(149, 4, 159),
(150, 4, 160),
(151, 4, 161),
(152, 4, 162),
(153, 4, 163),
(154, 4, 164),
(155, 4, 165),
(156, 4, 166),
(157, 4, 167),
(158, 4, 168),
(159, 4, 169),
(160, 4, 170),
(161, 4, 171),
(162, 4, 172),
(163, 4, 173),
(164, 4, 174),
(165, 4, 175),
(166, 4, 176),
(167, 6, 173),
(168, 6, 177),
(169, 5, 178),
(170, 5, 179),
(171, 5, 180),
(172, 5, 181),
(173, 5, 182),
(174, 5, 183),
(175, 5, 184),
(176, 5, 185),
(177, 5, 186),
(178, 5, 187),
(179, 5, 188),
(180, 5, 189),
(181, 5, 190),
(182, 5, 191),
(183, 5, 192),
(184, 5, 193),
(185, 5, 194),
(186, 5, 195),
(187, 5, 196),
(188, 5, 197),
(189, 6, 199),
(190, 4, 198),
(191, 4, 199),
(192, 4, 200),
(193, 4, 201),
(478, 4, 474),
(477, 4, 462),
(196, 4, 204),
(197, 4, 205),
(198, 4, 206),
(476, 4, 463),
(200, 4, 208),
(201, 6, 209),
(202, 6, 210),
(203, 6, 211),
(204, 6, 212),
(205, 5, 213),
(206, 5, 214),
(207, 5, 215),
(208, 5, 216),
(209, 5, 217),
(210, 5, 218),
(211, 5, 219),
(212, 5, 220),
(213, 5, 221),
(214, 5, 222),
(215, 5, 223),
(216, 5, 224),
(217, 5, 225),
(218, 5, 226),
(219, 5, 227),
(220, 5, 228),
(221, 5, 229),
(222, 5, 230),
(223, 5, 231),
(224, 5, 232),
(225, 6, 243),
(226, 4, 233),
(227, 4, 234),
(228, 4, 235),
(229, 4, 236),
(230, 4, 237),
(231, 4, 238),
(232, 4, 239),
(233, 4, 240),
(234, 4, 241),
(475, 4, 464),
(236, 4, 243),
(237, 4, 244),
(483, 4, 486),
(239, 4, 246),
(241, 6, 247),
(242, 4, 248),
(243, 4, 249),
(244, 4, 250),
(245, 4, 251),
(246, 4, 252),
(247, 4, 253),
(248, 4, 254),
(249, 4, 255),
(250, 4, 256),
(251, 4, 257),
(252, 4, 258),
(253, 6, 255),
(254, 4, 259),
(473, 4, 466),
(256, 4, 261),
(257, 4, 262),
(258, 4, 263),
(472, 4, 467),
(260, 4, 265),
(261, 4, 266),
(262, 4, 267),
(263, 4, 269),
(264, 6, 262),
(265, 4, 268),
(266, 4, 270),
(267, 4, 271),
(268, 4, 272),
(471, 4, 468),
(470, 4, 469),
(271, 4, 275),
(272, 6, 272),
(469, 4, 470),
(274, 4, 277),
(275, 6, 278),
(281, 4, 284),
(282, 4, 285),
(283, 4, 286),
(284, 4, 287),
(285, 4, 288),
(286, 4, 289),
(287, 4, 290),
(468, 4, 471),
(467, 4, 472),
(479, 4, 475),
(480, 4, 476),
(481, 4, 488),
(293, 4, 296),
(294, 4, 297),
(295, 4, 298),
(495, 4, 491),
(494, 4, 490),
(493, 4, 489),
(299, 4, 302),
(300, 4, 303),
(301, 4, 304),
(302, 4, 305),
(492, 4, 477),
(304, 6, 290),
(305, 6, 297),
(306, 5, 307),
(307, 5, 308),
(308, 5, 309),
(309, 5, 310),
(310, 5, 311),
(311, 5, 312),
(312, 5, 313),
(313, 5, 314),
(314, 5, 315),
(315, 5, 316),
(316, 5, 317),
(317, 5, 318),
(318, 5, 319),
(319, 5, 320),
(320, 5, 321),
(321, 5, 322),
(322, 5, 323),
(323, 5, 324),
(324, 5, 325),
(325, 5, 326),
(326, 5, 327),
(327, 5, 328),
(328, 5, 329),
(329, 5, 330),
(330, 5, 331),
(331, 5, 332),
(332, 5, 333),
(333, 5, 334),
(334, 5, 335),
(335, 5, 336),
(336, 4, 337),
(337, 4, 338),
(338, 4, 339),
(339, 4, 340),
(340, 4, 341),
(491, 4, 478),
(342, 4, 343),
(343, 4, 344),
(490, 4, 479),
(489, 4, 480),
(488, 4, 481),
(487, 4, 482),
(348, 4, 349),
(486, 4, 483),
(350, 4, 350),
(351, 4, 351),
(352, 4, 352),
(353, 4, 353),
(354, 4, 354),
(355, 4, 355),
(356, 4, 356),
(357, 4, 357),
(358, 4, 358),
(359, 4, 359),
(360, 4, 360),
(361, 5, 361),
(362, 5, 362),
(363, 5, 363),
(364, 5, 364),
(365, 5, 365),
(366, 5, 366),
(367, 5, 367),
(368, 5, 368),
(369, 5, 369),
(370, 5, 370),
(371, 5, 371),
(372, 5, 372),
(373, 5, 373),
(374, 5, 374),
(375, 5, 375),
(376, 5, 376),
(377, 5, 377),
(378, 5, 378),
(379, 5, 379),
(380, 5, 380),
(381, 6, 356),
(484, 4, 485),
(383, 5, 382),
(384, 5, 383),
(385, 5, 384),
(386, 5, 385),
(387, 5, 386),
(388, 5, 387),
(389, 5, 388),
(390, 5, 389),
(391, 5, 390),
(392, 5, 391),
(393, 5, 392),
(394, 5, 393),
(395, 5, 394),
(396, 5, 395),
(397, 5, 396),
(398, 5, 397),
(399, 5, 398),
(400, 5, 399),
(401, 5, 400),
(402, 5, 401),
(482, 4, 487),
(405, 4, 404),
(406, 4, 406),
(407, 4, 407),
(408, 4, 408),
(409, 4, 409),
(410, 4, 410),
(411, 4, 411),
(412, 4, 412),
(413, 4, 413),
(414, 4, 414),
(415, 4, 415),
(485, 4, 484),
(417, 66, 409),
(474, 4, 465),
(419, 6, 409),
(420, 5, 418),
(421, 5, 419),
(422, 5, 420),
(423, 5, 421),
(424, 5, 422),
(425, 5, 423),
(426, 5, 424),
(427, 5, 425),
(428, 5, 426),
(429, 5, 427),
(430, 5, 428),
(431, 5, 429),
(432, 5, 430),
(433, 5, 431),
(434, 5, 432),
(435, 5, 433),
(436, 5, 434),
(437, 5, 435),
(438, 5, 436),
(439, 5, 437),
(440, 4, 438),
(441, 6, 438),
(442, 6, 447),
(443, 6, 457),
(444, 4, 439),
(445, 4, 440),
(446, 4, 441),
(447, 4, 442),
(448, 4, 443),
(449, 4, 444),
(450, 4, 445),
(451, 4, 446),
(452, 4, 447),
(453, 4, 448),
(454, 4, 449),
(455, 4, 450),
(456, 4, 451),
(457, 4, 452),
(458, 4, 453),
(459, 4, 454),
(460, 4, 455),
(461, 4, 456),
(462, 4, 457),
(463, 4, 458),
(464, 4, 459),
(465, 4, 460),
(496, 4, 492),
(497, 4, 493),
(498, 4, 494),
(499, 4, 495),
(500, 4, 496),
(501, 4, 497),
(502, 4, 498),
(503, 4, 499),
(504, 4, 500),
(505, 4, 501),
(506, 4, 502),
(507, 4, 503),
(508, 4, 504),
(509, 4, 505),
(510, 4, 506),
(511, 4, 507),
(512, 4, 508),
(513, 4, 509),
(514, 4, 510),
(515, 4, 511),
(516, 4, 512),
(517, 4, 513),
(518, 4, 514),
(519, 4, 515),
(520, 4, 516),
(521, 4, 517),
(522, 4, 518),
(523, 4, 519),
(524, 4, 520),
(525, 4, 521),
(526, 4, 522),
(527, 4, 523),
(528, 4, 524),
(529, 4, 525),
(530, 4, 526),
(531, 4, 527),
(532, 4, 528),
(533, 4, 529),
(534, 4, 530),
(535, 4, 531),
(536, 4, 532),
(537, 4, 533),
(538, 4, 534),
(539, 4, 535),
(540, 4, 536),
(541, 4, 537),
(542, 4, 538),
(543, 4, 539),
(544, 4, 540),
(545, 4, 541),
(546, 4, 542),
(547, 4, 543),
(548, 4, 544),
(549, 4, 545),
(550, 4, 546),
(551, 4, 547),
(552, 4, 548),
(553, 4, 549),
(554, 4, 550),
(555, 4, 551),
(556, 4, 552),
(557, 4, 553),
(558, 4, 554),
(559, 4, 555),
(560, 4, 556),
(561, 4, 557),
(562, 4, 558),
(563, 4, 559),
(564, 4, 560),
(565, 4, 561),
(566, 4, 562),
(567, 4, 563),
(568, 4, 564),
(569, 4, 565),
(570, 4, 566),
(571, 4, 567),
(572, 4, 568),
(573, 4, 569),
(574, 4, 570),
(575, 4, 571),
(576, 4, 572),
(577, 4, 573),
(578, 4, 574),
(579, 4, 575),
(580, 4, 576),
(581, 4, 577),
(582, 4, 578),
(583, 4, 579),
(584, 4, 580),
(585, 4, 581),
(586, 4, 582),
(587, 4, 583),
(588, 4, 584),
(589, 4, 585),
(590, 4, 586),
(591, 4, 587),
(592, 4, 588),
(593, 4, 589),
(594, 4, 590),
(595, 4, 591),
(596, 4, 592),
(597, 4, 593),
(598, 4, 594),
(599, 4, 595),
(600, 4, 596),
(601, 4, 597),
(602, 4, 598),
(603, 4, 599),
(604, 4, 600),
(605, 4, 601),
(606, 4, 602),
(607, 4, 603),
(608, 4, 604),
(609, 4, 605),
(610, 4, 606),
(611, 4, 607),
(612, 4, 608),
(613, 4, 609),
(614, 4, 610),
(615, 4, 611),
(616, 4, 612),
(617, 4, 613),
(618, 4, 614),
(619, 4, 615),
(620, 4, 616),
(621, 4, 617),
(622, 4, 618),
(623, 4, 619),
(624, 4, 620),
(625, 4, 621),
(626, 4, 622),
(627, 4, 623),
(628, 4, 624),
(629, 4, 625),
(630, 4, 626),
(631, 4, 627),
(632, 4, 628),
(633, 4, 629),
(634, 4, 630),
(635, 4, 631),
(636, 4, 632),
(637, 4, 633),
(638, 4, 634),
(639, 4, 635),
(640, 4, 636),
(641, 4, 637),
(642, 4, 638),
(643, 4, 639),
(644, 4, 640),
(645, 4, 641),
(646, 4, 642),
(647, 4, 643),
(64
2
Contributors
5
Replies
6
Views
4 Years
Discussion Span
Last Post by MargateSteve
0

I'm having a look at it - looks like a challenge!

indexes were missing on these - dropped the query from 90 seconds to 6 seconds for me
CU.comp
CU.user
CG.comp
CG.game
P.user

2
SELECT compuserid,`PTS`,`TOT`,`PPP`,WST,WND,WK,
        CASE
        WHEN @currweek != YWK THEN @rank := 1
        ELSE @rank := @rank + 1
        END AS rank,
        @currweek := YWK AS t,@currweek AS tt
    FROM (SELECT compuserid,SUM(PTS) AS `PTS`,SUM(TOT) AS `TOT`,SUM(PTS)/SUM(TOT) AS `PPP`,WST,WND,WK,YWK
        FROM (SELECT CU.compuserid,
                (CASE WHEN P.home = G.hgoals AND P.away = G.agoals THEN 3
                WHEN (P.home > P.away AND G.hgoals > G.agoals) OR
                (P.home < P.away AND G.hgoals < G.agoals) OR
                (P.home <> G.hgoals AND G.hgoals = G.agoals AND P.home = P.away) THEN 1
                ELSE 0 END) PTS,
                (CASE WHEN P.home IS NOT NULL AND P.away IS NOT NULL AND G.hgoals IS NOT NULL
                AND G.agoals IS NOT NULL THEN 1 ELSE 0 END) TOT,
                (CASE WHEN P.home = G.hgoals AND P.away = G.agoals THEN 3
                WHEN (P.home > P.away AND G.hgoals > G.agoals) OR
                (P.home < P.away AND G.hgoals < G.agoals) OR
                (P.home <> G.hgoals AND G.hgoals = G.agoals AND P.home = P.away) THEN 1
                ELSE 0 END) / (CASE
                WHEN P.home IS NOT NULL AND P.away IS NOT NULL AND G.hgoals IS NOT NULL
                AND G.agoals IS NOT NULL THEN 1 ELSE 0 END) PPP,
                DATE_ADD(G.date, INTERVAL (0 - WEEKDAY(G.date)) DAY) WST,
                DATE_ADD(G.date, INTERVAL (6 - WEEKDAY(G.date)) DAY) WND,
                WEEK(G.date) WK,
                YEARWEEK(G.date, 1) YWK
            FROM compuser CU
            LEFT JOIN comps C ON c.compid = CU.comp
            LEFT JOIN users U ON U.userid = CU.user
            LEFT JOIN compgame CG ON C.compid = CG.comp
            LEFT JOIN games G ON G.gamesid = CG.game
            LEFT JOIN predictions P ON CU.user = P.user AND CG.game = P.game
            WHERE 
            G.date < NOW()
            ORDER BY G.date ASC
            ) AS `datatable` 
    GROUP BY YWK, compuserid
    ORDER BY YWK ASC, PPP DESC) AS `groupedtable`
;

Can i have a medal please?

Votes + Comments
LOL. Nice work!
0

You can't have the medal yet but it is certainly on order!!

It is very nearly there in so much as the grouping and sorting function is working spot on. The only bits missing are limiting each week to the top 3 members and the fact that the rank is showing as null (and a small typo in the table alias on line 28).

However, massive thanks for getting it this close. I have had a quick play to see if I can get the rank working but tired eyes (2.15am here) are not helping so will have another look in the morning.

Thanks again
Steve

0

Close but frustratingly still not there. I have succesfully got the query to rank the members in the correct order and all that is left is to limit it to the top 3 per week.

I have come close in the code below but it is giving a very strange outcome. Quite simply it only returns odd numbered ranks, so anything ranked 1 or 3. I tried changing it to <=5 and that returned ranks 1,3,5 so only odd ones again.

Also, that final 'WHERE' looks like it could be shortened, but trying groupedtable.RNK returned a 'column not found' error.

Select
  groupedtable.compuserid,
  groupedtable.PTS,
  groupedtable.TOT,
  groupedtable.PPP,
  groupedtable.WST,
  groupedtable.WND,
  groupedtable.WK,
  Case When @currweek != groupedtable.YWK Then @rank := 1
    Else @rank := @rank + 1 End As RNK,
  @currweek := groupedtable.YWK As t,
  @currweek As tt
From
  (Select
    datatable.compuserid,
    Sum(datatable.PTS) As PTS,
    Sum(datatable.TOT) As TOT,
    Sum(datatable.PTS) / Sum(datatable.TOT) As PPP,
    datatable.WST,
    datatable.WND,
    datatable.WK,
    datatable.YWK,
    datatable.DTE
  From
    (Select
      CU.compuserid,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) PTS,
      (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) TOT,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) / (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) PPP,
      Date_Add(G.date, Interval (0 - WeekDay(G.date)) Day) WST,
      Date_Add(G.date, Interval (6 - WeekDay(G.date)) Day) WND,
      Week(G.date) WK,
      G.date DTE,
      YearWeek(G.date, 1) YWK
    From
      compuser CU Left Join
      comps C On C.compid = CU.comp Left Join
      users U On U.userid = CU.user Left Join
      compgame CG On C.compid = CG.comp Left Join
      games G On G.gamesid = CG.game Left Join
      predictions P On CU.user = P.user And CG.game = P.game
    Where
      G.date < Now()  

    Order By
      G.date,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) / (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) Desc) As datatable

    Group By
    datatable.compuserid, YearWeek(datatable.DTE, 1)
  Order By
    datatable.YWK,
    Sum(datatable.PTS) / Sum(datatable.TOT) Desc) As groupedtable
    WHERE
      Case When @currweek != groupedtable.YWK Then @rank := 1
    Else @rank := @rank + 1 End <= 3
0

Still no joy, despite having played around with it all. Have replaced the last WHERE clause with 'HAVING' instead, but it still only returns odd numbered ransk (1,3,5).

Select
  groupedtable.compuserid,
  groupedtable.PTS,
  groupedtable.TOT,
  groupedtable.PPP,
  groupedtable.WST,
  groupedtable.WND,
  groupedtable.WK, 
  groupedtable.DTE,
  (Case When @currweek != YWK Then @rank := 1
    Else @rank := @rank + 1 End) RNK,
  @currweek := YWK As t,
  @currweek As tt
From
  (Select
    datatable.compuserid,
    Sum(datatable.PTS) As PTS,
    Sum(datatable.TOT) As TOT,
    Sum(datatable.PTS) / Sum(datatable.TOT) As PPP,
    datatable.WST,
    datatable.WND,
    datatable.WK,
    datatable.YWK,
    datatable.DTE
  From
    (Select
      CU.compuserid,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) PTS,
      (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) TOT,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) / (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) PPP,
      Date_Add(G.date, Interval (0 - WeekDay(G.date)) Day) WST,
      Date_Add(G.date, Interval (6 - WeekDay(G.date)) Day) WND,
      Week(G.date) WK,
      G.date DTE,
      YearWeek(G.date, 1) YWK
    From
      compuser CU Left Join
      comps C On C.compid = CU.comp Left Join
      users U On U.userid = CU.user Left Join
      compgame CG On C.compid = CG.comp Left Join
      games G On G.gamesid = CG.game Left Join
      predictions P On CU.user = P.user And CG.game = P.game
    Where
      G.date < Now()
    Order By
      G.date,
      (Case When P.home = G.hgoals And P.away = G.agoals Then 3
        When (P.home > P.away And G.hgoals > G.agoals) Or
        (P.home < P.away And G.hgoals < G.agoals) Or
        (P.home <> G.hgoals And G.hgoals = G.agoals And P.home = P.away) Then 1
        Else 0 End) / (Case
        When P.home Is Not Null And P.away Is Not Null And G.hgoals Is Not Null
        And G.agoals Is Not Null Then 1 Else 0 End) Desc) As datatable  
  Group By
    datatable.compuserid, YearWeek(datatable.DTE, 1)
  Order By
    datatable.YWK,
    Sum(datatable.PTS) / Sum(datatable.TOT) Desc) As groupedtable      
   HAVING RNK <=5
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.