最近看了些查询方面的习题,对一些习题表示无从下手
比如:查询成绩比该课程平均成绩低的记录。
比如:查询成绩比该课程平均成绩低的记录。
解决方案 »
- SQL Server安装问题 急啊```````````````````````````````````````
- 事务回滚
- 存储过程 output问题
- 数据更新问题
- iif函数该用在什么地方啊?
- sql 查询得出去年同期得分
- 的全文搜索对一些关键字不能搜索 CONTAINS(title,'"*供奥*"')不能搜索到内容
- 急!!!!如何进行批量加主键???
- help! 急 急 如何将两个数据库并成一个数据库,在线候教!
- 如何在vf的表单中加一个用于显示的表~~~~~~~~~~~~~~~~~~~~~~那个表的字段是动态的~~~
- 大家帮忙想下这个SQL语句。。。
- sql server2000与2005)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
FROM A
WHERE A.chengji < (SELECT AVG(chengji) FROM A AS B WHERE A.科目 = B.科目 GROUP BY 科目)类似这样的关联
go
if (object_id('tempdb..#temp', 'u') is not null)
drop table #temp
go
create table #temp
(
ID int identity,
[name] nvarchar(10),
score int
)
insert into #temp
select N'A', 88 union all
select N'A', 99 union all
select N'B', 77
go
select * from #temp
--SQL:
select * from
(
select
*,
avgScore = avg(1.*score) over(partition by name)
from #temp
) t
where score < avgScore--result:
/*
ID name score avgScore
1 A 88 93.500000
*/
use CSDN
go
if (object_id('tempdb..#temp', 'u') is not null)
drop table #temp
go
create table #temp
(
ID int identity,
[name] nvarchar(10),
score int
)
上面这些代码是查看表存在不存在, 并创建表的一个操作
insert into #temp
select N'A', 88 union all
select N'A', 99 union all
select N'B', 77
go插入一些数据.
select * from #temp
测试下数据都OK不.
--SQL:
select * from
(
select
*,
avgScore = avg(1.*score) over(partition by name)
from #temp
) t
where score < avgScore这里才是你需要的SQL语句. 不解释了 你自己百度一下吧. --result:
/*
ID name score avgScore
1 A 88 93.500000
*/另外 这个不是存储过程
SELECT *
FROM A
WHERE A.chengji < (SELECT AVG(chengji) FROM A AS B WHERE A.科目 = B.科目 GROUP BY 科目)
楼主更好理解