create table #temp ( 语文 int, 数学 int ,英语 int ) insert #temp select 70, 80, 58 --SQL: select 语文 = case when 语文 >= 80 then N'优秀' when 语文 >= 60 then N'及格' else N'不及格' end, 数学 = case when 数学 >= 80 then N'优秀' when 数学 >= 60 then N'及格' else N'不及格' end, 英语 = case when 英语 >= 80 then N'优秀' when 英语 >= 60 then N'及格' else N'不及格' end from #temp /* 语文 数学 英语 及格 优秀 不及格 */
;with cte as ( select 等级 = N'优秀', 分数 = 80 union all select N'及格', 60 union all select N'不及格', 0 ) select * from ( select m.科目, n.等级 from ( select * from #temp a unpivot (分数 for 科目 in([语文], [数学], [英语])) b ) m cross apply (select top(1) * from cte where 分数 <= m.分数 order by 分数 desc) n ) x pivot (max(等级) for 科目 in([语文], [数学], [英语])) y/* 语文 数学 英语 及格 优秀 不及格 */
create table #temp ( 语文 int, 数学 int ,英语 int ) insert #temp select 70, 80, 58 create table #(segm1 int,segm2 int,scroe nvarchar(3)) insert into # select 80,100,'优秀' insert into # select 60,79,'及格' insert into # select 0,60,'不及格' go select b.scroe 语文,c.scroe 数学,d.scroe 英语 from #temp a,# b,# c,# d where a.语文 between b.segm1 and b.segm2 and a.数学 between c.segm1 and c.segm2 and a.英语 between d.segm1 and d.segm2 drop table #temp,# /* 语文 数学 英语 ---- ---- ---- 及格 优秀 不及格(1 行受影响)*/
呵呵... 一条语句: create table #temp ( 语文 int, 数学 int ,英语 int ) insert #temp select 70, 80, 58 go ;with cte as( select 80 segm1,100 segm2,'优秀' scroe union all select 60,79,'及格' union all select 0,60,'不及格' ) select b.scroe 语文,c.scroe 数学,d.scroe 英语 from #temp a,cte b,cte c,cte d where a.语文 between b.segm1 and b.segm2 and a.数学 between c.segm1 and c.segm2 and a.英语 between d.segm1 and d.segm2 drop table #temp /* 语文 数学 英语 ---- ---- ---- 及格 优秀 不及格(1 行受影响)*/
不用case when 那就别用SQL 用手写把。
最简单的方法是用case when
create table Score ( 数学 int, 外语 int, 语文 int ) insert into Score (数学,外语,语文) select 78,56,97 select 语文=case when 语文>=60 and 语文<80 then '及格' when 语文<60 then '不及格' else '优秀' end, 数学=case when 数学>=60 and 数学<80 then '及格' when 数学<60 then '不及格' else '优秀' end, 外语=case when 外语>=60 and 外语<80 then '及格' when 外语<60 then '不及格' else '优秀' end from Score 语文 数学 外语 ------ ------ ------ 优秀 及格 不及格(1 行受影响)
(
语文 int, 数学 int ,英语 int
)
insert #temp
select 70, 80, 58
--SQL:
select
语文 = case when 语文 >= 80 then N'优秀' when 语文 >= 60 then N'及格' else N'不及格' end,
数学 = case when 数学 >= 80 then N'优秀' when 数学 >= 60 then N'及格' else N'不及格' end,
英语 = case when 英语 >= 80 then N'优秀' when 英语 >= 60 then N'及格' else N'不及格' end
from #temp
/*
语文 数学 英语
及格 优秀 不及格
*/
(
select 等级 = N'优秀', 分数 = 80 union all
select N'及格', 60 union all
select N'不及格', 0
)
select * from
(
select m.科目, n.等级 from
(
select * from #temp a
unpivot
(分数 for 科目 in([语文], [数学], [英语])) b
) m
cross apply
(select top(1) * from cte where 分数 <= m.分数 order by 分数 desc) n
) x
pivot
(max(等级) for 科目 in([语文], [数学], [英语])) y/*
语文 数学 英语
及格 优秀 不及格
*/
(
语文 int, 数学 int ,英语 int
)
insert #temp
select 70, 80, 58
create table #(segm1 int,segm2 int,scroe nvarchar(3))
insert into # select 80,100,'优秀'
insert into # select 60,79,'及格'
insert into # select 0,60,'不及格'
go
select b.scroe 语文,c.scroe 数学,d.scroe 英语 from #temp a,# b,# c,# d
where a.语文 between b.segm1 and b.segm2
and a.数学 between c.segm1 and c.segm2
and a.英语 between d.segm1 and d.segm2
drop table #temp,#
/*
语文 数学 英语
---- ---- ----
及格 优秀 不及格(1 行受影响)*/
一条语句:
create table #temp
(
语文 int, 数学 int ,英语 int
)
insert #temp
select 70, 80, 58
go
;with cte as(
select 80 segm1,100 segm2,'优秀' scroe
union all
select 60,79,'及格'
union all
select 0,60,'不及格'
)
select b.scroe 语文,c.scroe 数学,d.scroe 英语 from #temp a,cte b,cte c,cte d
where a.语文 between b.segm1 and b.segm2
and a.数学 between c.segm1 and c.segm2
and a.英语 between d.segm1 and d.segm2
drop table #temp
/*
语文 数学 英语
---- ---- ----
及格 优秀 不及格(1 行受影响)*/
create table Score
(
数学 int,
外语 int,
语文 int
)
insert into Score
(数学,外语,语文)
select 78,56,97 select 语文=case when 语文>=60 and 语文<80 then '及格' when 语文<60 then '不及格' else '优秀' end,
数学=case when 数学>=60 and 数学<80 then '及格' when 数学<60 then '不及格' else '优秀' end,
外语=case when 外语>=60 and 外语<80 then '及格' when 外语<60 then '不及格' else '优秀' end from Score
语文 数学 外语
------ ------ ------
优秀 及格 不及格(1 行受影响)