首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

数据库查询思路解决方法

2012-06-22 
数据库查询思路最近看了些查询方面的习题,对一些习题表示无从下手比如:查询成绩比该课程平均成绩低的记录。

数据库查询思路
最近看了些查询方面的习题,对一些习题表示无从下手
比如:查询成绩比该课程平均成绩低的记录。


[解决办法]

探讨

谢谢回答。 我该如何区分主查询中查询记录的字段能够对应上子查询中的相应的字段呢。
引用:

无从下手就先把问题差分开处理。
查询成绩比该课程平均成绩低的记录。
1.先查询课程平均成绩,形成子查询A
2.查询成绩小于A中平均成绩的记录。

[解决办法]
SQL code
use CSDNgoif (object_id('tempdb..#temp', 'u') is not null)    drop table #tempgocreate table #temp(    ID int identity,    [name] nvarchar(10),    score int)insert into #tempselect N'A', 88 union allselect N'A', 99 union allselect N'B', 77goselect * from #temp--SQL:select * from (    select        *,        avgScore = avg(1.*score) over(partition by name)    from #temp) twhere score < avgScore--result:/*ID    name    score    avgScore1    A    88    93.500000*/
[解决办法]
探讨
您是在屠杀一个刚想学存储过程的少年的心。。

引用:

SQL code
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),
s……

[解决办法]
除了楼上说的,还可以考虑用any、all这些关键字来实现大于所有xxx或者in的部分逻辑
[解决办法]
4楼的答案是正确的, 你的这个问题只能通过这样的方式来解决. 

我借花献佛解释下他的太呆.
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
*/



另外 这个不是存储过程

热点排行