求一个SQL

zhaokeke2004 2016-01-04 08:01:18
有表如下
姓名 语文 数学 英语
张三 NA 100 100
李四 50 60 NA
王五 80 90 80

要得到

姓名 语文 数学 英语 平均分
张三 NA 100 100 100
李四 50 60 NA 55
王五 80 90 80 83

NA就代表未参加考试,算平均分的时候要去除,SQL怎么写 ?
...全文
404 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy18755122285 2016-03-11
  • 打赏
  • 举报
回复
select grade2.name,grade2.chinese,grade2.math,grade2.english, (case chinese when 'NA' then 0 else convert(integer,chinese) end + case math when 'NA' then 0 else convert(integer,math) end + case english when 'NA' then 0 else convert(integer,english) end ) / (case chinese when 'NA' then 0 else 1 end + case math when 'NA' then 0 else 1 end + case english when 'NA' then 0 else 1 end ) from grade2
刘大神仙 2016-01-07
  • 打赏
  • 举报
回复
我去。。好强大的人民军
卖水果的net 2016-01-06
  • 打赏
  • 举报
回复
好像 LZ 前两天发了一个 SQL Server 版的帖子。 我给你回复了,那个语法拿到 oracle 也可以用,换成 decode 更好一些; http://bbs.csdn.net/topics/391886747
PCCYC 2016-01-06
  • 打赏
  • 举报
回复
3楼的正解: SQL> select yw,sx,yy, (decode(yw,'NA',0,yw)+decode(sx,'NA',0,sx)+decode(yy,'NA',0,yy))/ (decode(yw,'NA',0,1)+decode(sx,'NA',0,1)+decode(yy,'NA',0,1)) myavg from tstudy; YW SX YY MYAVG ---------- ---------- ---------- ---------- NA 100 100 100 50 60 NA 55 80 90 80 83.3333333
IvanO_O 2016-01-06
  • 打赏
  • 举报
回复
引用 2 楼 zhaokeke2004 的回复:
我是要求所有课程的平均分,不是单门课程的平均分啊
这个可以,decode还是很强大的
beyon2008 2016-01-06
  • 打赏
  • 举报
回复
SQL> select xm,yw,sx,yy, (decode(yw,'NA',0,yw)+decode(sx,'NA',0,sx)+decode(yy,'NA',0,yy))/ (decode(yw,'NA',0,1)+decode(sx,'NA',0,1)+decode(yy,'NA',0,1)) myavg  from tstudy;
陈灬风 2016-01-05
  • 打赏
  • 举报
回复
楼上是可行的
z123zjf 2016-01-05
  • 打赏
  • 举报
回复
(decode(语文,'NA',0,语文)+decode(数学,'NA',0,数学)+decode(英语,'NA',0,英语))/(decode(语文,'NA',0,1)+decode(数学,'NA',0,1)+decode(英语,'NA',0,1)) 这样?
zhaokeke2004 2016-01-04
  • 打赏
  • 举报
回复
我是要求所有课程的平均分,不是单门课程的平均分啊
z123zjf 2016-01-04
  • 打赏
  • 举报
回复
avg(decode(语文,'NA',null,语文))这样

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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