http://topic.csdn.net/u/20071218/21/9941d141-55b5-4711-88c8-ffad269f0869.html
问题如上.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER PROCEDURE [dbo].[QueryCount]
(
@cust_id varchar(20) 
)
AS
select isnull(TianShu,'f') TianShu ,
  sum(case when flag is null or flag='' then 1 else 0 end) 'c1',--未处理
  sum(case when flag ='0' or flag='1' then 1 else 0 end) 'c2',--正在处理中
  --sum(case flag when 2 then 1 else 0 end) 'c3',--已回复
  sum(case isnull(flag,3) when 3 then 1 else 0 end) 'c4'--无效
from
(
  select input_time,flag,isnull(a.TianShu,b.TianShu) TianShu from
  (select input_time , flag , TianShu = 
    case 
        when datediff(day,input_time,getdate()) between 0 and 7 then 'a'
        when datediff(day,input_time,getdate()) between 8 and 15 then 'b'
        when datediff(day,input_time,getdate()) between 16 and 30 then 'c'
        when datediff(day,input_time,getdate()) between 31 and 90 then 'd'
        when datediff(day,input_time,getdate()) > 90 then 'e'
    end
  from letter_files where input_time is not null and cust_id=rtrim(@cust_id)) a --这里的cust_id=rtrim(@cust_id)不起筛选作用
  right join 
  (select 'a' as TianShu union all select 'b' union all 
   select 'c' union all  select 'd' union all select 'e') b
  on a.TianShu = b.TianShu
) t
 group by TianShu with rollup

解决方案 »

  1.   

    忘了提我的问题了
    我是想利用该表的另一个字段 cust_id 等于存储过程的 @cust_id --传入参数
    作条件筛选,不知道为什么加了不起作用,究竟加在哪个地方或如何修改?exec QueryCount 'xxx' 无论xxx为任何值依然把表的所有数据都统计了.奇怪
      

  2.   


    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    ALTER PROCEDURE [dbo].[QueryCount]
    (
    @cust_id varchar(20) 
    )
    AS
    select isnull(TianShu,'f') TianShu ,
      sum(case when flag is null or flag='' then 1 else 0 end) 'c1',--未处理
      sum(case when flag ='0' or flag='1' then 1 else 0 end) 'c2',--正在处理中
      --sum(case flag when 2 then 1 else 0 end) 'c3',--已回复
      sum(case isnull(flag,3) when 3 then 1 else 0 end) 'c4'--无效
    from
    (
      select input_time,flag,isnull(a.TianShu,b.TianShu) TianShu from
      (select input_time , flag , TianShu = 
        case 
            when datediff(day,input_time,getdate()) between 0 and 7 then 'a'
            when datediff(day,input_time,getdate()) between 8 and 15 then 'b'
            when datediff(day,input_time,getdate()) between 16 and 30 then 'c'
            when datediff(day,input_time,getdate()) between 31 and 90 then 'd'
            when datediff(day,input_time,getdate()) > 90 then 'e'
        end
      from letter_files where input_time is not null ) a --这里的cust_id=rtrim(@cust_id)不起筛选作用
      right join 
      (select 'a' as TianShu union all select 'b' union all 
       select 'c' union all  select 'd' union all select 'e') b
      on a.TianShu = b.TianShu
      where cust_id=rtrim(@cust_id) --挪到这里的试试
    ) t
     group by TianShu with rollup
      

  3.   

    消息 207,级别 16,状态 1,过程 QueryCount,第 27 行
    列名 'cust_id' 无效。
      

  4.   

    消息   207,级别   16,状态   1,过程   QueryCount,第   27   行 
    列名   'cust_id'   无效。
      

  5.   


    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    ALTER PROCEDURE [dbo].[QueryCount]
    (
        @cust_id varchar(20) 
    )
    AS
    select isnull(TianShu,'f') TianShu ,
      sum(case when flag is null or flag='' then 1 else 0 end) 'c1',--未处理
      sum(case when flag ='0' or flag='1' then 1 else 0 end) 'c2',--正在处理中
      --sum(case flag when 2 then 1 else 0 end) 'c3',--已回复
      sum(case isnull(flag,3) when 3 then 1 else 0 end) 'c4'--无效
    from
    (
      select input_time,flag,isnull(a.TianShu,b.TianShu) TianShu , cust_id from
      (select input_time , flag , TianShu = 
        case 
            when datediff(day,input_time,getdate()) between 0 and 7 then 'a'
            when datediff(day,input_time,getdate()) between 8 and 15 then 'b'
            when datediff(day,input_time,getdate()) between 16 and 30 then 'c'
            when datediff(day,input_time,getdate()) between 31 and 90 then 'd'
            when datediff(day,input_time,getdate()) > 90 then 'e'
        end , cust_id
      from letter_files where input_time is not null ) a --这里的cust_id=rtrim(@cust_id)不起筛选作用
      right join 
      (select 'a' as TianShu union all select 'b' union all 
       select 'c' union all  select 'd' union all select 'e') b
      on a.TianShu = b.TianShu
      where cust_id=rtrim(@cust_id) --挪到这里的试试
    ) t
     group by TianShu with rollup
      

  6.   

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    goALTER PROCEDURE [dbo].[QueryCount]
    (
    @cust_id varchar(20) 
    )
    AS
    select isnull(TianShu,'f') TianShu ,
      sum(case when flag is null or flag='' then 1 else 0 end) 'c1',--未处理
      sum(case when flag ='0' or flag='1' then 1 else 0 end) 'c2',--正在处理中
      --sum(case flag when 2 then 1 else 0 end) 'c3',--已回复
      sum(case isnull(flag,3) when 3 then 1 else 0 end) 'c4'--无效
    from
    (
      select input_time,flag,isnull(a.TianShu,b.TianShu) TianShu from
      (select input_time , flag , TianShu = 
        case 
            when datediff(day,input_time,getdate()) between 0 and 7 then 'a'
            when datediff(day,input_time,getdate()) between 8 and 15 then 'b'
            when datediff(day,input_time,getdate()) between 16 and 30 then 'c'
            when datediff(day,input_time,getdate()) between 31 and 90 then 'd'
            when datediff(day,input_time,getdate()) > 90 then 'e'
        end
      from letter_files where input_time is not null) a 
      right join 
      (select 'a' as TianShu union all select 'b' union all 
       select 'c' union all  select 'd' union all select 'e') b
      on a.TianShu = b.TianShu
      where a.cust_id=rtrim(@cust_id) --放这里
    ) t
     group by TianShu with rollup
      

  7.   

    To:潇洒老乌龟消息 207,级别 16,状态 1,过程 QueryCount,第 28 行
    列名 'cust_id' 无效。
      

  8.   

    作条件筛选,不知道为什么加了不起作用,究竟加在哪个地方或如何修改? 
    ---------
    right join--表连接这里