我可以在where条件中使用select out变量吗?

时间:2014-10-07 05:42:08

标签: sql-server

请查看我的sql语句:

declare @rptID as int
declare @lastRptID as int

select  @rptID = 238507
select top 1 @lastRptID = reportid from MeraRpt  where reportid < @rptid order by reportid desc

 SELECT
           @rptid
           ,[BelongTo] as Customer  
           ,[Prefix] as Prefix
           ,sum([ASRSuccessCalls])
          , (select sum(ASRSuccessCalls) from [V_MeraRpt_Detail_ByPrefix] a 
            where a.[BelongTo] = Customer and a.[Prefix] = Prefix
            and a.[reportID] in (@rptID,@lastRptID))

      from [V_MeraRpt_Detail_ByPrefix]
      where reportid = @rptid and Customer in (select custid from v_MostUsedCustomer )
      group by [BelongTo],[Prefix]

这个sql可以通过编译运行,但是它给出了错误的结果,问题就是这句话:

(select sum(ASRSuccessCalls) from [V_MeraRpt_Detail_ByPrefix] a 
                where a.[BelongTo] = Customer and a.[Prefix] = Prefix
                and a.[reportID] in (@rptID,@lastRptID))

结果是这种形式:

238507  Umobile 23480   1   4294
238507  Umobile 234803  0   4294
238507  Umobile 62811   0   4294
238507  01.2136 62812   10  4294
238507  Umobile 62812   18  4294
238507  01.2136 62813   15  4294
238507  Umobile 62813   17  4294

请查看最后一栏,它是完全相同的值。但我确信正确的结果应该根据不同的客户和前缀不同,我认为哪里的情况发生了一些错误, 任何人都可以告诉我如何解决它?谢谢。

0 个答案:

没有答案
相关问题