刚开始学,问题比较基础,感谢目标:返回table中所有A列值大于A列平均的记录
select *,
from table
where A > (select avg(A) from table)
还是报错,请问where不能这么用吗?
要达到目的怎么写比较好
谢谢------------------------------------------------另外请教,在表的联合的时候,以下两种写法有没有什么优劣。
前提是表的数据量比较大
1.select a.A,b.B
from table1 a
join
(select A, B
from table2
where <C=condition>)b
on a.A=b.A2. select a.A,b.B
from table1 a
join table2 b
on a.A=b.A
where b.C=condition
select *,
from table
where A > (select avg(A) from table)
还是报错,请问where不能这么用吗?
要达到目的怎么写比较好
谢谢------------------------------------------------另外请教,在表的联合的时候,以下两种写法有没有什么优劣。
前提是表的数据量比较大
1.select a.A,b.B
from table1 a
join
(select A, B
from table2
where <C=condition>)b
on a.A=b.A2. select a.A,b.B
from table1 a
join table2 b
on a.A=b.A
where b.C=condition
from table
where A > (select avg(A) from table group by 分组的字段)
多表查询的时候用第二种方法更快
SELECT *
FROM table
WHERE A > ( SELECT AVG(A)
FROM table
) 2、哪个写法好,楼主可以看一下执行计划,这两个的查询都是怎么执行的;这俩应该差不多。
但还是报错 invalid token 'select'然后1楼提到group by,可能我问题没有表述清楚
假设table如下,表中所有记录数量平均值为12.7,我想通过sql select表中数量>这个平均12.7的记录。
即返回第一行和第三行数据,怎么写比较好姓名 数量 金额
A 14 300
B 2 50
C 22 410
2. 第一种方法有子查询,个人认为第二种更好.if not object_id(N'Tempdb..#Tmp_DATA') is null
drop table #Tmp_DATA
Go
create table #Tmp_DATA(A int) INSERT INTO #Tmp_Data
Select 2 union
Select 7 union
Select 12
select *
from #Tmp_Data
where A > (select avg(A) from #Tmp_Data)
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([姓名] nvarchar(21),[数量] int,[金额] int)
Insert #T
select N'A',14,300 union all
select N'B',2,50 union all
select N'C',22,410
Go
--测试数据结束
SELECT *
FROM #T
WHERE 数量 > ( SELECT AVG(数量)
FROM #T
)