问一个简单的问题
下面是以前,paoluo(一天到晚游泳的鱼)给我的一个答案,很好用,再次感谢
Create Table TEST
(Type Char(1),
State Char(1))
Insert TEST
Select 'A','a' Union All
Select 'A','b' Union All
Select 'A','b' Union All
Select 'B','a' Union All
Select 'B','a' Union All
Select 'B','b' Union All
Select 'C','a' Union All
Select 'C','b'
GO
--State不固定的情況下的
Declare @S Varchar(8000)
Select @S=''
Select @S=@S+',SUM(Case State When '''+State+'''Then 1 Else 0 End) As '+State
From TEST Group By State Order By State
Select @S='Select Type'+@S+' From TEST Group By Type'
EXEC(@S)
GO
Drop Table TEST
GO
--Resilt
/*
Type a b
A 1 2
B 2 1
C 1 1
*/
现在的问题是,现在我把State 改为datetime类型,就不知道该怎么对下面这句话进行赋值了
Select @S=@S+',SUM(Case State When '''+State+'''Then 1 Else 0 End) As '+State
From TEST Group By State Order By State
总是提示:字符转datetime类型时出错
改为这样后也不行,晕了
Select @S=@S+',SUM(Case State When cast('''+State+''' as datetime) Then 1 Else 0 End) As '+State
From TEST Group By State Order By State