主题:求一条SQL语句
8500708
[专家分:120] 发布于 2007-01-29 20:41:00
姓名 学号 笔试成绩 机试成绩 是否通过
-------------------- -------------------- ----------- ----------- ----
张秋丽 s25301 77 82 是
李斯文 s25302 50 90 否
李斯文 s25302 65 0 否
李文才 s25303 80 58 否
欧阳俊雄 s25304 NULL NULL 否
梅超风 s25318 NULL NULL 否
请问一下!如果我要笔试这一列null的字段显示为 缺考 要怎么办啊!
笔试成绩这一列是整形的列
stumarks表为成绩表 stuinfo表为学员信息表
select 姓名=a.stuname,学号=a.stuno,笔试成绩=b.writtenexam,
机试成绩=b.labexam,是否通过=case
when b.writtenexam <60 or b.labexam<60 then '否'
when b.writtenexam is null or b.labexam is null then '否'
else '是'
end
from stuinfo a left join stumarks b on a.stuno=b.stuno
create table stumarks
(
Examno varchar(20),
stuno varchar(20) not null,
writtenExam int not null,
labexam int not null
)
create table stuinfo
(
stuname varchar(20) not null,
stuno varchar(20) not null,
stusex varchar(20) not null,
stuage int not null,
stuseat int identity(1,1),
stuaddress varchar(255)
)
[em10]
回复列表 (共7个回复)
沙发
ilovemountainking [专家分:3730] 发布于 2007-01-31 18:03:00
select 姓名,学号,NVL(笔试成绩, '缺考'),机试成绩,是否通过 from 表名
板凳
8500708 [专家分:120] 发布于 2007-01-31 21:12:00
select 姓名,学号,NVL(笔试成绩, '缺考'),机试成绩,是否通过 from 表名
谢谢大哥,但想问下nvl指的是什么啊!??我笔试成绩那一栏是整形的列!但我要插 ‘缺考’进去啊!
4 楼
8500708 [专家分:120] 发布于 2007-02-01 19:28:00
十分感谢大哥的回帖!可能没和您说清楚,不好意思!我用的是SQL Server2000
你所说的那个我用不上!还请麻烦再看看 谢谢!![em2]
5 楼
supercrsky [专家分:580] 发布于 2007-02-01 21:00:00
给你做了个标准的,楼主在北大青鸟上学吧。
源码:
exec xp_cmdshell 'md f:\project'
exec xp_cmdshell 'dir f:\'
go
create database stuDB
on
(
name = 'stuDB_Data',
filename = 'f:\project\stuDB_Data.mdf',
size = 2mb,
maxsize = 100mb,
filegrowth = 20%
)
log on
(
name = 'stuDB_Log',
filename = 'f:\project\stuDB_Log.ldf',
size = 1mb,
maxsize = 50mb,
filegrowth = 15%
)
go
if exists(select * from sysobjects where name = 'stuInfo')
drop table stuINfo
if exists(select * from sysobjects where name = 'stuMarks')
drop table stuMarks
go
create table stuInfo
(
stuName varchar(20) not null,
stuNO varchar(20) not null,
stuSex varchar(2) not null,
stuAge int not null,
stuseat int not null,
stuAdress varchar(50) null
)
go
create table stuMarks
(
ExamNo varchar(20) not null,
stuNo varchar(20) not null,
writtenExam int not null,
labExam int not null
)
use stuDb
select * from stuInfo
select * from stuMarks
go
insert into stuInfo
select '张秋丽','s25301','男',18,1,'北京海淀' union
select '李文才','s25302','男',31,3,'地址不详' union
select '李斯文','s25303','女',22,2,'河南洛阳' union
select '欧阳俊雄','s25304','男',28,4,'新疆' union
select '梅超风','s25318','女',23,5,'地址不详'
go
insert into stuMarks
select 's271811','s25303',93,59 union
select 's271813','s25302',63,91 union
select 's271816','s25301',90,83 union
select 's271817','s25318',63,53
go
select * from stuInfo
select * from stuMarks
go
select 应到人数 = (select count(*) from stuInfo),
实到人数 = (select count(*) from stuMarks),
缺考人数 = ((select count(*) from stuInfo)-(select count(*) from stuMarks))
go
if exists(select * from sysobjects where name = 'newTable')
drop table newTAble
go
select stuName, stuInfo.stuNO, writtenExam,labExam,
isPass = case
when writtenExam >= 60 and labExam >= 60 then 1
else 0
end
into newTable from stuINfo left join stuMarks on stuInfo.stuNO = stuMarks.stuNO
go
select * from newTAble
go
declare @avgwritten numeric(3,1),@avglab numeric(3,1)
select @avgwritten = avg(writtenExam), @avglab = avg(labExam) from newTAble
if(@avgwritten > @avglab)
while(1 = 1)
begin
update newTAble set labExam = labExam + 1
if(select max(labExam) from newTAble ) >= 97
break
end
else
while(1 = 1)
begin
update newTAble set writtenExam = writtenExam + 1
if(select max(writtenExam) from newTAble) >= 97
break
end
select * from newTAble
update newTAble set
isPass = case
when writtenExam >= 60 and labExam >= 60 then 1
else 0
end
select stuName as 姓名, stuNo as 学号,
case
when writtenExam is null then '缺考'
else convert(varchar(5),writtenExam)
end as 笔试成绩,
case
when labExam is null then '缺考'
else convert(varchar(5),labExam)
end as 机试成绩
,case
when isPass = 1 then '是'
else '否'
end as 不否通过
from newTAble
go
select count(*) as 总人数,sum(isPass) as 通过人数,((sum(isPass)/count(*))*100) as 通过率 from newTAble
go
select stuName as 缺考学员 from stuInfo where stuNo not in(select stuNO from stuMarks)
select stuName as 缺考学员 from stuINfo where stuNo not in(select stuNo from stuMarks)
6 楼
233238447 [专家分:1350] 发布于 2007-02-05 23:27:00
select (case 笔试成绩 when null then '缺考' else 笔试成绩 end) as 笔试成绩
from tablename
7 楼
Leo64823900 [专家分:960] 发布于 2007-02-06 17:24:00
MS-SQL 可用 ISNULL 函数,作用与 NVL 相同。需要注意的是两个参数的类型应一致。因此,应转换"笔试成绩"的类型。以下是 ISNULL 的使用说明:
Syntax: ISNULL(check_expression, replacement_value)
Arguments:
check_expression
Is the expression to be checked for NULL. check_expression can be of any type.
replacement_value
Is the expression to be returned if check_expression is NULL. replacement_value must have the same type as check_expresssion.
Return Types: Returns the same type as check_expression.
Remarks:
The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned.
我来回复