能不能写个循环,就实现下面的操作,谢谢。select comm_createdby,month(comm_createddate) as cmonth,
(case day(comm_createddate) when '1' then count(day(comm_createddate)) else 0 end) as '1' ,
(case day(comm_createddate) when '2' then count(day(comm_createddate)) else 0 end) as '2' ,
(case day(comm_createddate) when '3' then count(day(comm_createddate)) else 0 end) as '3' ,
(case day(comm_createddate) when '4' then count(day(comm_createddate)) else 0 end) as '4' ,
(case day(comm_createddate) when '5' then count(day(comm_createddate)) else 0 end) as '5' ,
(case day(comm_createddate) when '6' then count(day(comm_createddate)) else 0 end) as '6' ,
(case day(comm_createddate) when '7' then count(day(comm_createddate)) else 0 end) as '7' ,
(case day(comm_createddate) when '8' then count(day(comm_createddate)) else 0 end) as '8' ,
(case day(comm_createddate) when '9' then count(day(comm_createddate)) else 0 end) as '9' ,
(case day(comm_createddate) when '10' then count(day(comm_createddate)) else 0 end) as '10' ,
(case day(comm_createddate) when '11' then count(day(comm_createddate)) else 0 end) as '11' ,
(case day(comm_createddate) when '12' then count(day(comm_createddate)) else 0 end) as '12' ,
(case day(comm_createddate) when '13' then count(day(comm_createddate)) else 0 end) as '13' ,
(case day(comm_createddate) when '14' then count(day(comm_createddate)) else 0 end) as '14' ,
(case day(comm_createddate) when '15' then count(day(comm_createddate)) else 0 end) as '15' ,
(case day(comm_createddate) when '16' then count(day(comm_createddate)) else 0 end) as '16' ,
(case day(comm_createddate) when '17' then count(day(comm_createddate)) else 0 end) as '17' ,
(case day(comm_createddate) when '18' then count(day(comm_createddate)) else 0 end) as '18' ,
(case day(comm_createddate) when '19' then count(day(comm_createddate)) else 0 end) as '19' ,
(case day(comm_createddate) when '20' then count(day(comm_createddate)) else 0 end) as '20' ,
(case day(comm_createddate) when '21' then count(day(comm_createddate)) else 0 end) as '21' ,
(case day(comm_createddate) when '22' then count(day(comm_createddate)) else 0 end) as '22' ,
(case day(comm_createddate) when '23' then count(day(comm_createddate)) else 0 end) as '23' ,
(case day(comm_createddate) when '24' then count(day(comm_createddate)) else 0 end) as '24' ,
(case day(comm_createddate) when '25' then count(day(comm_createddate)) else 0 end) as '25' ,
(case day(comm_createddate) when '26' then count(day(comm_createddate)) else 0 end) as '26' ,
(case day(comm_createddate) when '27' then count(day(comm_createddate)) else 0 end) as '27' ,
(case day(comm_createddate) when '28' then count(day(comm_createddate)) else 0 end) as '28' ,
(case day(comm_createddate) when '29' then count(day(comm_createddate)) else 0 end) as '29' ,
(case day(comm_createddate) when '30' then count(day(comm_createddate)) else 0 end) as '30' from communication where comm_deleted is null 
group  by day(comm_createddate),month(comm_createddate),comm_createdby 

解决方案 »

  1.   

    declare @s varchar(8000)
    declare @i int
    set @i=1
    set @s='select comm_createdby,month(comm_createddate) as cmonth '
    while @i<31
    begin
      set @s=@s+',(case day(comm_createddate) when '+cast(@i as varchar(10))+' then count(day(comm_createddate)) else 0 end) as '+cast(@i as varchar(10))+''+char(10)
      set @i=@i+1
    end
    exec(@s+' from communication where comm_deleted is null group  by day(comm_createddate),month(comm_createddate),comm_createdby')
      

  2.   


    在sql2000运行一下,报错"服务器: 消息 170,级别 15,状态 1,行 1
                          第 1 行: '1' 附近有语法错误。"能帮我看一下吗?
      

  3.   


    declare @sql varchar(max)
    set @sql='select comm_createdby,month(comm_createddate) as cmonth, '
    declare @n int
    set @n=1
    while @n<=31 
    begin
     set @sql=@sql+'(case day(comm_createddate) when  ''' +cast(@n as varchar(2))+''' then count(day(comm_createddate)) else 0 end) as '''+cast(@n as varchar(2))+''',' 
     set @n=@n+1
    end
    set @sql=@sql+'  from communication where comm_deleted is null group  by day(comm_createddate),month(comm_createddate),comm_createdby '
    ---print(@sql)
    exec(@sql)
      

  4.   

    我是把你的代码COPY下来,在SQL2000下运行的。
    前面这些没有错误:
    declare @s varchar(8000)
    declare @i int
    set @i=1
    set @s='select comm_createdby,month(comm_createddate) as cmonth '
    while @i<31
    begin
      set @s=@s+',(case day(comm_createddate) when '+cast(@i as varchar(10))+' then count(day(comm_createddate)) else 0 end) as '+cast(@i as varchar(10))+''+char(10)
      set @i=@i+1
    end
    就是最后执行"exec(@s+' from communication where comm_deleted is null group  by day(comm_createddate),month(comm_createddate),comm_createdby')
    "的时候,就报错了:"服务器: 消息 170,级别 15,状态 1,行 1
                      第 1 行: '1' 附近有语法错误。"
      

  5.   

    你的也不行,报错:服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: 'max' 附近有语法错误。
    服务器: 消息 137,级别 15,状态 1,行 3
    必须声明变量 '@sql'。
    服务器: 消息 137,级别 15,状态 1,行 7
    必须声明变量 '@sql'。
    服务器: 消息 137,级别 15,状态 1,行 10
    必须声明变量 '@sql'。
    服务器: 消息 137,级别 15,状态 1,行 12
    必须声明变量 '@sql'。
      

  6.   

    我试把max 改为 8000 ;也报错:服务器: 消息 156,级别 15,状态 1,行 1
                               在关键字 'from' 附近有语法错误。谢谢
      

  7.   


    给数字加上引号???数字是int类型的
      

  8.   


    declare @s varchar(8000)
    declare @i int
    set @i=1
    set @s='select comm_createdby,month(comm_createddate) as cmonth '
    while @i<31
    begin
      set @s=@s+',(case day(comm_createddate) when '+cast(@i as varchar(10))+' then count(day(comm_createddate)) else 0 end) as '''+cast(@i as varchar(10))+''''+char(10)
      set @i=@i+1
    end
    exec(@s+'from communication where comm_deleted is null group  by day(comm_createddate),month(comm_createddate),comm_createdby')
    试试?
      

  9.   


    declare @sql varchar(8000)
    set @sql='select comm_createdby,month(comm_createddate) as cmonth, '
    declare @n int
    set @n=1
    while @n<=31 
    begin
     set @sql=@sql+'(case day(comm_createddate) when  ''' +cast(@n as varchar(2))+''' then count(day(comm_createddate)) else 0 end) as '''+cast(@n as varchar(2))+''',' 
     set @n=@n+1
    end
    set @sql=left(@sql,len(@sql)-1)----取from前面的逗号
    set @sql=@sql+'  from communication where comm_deleted is null group  by day(comm_createddate),month(comm_createddate),comm_createdby '
    ---print(@sql)
    exec(@sql)