为什么在子查询中进行分组会导致问题

时间:2009-10-16 10:39:24

标签: sql sybase-ase

当我在以下子查询中包含2个注释掉的行时,似乎需要一直持续到我的Sybase 12.5 ASE服务器获得任何结果。没有这两行,查询运行正常。这个分组有什么问题?

select days_played.day_played, count(distinct days_played.user_id) as OLD_users
from days_played inner join days_received
on days_played.day_played = days_received.day_received
and days_played.user_id = days_received.user_id
where days_received.min_bulk_MT > days_played.min_MO
and days_played.user_id in

(select sgia.user_id 
from days_played as sgia
where sgia.day_played < days_played.day_played
--group by sgia.user_id 
--having sum(sgia.B_first_msg) = 0
)

group by days_played.day_played

3 个答案:

答案 0 :(得分:0)

使用showplan显示解释,找出查询的作用。

在这种情况下,您是否可以通过将子查询作为主查询的一部分来消除子查询?

答案 1 :(得分:0)

您可以尝试按如下方式重写查询吗?

select days_played.day_played,
       count(distinct days_played.user_id) as OLD_users
  from days_played
 inner join days_received on days_played.day_played = days_received.day_received
                         and days_played.user_id = days_received.user_id
 where days_received.min_bulk_MT > days_played.min_MO
   and 0 = (select sum(sgia.B_first_msg)
              from days_played as sgia
             where sgia.user_id = days_played.user_id
               and sgia.day_played < days_played.day_played
            )
 group by days_played.day_played

我想这会给你带来更好的表现......

答案 2 :(得分:0)

好的,我发现了问题所在 我必须在子查询中包含用户ID:“其中days_played.user_id = sgia.user_id 和sgia.day_played&lt; days_played.day_played“