关于动态Case When的写法

将星陨落天命难违 2012-04-17 01:15:41
不知道标题描述有没有歧义。先看我写的SQL:

select COUNT(1) c,
(case Professional
when '' then ' '
when '电测' then '电测'
when '电能' then '电能'
when '电能/互感器' then '电能/互感器'
when '高压' then '高压'
when '热工' then '热工'
else '其它' end) as Professional
from device where classfication='mStandard' and ParentID is null group by Professional

得出结果:

c Professional
4
16 电测
8 电能
2 电能/互感器
3 高压
14 热工



但是考虑到Professional有可能是动态的,那么上面的写法局限性就很大了,不知能不能动态控制case when?
有个前提是when '' then ' '一定要执行
...全文
508 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

select COUNT(1) c,
case ISNULL(Professional,'COL-NAN')
when 'COL-NAN' then '没有值'
when '' then ' '
when Professional then Professional
else '其它' end as Professional
from device where classfication='mStandard' and ParentID is null group by Professional

这个可以得到我要的结果
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else '没有值' end) ['+ Professional+']'
from (select distinct Professional from device where classfication='mStandard' and ParentID is null) as a
set @sql=@sql+' group by Professional'
exec(@sql)
  • 打赏
  • 举报
回复
是when NULL then '没有值'这一行。
如果Professional这列的值有NULL的话,转换后是'其它',并不是'没有值'
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
mStandard=classfication 是你写的条件啊,你应该知道怎么回事的 这是别的表的字段?还是只是一个值?还是?
[/Quote]
是同一个表的字段


select COUNT(1) c,
case Professional
when '' then ' '
when NULL then '没有值'
when Professional then Professional
else '其它' end as Professional
from device where classfication='mStandard' and ParentID is null group by Professional

先不看红色那行,可以得到想要的结果。现在的问题是数据库中字段是NULL时不是when NULL then ''这样判断吗?
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
mStandard=classfication 是你写的条件啊,你应该知道怎么回事的 这是别的表的字段?还是只是一个值?还是?
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) ['+ Professional+']'
from (select distinct Professional from device where ParentID is null) as a
set @sql=@sql+' group by Professional'
exec(@sql)
  • 打赏
  • 举报
回复

declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) ['+ Professional+']'
from (select distinct Professional from device where classfication=mStandard and ParentID is null) as a
set @sql=@sql+' group by Professional'
exec(@sql)



消息 207,级别 16,状态 1,第 4 行
列名 'mStandard' 无效。
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
8楼说的对,起始楼主这个根本就不是什么动态case
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
这个是你自己写的连接条件啊
你应该知道是怎么回事啊 还是是一个值啊
  • 打赏
  • 举报
回复
1L写的SQL其实是统计数量的,如果有相同列名的则相加。
原始SQL语句是

select Professional,COUNT(1) c from device where classfication='mStandard' and ParentID is null group by Professional
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) ['+ Professional……
[/Quote]


消息 207,级别 16,状态 1,第 4 行
列名 'mStandard' 无效。
十三门徒 2012-04-17
  • 打赏
  • 举报
回复

declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) ['+ Professional+']'
from(select Professional from device where classfication=mStandard and ParentID is null) as b
十三门徒 2012-04-17
  • 打赏
  • 举报
回复
[code=SQL][/declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', max(case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) ['+ Professional+']'
from(select Professional from device where classfication=mStandard and ParentID is null) as b
]
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
给你个动态case when 的实例:

SQL code


create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values(……
[/Quote]

declare @sql varchar(8000)
set @sql='select COUNT(1) c'
select @sql=@sql+', (case Professional when '''+Professional+''' then '''+Professional+''' else 其它 end) as Professional'
set @sql=@sql+' from device where classfication=mStandard and ParentID is null group by Professional'
exec(@sql)


消息 207,级别 16,状态 1,第 3 行
列名 'Professional' 无效。
消息 207,级别 16,状态 1,第 3 行
列名 'Professional' 无效。
Mr_Nice 2012-04-17
  • 打赏
  • 举报
回复
建一个表TB。

电测
电能
...


与当前
device
right join TB

对应值为null的就用‘其它’,

类似这样
select COUNT(1) c,
(case when device.Professional is null then '其它'
when device.Professional='' then ' '
else TB.Professional end) as Professional
from device
right join TB on device.Professional = TB.Professional
where classfication='mStandard' and ParentID is null group by device.Professional





叶子 2012-04-17
  • 打赏
  • 举报
回复
给你个动态case when 的实例:

create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go

declare @sql varchar(8000)
set @sql = 'select 姓名'
select @sql = @sql + ' , max(case 课程 when ''' + 课程+ ''' then 分数 else 0 end) [' + 课程+ ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql)
/*
姓名 数学 物理 语文
---------- ----------- ----------- -----------
李四 84 94 74
张三 83 93 74
*/

34,576

社区成员

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

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