I’m stuck on a SQL query.
Consider the following table:
Table DG_GAME_ROUNDS
RoundId int
GameId int
RoundNumber int
Value varchar(20)
Guess varchar(20)
Answer varchar(20)
Correct bit
Minutes int
Seconds int
Milliseconds int
This table holds the results of game rounds. Now sometimes you can fat finger an answer to the game and wind up with a guess time of 35 or even 0 milliseconds. These answers skew the results of my game and I want to remove them.
I want to figure out the average guess time where the guess is at least 200 milliseconds long. So if a game had five rounds with guesses of 455, 400, 340, 30, 300. I want to ignore the 30 and average out the remaining four values and get an average guess time of 374. Without dropping the 30 the average guess time would be 305.
My problem is that I’m trying to join two subqueries and I’m getting an error message that there is a problem around the “on” statement. I think joining subqueries is allowed.
select vt.gameid, vt.totalms, vt.numofguesses, vt.correctguesses
from
(select gr.gameid
, sum((gr.seconds*1000) + gr.milliseconds) as totalms
, count(gr.roundid) as numofguesses
, sum(cast(gr.correct as int)) as correctguesses
from work_tables.dbo.dg_game_rounds gr (nolock)
group by gr.gameid
) vt
inner join (
select vtIII.gameid, vtIII.avgtime
from
(
select vtII.gameid, sum(vtII.avgms)/count(vtII.avgms) as avgtime
from (
select gr.gameid, gr.seconds * 1000 + gr.milliseconds as avgms
from dg_game_rounds gr (nolock)
where gr.seconds * 1000 + gr.milliseconds > 200
) vtII
group by vtII.gameid
) vtIII
on vtIII.gameid = vt.gameid
Because you’re missing an ending ) (2nd to last line)