34,576
社区成员
发帖
与我相关
我的任务
分享
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 热工
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
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
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' 无效。
select Professional,COUNT(1) c from device where classfication='mStandard' and ParentID is null group by Professional
消息 207,级别 16,状态 1,第 4 行
列名 'mStandard' 无效。
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' 无效。
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
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
*/