我有一个体育网站。它生成一个动态查询,根据结果行是否存在以及两个日期之间的时间戳来计算点。
这是实际查询,请原谅其冗长。
SELECT sum(totalpoints),
playerid
FROM fch_consolidated_results
WHERE datestamp > date_add(now(), INTERVAL -30 day)
AND season = "2016-2017"
AND ((
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-01 15:55:56'
AND datestamp < '2016-10-08 17:34:19')
OR (
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-15 12:49:50'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-19 14:10:31'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "AraNazarianNewHampshire"
AND datestamp > '2016-10-01 15:54:53'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "AraNazarianNewHampshire"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "BoboCarpenterBostonUniversity"
AND datestamp > '2016-10-01 15:54:21'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "BoboCarpenterBostonUniversity"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "BrandonHickeyBostonUniversity"
AND datestamp > '2016-10-01 15:55:46'
AND datestamp < '2016-10-04 08:54:18')
OR (
playerid = "BrandonHickeyBostonUniversity"
AND datestamp > '2016-10-08 17:34:19'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "CallumFryerUMassachusetts"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-01 15:53:45'
AND datestamp < '2016-10-04 12:00:31')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-07 11:11:33'
AND datestamp < '2016-10-15 12:49:50')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "ConnorLaCouveeBostonUniversity"
AND datestamp > '2016-10-01 15:56:40'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-01 15:57:37'
AND datestamp < '2016-10-12 09:57:32')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-15 12:49:50'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "DylanMalmquistNotreDame"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "DylanMalmquistNotreDame"
AND datestamp > '2016-10-19 14:10:30'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JakeKamrassMassachusettsLowell"
AND datestamp > '2016-10-01 15:55:08'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "JakeKamrassMassachusettsLowell"
AND datestamp > '2016-10-12 09:57:32'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "JakeOettingerBostonUniversity"
AND datestamp > '2016-10-01 15:56:27'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JasonSalvaggioNewHampshire"
AND datestamp > '2016-10-04 12:00:00'
AND datestamp < '2016-10-07 11:11:33')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-04 12:02:59'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JohnEdwardhMassachusettsLowell"
AND datestamp > '2016-10-04 08:54:17'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "JordanGrossNotreDame"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "KevinIrwinVermont"
AND datestamp > '2016-10-04 08:54:17'
AND datestamp < '2016-10-04 11:59:11')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-01 15:55:29'
AND datestamp < '2016-10-04 08:54:18')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "PatrickHarperBostonUniversity"
AND datestamp > '2016-10-01 15:54:40'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "PatrickHarperBostonUniversity"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-01 15:56:12'
AND datestamp < '2016-10-15 12:49:50')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-19 14:10:31'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59'))
GROUP BY playerid
ORDER BY sum(totalpoints) DESC
问题是JakeOettingerBostonUniversity
的点数总和应为22.此查询返回21. 请注意JakeOettingerBostonUniversity
仅在其他可能多次出现的情况下发生一次。
Source Data From fch_consolidated_results
当我开始删除部分查询作为调试策略时,让我更加困惑的是什么。我发现如果删除底部5 OR
子句,它将返回正确的数字。
SELECT sum(totalpoints),
playerid
FROM fch_consolidated_results
WHERE datestamp > date_add(now(), INTERVAL -30 day)
AND season = "2016-2017"
AND ((
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-01 15:55:56'
AND datestamp < '2016-10-08 17:34:19')
OR (
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-15 12:49:50'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "AnthonyFlorentinoProvidence"
AND datestamp > '2016-10-19 14:10:31'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "AraNazarianNewHampshire"
AND datestamp > '2016-10-01 15:54:53'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "AraNazarianNewHampshire"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "BoboCarpenterBostonUniversity"
AND datestamp > '2016-10-01 15:54:21'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "BoboCarpenterBostonUniversity"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "BrandonHickeyBostonUniversity"
AND datestamp > '2016-10-01 15:55:46'
AND datestamp < '2016-10-04 08:54:18')
OR (
playerid = "BrandonHickeyBostonUniversity"
AND datestamp > '2016-10-08 17:34:19'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "CallumFryerUMassachusetts"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-01 15:53:45'
AND datestamp < '2016-10-04 12:00:31')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-07 11:11:33'
AND datestamp < '2016-10-15 12:49:50')
OR (
playerid = "ColinWhiteBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "ConnorLaCouveeBostonUniversity"
AND datestamp > '2016-10-01 15:56:40'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-01 15:57:37'
AND datestamp < '2016-10-12 09:57:32')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-15 12:49:50'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "CoreyRonanConnecticut"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "DylanMalmquistNotreDame"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "DylanMalmquistNotreDame"
AND datestamp > '2016-10-19 14:10:30'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JakeKamrassMassachusettsLowell"
AND datestamp > '2016-10-01 15:55:08'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "JakeKamrassMassachusettsLowell"
AND datestamp > '2016-10-12 09:57:32'
AND datestamp < '2016-10-16 11:25:34')
OR (
playerid = "JakeOettingerBostonUniversity"
AND datestamp > '2016-10-01 15:56:27'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JasonSalvaggioNewHampshire"
AND datestamp > '2016-10-04 12:00:00'
AND datestamp < '2016-10-07 11:11:33')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-04 12:02:59'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "JDDudekBostonCollege"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "JohnEdwardhMassachusettsLowell"
AND datestamp > '2016-10-04 08:54:17'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "JordanGrossNotreDame"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "KevinIrwinVermont"
AND datestamp > '2016-10-04 08:54:17'
AND datestamp < '2016-10-04 11:59:11')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-01 15:55:29'
AND datestamp < '2016-10-04 08:54:18')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "MatiasClelandNewHampshire"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2016-10-23 12:27:46')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-04 08:54:18'
AND datestamp < '2016-10-08 11:55:23')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-16 11:25:34'
AND datestamp < '2016-10-19 14:10:30')
OR (
playerid = "MichaelKimBostonCollege"
AND datestamp > '2016-10-22 10:30:04'
AND datestamp < '2099-12-31 23:59:59')
/* BEGIN REMOVED CLAUSES
OR (
playerid = "PatrickHarperBostonUniversity"
AND datestamp > '2016-10-01 15:54:40'
AND datestamp < '2016-10-04 08:54:17')
OR (
playerid = "PatrickHarperBostonUniversity"
AND datestamp > '2016-10-08 11:55:23'
AND datestamp < '2099-12-31 23:59:59')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-01 15:56:12'
AND datestamp < '2016-10-15 12:49:50')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-19 14:10:31'
AND datestamp < '2016-10-22 10:30:04')
OR (
playerid = "WilliamLagessonUMassachusetts"
AND datestamp > '2016-10-23 12:27:46'
AND datestamp < '2099-12-31 23:59:59')
*/
)
GROUP BY playerid
ORDER BY sum(totalpoints) DESC
删除一些条款会给出不同的数字。
JakeOettingerBostonUniversity
仍然有他的一个条款,但我得到了不同的金额。JohnEdwardhMassachusettsLowell
未被删除作为评论的一部分,但他也得到了不同的太阳。Actual Results From Each Query
我想知道我的问题是否与GROUP BY有关,这导致了这一点,以及为什么不相关记录的存在(或不存在)会影响我的总和。
这些示例在2016-10-24运行,确保顶部的30天过滤器不是一个因素。
提前感谢您的帮助。