sql server 嵌套查询的问题

sql server 嵌套查询的问题,第1张

这样就可以了:n 是你要返回的行数。

select top n from datatable where name=

如果非要嵌套,应该这样:

select top 1 from (select from datatable where name=) as a

或者:

select top 1 from (select from datatable where name=) a

select achild_id, sum(bnum)aprice

from Base a,BOM b

where achild_id = bchild_id

group by achild_id,aprice

--已经测试了,如果还有什么问题 可以追问!

--汗,,,我不就是录入数据的时候用001表示你的1001嘛,,现在我改过来了。你在试试看。

--我偷懒,你不能偷懒啊。

--以下在SQL2005测试通过。

create table #stu_cour(cno varchar(10),sno varchar(10),grade int)

create table #student(sno varchar(10),sname varchar(10),deptno varchar(10))

create table #teacher(tno varchar(10),tname varchar(10),deptno varchar(10))

create table #course(cno varchar(10),cname varchar(10), tno varchar(10), credit int)

insert #stu_cour

select 'sx','1001',100 union all

select 'yw','1001',76 union all

select 'yy','1001',54 union all

select 'sx','1002',85 union all

select 'yy','1002',60 union all

select 'hx','1002',66 union all

select 'sx','1003',77 union all

select 'yy','1003',88 union all

select 'sx','1004',99 union all

select 'yw','1004',98 union all

select 'yy','1004',67 union all

select 'sx','1005',89 union all

select 'yy','1005',78 union all

select 'yw','1005',56

insert #student

select '1001','学生1','081班' union all

select '1002','学生2','081班' union all

select '1003','学生3','081班' union all

select '1004','学生4','082班' union all

select '1005','学生5','082班'

insert #teacher

select 'T1','张星','081班' union all

select 'T2','老师2','082班'

insert #course

select 'sx','数学','T1',null union all

select 'yw','语文','T1',null union all

select 'hx','化学','T2',null union all

select 'yy','英语','T2',null

--1各门功课取得最高成绩的学生姓名及其成绩

select ccname,ssname,max(scgrade) maxgrade

from #student s join #stu_cour sc on ssno=scsno

join #course c on sccno=ccno

group by ccname,sccno,ssname

having max(scgrade) >=all(

select max(sc2grade)

from #stu_cour sc2 where sccno=sc2cno

group by sc2cno

)

--2查询选修了1001学生选修的全部课程的学生学号(即:学生2、学生3)

select distinct sno

from #stu_cour sc

where not exists

(

select cno

from #stu_cour

where sno='1001'

and

cno not in (select cno from #stu_cour where sno=scsno)

)

--3查询选修了tname=‘张星’开设的全部课程的学生姓名(即:学生2、学生3)

select distinct ssname

from #stu_cour sc join #student s on scsno=ssno

where not exists

(

select cno

from #teacher t join #course c on ttno=ctno

where tname='张星'

and

cno not in (select cno from #stu_cour where sno=scsno)

)

drop table #stu_cour

drop table #student

drop table #teacher

drop table #course

我不知道他的对不对

白天验证一下

我给你一个看你能看明白不

select ssn,ssd from s join

(select s#,count(distinct c#) num from sc group by s#) a

on

as#=ss#

where

anum=(select count(distinct c#) num from c)

以上就是关于sql server 嵌套查询的问题全部的内容,包括:sql server 嵌套查询的问题、sql 求和 嵌套查询、请教数据库脚本语言的嵌套查询几个问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10194765.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存