T-sql一个面试题,大家帮忙看看怎么解决

hy_rr 2011-03-12 08:25:26
一张表,里面有3个字段:语文,数学,英语。三个字段的值分别是语文70分,数学80分,英语58分,用一条sql语句查出结果(大于或等于80表示为优秀,大于或等于60表示为及格,小于60的表示不及格)。
显示格式:
语文 数学 英语
及格 优秀 不及格

怎样在sql中不用case when语句就能实现这种结果!
...全文
211 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
luhai19810713 2011-03-14
  • 打赏
  • 举报
回复
case when
michaelgong 2011-03-14
  • 打赏
  • 举报
回复

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 行受影响)
xiaoxiangqing 2011-03-14
  • 打赏
  • 举报
回复
最简单的方法是用case when
幸运的意外 2011-03-14
  • 打赏
  • 举报
回复
不用case when 那就别用SQL 用手写把。
hy_rr 2011-03-14
  • 打赏
  • 举报
回复
我的意思是不用case when语句,有没有其他的方法,这只是个例子,表里有很多条记录,而且值各不相同,怎样用sql语句实现!我有一个思路,可就是不知道怎么实现,我想先把表里竖着的记录横过来,然后再判断,生成出来的结果再竖过来,就是不知道怎么实现?
guanjm 2011-03-13
  • 打赏
  • 举报
回复
用函数啊,要扩展性思维
青田 2011-03-12
  • 打赏
  • 举报
回复
还有很多东西得学啊
-晴天 2011-03-12
  • 打赏
  • 举报
回复
呵呵...
一条语句:
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 行受影响)

*/
-晴天 2011-03-12
  • 打赏
  • 举报
回复
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 行受影响)

*/
Shawn 2011-03-12
  • 打赏
  • 举报
回复
;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

/*
语文 数学 英语
及格 优秀 不及格
*/
Shawn 2011-03-12
  • 打赏
  • 举报
回复
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
/*
语文 数学 英语
及格 优秀 不及格
*/
--小F-- 2011-03-12
  • 打赏
  • 举报
回复
为什么 不用case when语句? 最简单的就是CASE WHEN了

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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