问一个简单的问题

kdg2000 2006-08-11 10:17:30
下面是以前,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
...全文
96 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyxfly 2006-08-11
  • 打赏
  • 举报
回复
^_^
tianyajinhui 2006-08-11
  • 打赏
  • 举报
回复
1不可能转换为datetiem的类型啊
九斤半 2006-08-11
  • 打赏
  • 举报
回复
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
Select @S='Select Type'+@S+' From TEST Group By Type'
EXEC(@S)
Drop Table TEST
GO
xyxfly 2006-08-11
  • 打赏
  • 举报
回复
Create Table TEST
(Type Char(1),
State datetime
)
Insert TEST
Select 'A','2006-08-10' Union All
Select 'A','2006-08-14' Union All
Select 'A','2006-08-13' Union All
Select 'B','2006-08-20' Union All
Select 'B','2006-08-17' Union All
Select 'B','2006-08-19' Union All
Select 'C','2006-08-15' Union All
Select 'C','2006-08-20'
GO
select * from test

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
Select @S='Select Type'+@S+' From TEST Group By Type'
EXEC(@S)
QQMagicer 2006-08-11
  • 打赏
  • 举报
回复
你是求同一天的数量吗?那何必这样子呢,直接如下就可以了
Select @S=@S+',count(*) As '+State
From TEST
Group By State
Order By State
kdg2000 2006-08-11
  • 打赏
  • 举报
回复
创建Test表改为
Create Table TEST
(Type Char(1),
State datetime --此处改为datetime类型
Insert TEST
Select 'A','2006-6-6' Union All
Select 'A','2006-6-7' Union All
Select 'A','2006-6-7' Union All
Select 'B','2006-6-6' Union All
Select 'B','2006-6-6' Union All
Select 'B','2006-6-7' Union All
Select 'C','2006-6-6' Union All
Select 'C','2006-6-7'
GO
kdg2000 2006-08-11
  • 打赏
  • 举报
回复
大家帮忙, 在case条件中无论怎么转换datetime都无济于事,convert也用了

34,872

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧