Create Table TEST
(Type Char(1),
 State datetime)
Insert TEST
Select 'A','2006-5-10' Union All
Select 'A','2006-5-1' Union All
Select 'A','2006-5-9' Union All
Select 'B','2006-5-1' Union All
Select 'B','2006-5-5' Union All
Select 'B','2006-5-1' Union All
Select 'C','2006-5-5' Union All
Select 'C','2006-5-9' 
--select * from test
GO
--State不固定的情況下的
Declare @S Varchar(8000)
Select @S=''
Select @S=@S+',SUM(Case State When '''+convert(varchar(10),State,120)+ '''Then 1 Else 0 End) As ' + '[' + convert(varchar(10),State,120) + ']'
From TEST Group By State Order By State --这里的分组我想以 convert(varchar(10),state,120)的方式,可是在select 中如何写Select @S='Select Type'+@S+' From TEST Group By Type'
EXEC(@S)
Drop Table TEST
GO----------------------------
我想按state字段的convert(varchar(10),state,120)来分组Select @S=@S+',SUM(Case State When '''+convert(varchar(10),State,120)+ '''Then 1 Else 0 End) As ' + '[' + convert(varchar(10),State,120) + ']'From TEST Group By convert(varchar(10),state,120) Order By convert(varchar(10),state,120)如果改成这样之后,则在select @s=@s+...中总提示State不包含在group by 中在select 中如何写才能把 convert(varchar(10),State,120) 整个当成一个字段 而不是认为state是一个字段呢

解决方案 »

  1.   

    日期分组按天
    select type,convert(char(10),state,120) from table
    group by type,convert(char(10),state,120)
      

  2.   

    Create Table TEST
    (Type Char(1),
     State datetime)
    Insert TEST
    Select 'A','2006-5-10' Union All
    Select 'A','2006-5-1' Union All
    Select 'A','2006-5-9' Union All
    Select 'B','2006-5-1' Union All
    Select 'B','2006-5-5' Union All
    Select 'B','2006-5-1' Union All
    Select 'C','2006-5-5' Union All
    Select 'C','2006-5-9' 
    --select * from test
    GO
    --State不固定的情況下的
    Declare @S Varchar(8000)
    Select @S=''
    Select @S=@S+',SUM(Case Convert(Varchar(10),state,120) When '''+state+ '''Then 1 Else 0 End) As ' + '[' + state + ']'
    From (Select Distinct Convert(Varchar(10),state,120) As state From TEST ) A Order By stateSelect @S='Select Type'+@S+' From TEST Group By Type'
    EXEC(@S)
    Drop Table TEST
    GO
      

  3.   

    Create Table TEST
    (Type Char(1),
     State datetime)
    Insert TEST
    Select 'A','2006-5-10 00:01:01' Union All
    Select 'A','2006-5-1 00:05:01' Union All
    Select 'A','2006-5-9 00:06:01' Union All
    Select 'B','2006-5-1 00:03:01' Union All
    Select 'B','2006-5-5 00:08:01' Union All
    Select 'B','2006-5-1 00:09:01' Union All
    Select 'C','2006-5-5 00:11:01' Union All
    Select 'C','2006-5-9 00:02:01' 
    --select * from test
    GO
    --State不固定的情況下的
    Declare @S Varchar(8000)
    Select @S=''
    Select @S=@S+',SUM(Case Convert(Varchar(10),state,120) When '''+state+ '''Then 1 Else 0 End) As ' + '[' + state + ']'
    From (Select Distinct Convert(Varchar(10),state,120) As state From TEST ) A Order By stateSelect @S='Select Type'+@S+' From TEST Group By Type'
    EXEC(@S)
    Drop Table TEST
    GO
      

  4.   

    谢谢
    我想把EXEC(@S)的结果插入临时表#T中怎么处理我在
    Select @S='Select Type'+@S+' From TEST Group By Type'中加 into #T
    如:Select @S='Select Type'+@S+' into #T From TEST Group By Type'这样的话运行EXEC(@S)的话 只提示影响的行数,并没有插入到#T中可是如果把@S print出来后运行 就可以 郁闷
      

  5.   

    这样试试(把要操作的SQL继续写到汇总语句后面):
    Select @S='Select Type'+@S+' into #T From TEST Group By Type
    select * from #T         /*显示汇总结果*/
    select sum([2006-05-01]) from #T'      /*对汇总列求和*/
      

  6.   

    问题是这样,现在没有办法把数据插入临时表 #T中如:Select @S='Select Type'+@S+' into #T From TEST Group By Type'这样的话运行EXEC(@S)的话 只提示影响的行数,并没有插入到#T中可是如果把@S print出来后运行 就可以
      

  7.   

    to hellowork(一两清风) ( ) 
    我已经在EXEC(@S)的后面继续写了 select * from #T
    但是提示没有#T表,实际上就是没有插入成功
      

  8.   

    在EXEC內部生成的本地臨時表只在EXEC內部有效,如果想在外部還有效,用全局臨時表吧。
    Create Table TEST
    (Type Char(1),
     State datetime)
    Insert TEST
    Select 'A','2006-5-10 00:01:01' Union All
    Select 'A','2006-5-1 00:05:01' Union All
    Select 'A','2006-5-9 00:06:01' Union All
    Select 'B','2006-5-1 00:03:01' Union All
    Select 'B','2006-5-5 00:08:01' Union All
    Select 'B','2006-5-1 00:09:01' Union All
    Select 'C','2006-5-5 00:11:01' Union All
    Select 'C','2006-5-9 00:02:01' 
    --select * from test
    GO
    --State不固定的情況下的
    If OBJECT_ID('Tempdb.dbo.##T') Is Not Null
    Drop Table ##T
    Declare @S Varchar(8000)
    Select @S=''
    Select @S=@S+',SUM(Case Convert(Varchar(10),state,120) When '''+state+ '''Then 1 Else 0 End) As ' + '[' + state + ']'
    From (Select Distinct Convert(Varchar(10),state,120) As state From TEST ) A Order By stateSelect @S='Select Type'+@S+'  Into ##T From TEST Group By Type'
    EXEC(@S)
    Select * From ##T
    Drop Table TEST
    GO