SQL Puzzle - 为什么我不能加入这两个子查询?

时间:2011-08-15 15:28:37

标签: sql sql-server sql-server-2005

我坚持使用SQL查询。

考虑下表:

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

此表包含游戏回合的结果。现在,有时候你可以用手指对游戏做出回答,然后猜测时间为35或甚至0毫秒。这些答案扭曲了我的游戏结果,我想删除它们。

我想弄清楚猜测至少200毫秒长的平均猜测时间。因此,如果一个游戏有五轮,猜测为455,400,340,30,300。我想忽略30并平均剩下的四个值并获得374的平均猜测时间。不降低30的平均猜测时间将是305.

我的问题是我正在尝试加入两个子查询,并且我收到一条错误消息,指出“on”语句周围存在问题。我认为允许加入子查询。

 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

3 个答案:

答案 0 :(得分:5)

因为你错过了一个结尾)(倒数第二行)

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 ) vtIII
 on vtIII.gameid = vt.gameid

答案 1 :(得分:4)

您尚未关闭所有子查询:

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 ) f
 on f.gameid = vt.gameid

我添加了这个:) vtIII ) f

答案 2 :(得分:1)

计算括号。

inner join  (

永远不会关闭。