问题1:
我想用group by但是不想用到聚合函数,有办法吗?具体问题如下:
有表aaaFruitName Country Price
apple china 1
banana china 2
orange china 3
apple japan 2
cherry japan 5要求:列出每个国家价格大于3的水果的信息
select *
from aaa
group by country
having price>=3我这样写是不对的,因为用了group by,所以select后面必须用聚合函数,但我这里没必要这么用啊,怎么办?问题2:
是不是having后面也要用聚合函数才行啊问题3:
我这么写为什么也不对啊
select count(fruitname) as Mycount
from aaa
group by country
where price>=2
在PL/SQL下做的。
我想用group by但是不想用到聚合函数,有办法吗?具体问题如下:
有表aaaFruitName Country Price
apple china 1
banana china 2
orange china 3
apple japan 2
cherry japan 5要求:列出每个国家价格大于3的水果的信息
select *
from aaa
group by country
having price>=3我这样写是不对的,因为用了group by,所以select后面必须用聚合函数,但我这里没必要这么用啊,怎么办?问题2:
是不是having后面也要用聚合函数才行啊问题3:
我这么写为什么也不对啊
select count(fruitname) as Mycount
from aaa
group by country
where price>=2
在PL/SQL下做的。
解决方案 »
- oracle数据库中的数据导入sql-server中(韩文乱码),顶者有分
- Oracle结果查询
- 为何用VB程序访问ORACLE得到的结果与 PL/SQL DEVELOPER中的结果不一致?
- 在執行SQL查詢的時候,出現以下錯誤,求助。
- 这两句查询为什么第一句不能运行,而第二句却能运行?
- 创建触发器出错?????
- 请教如保利用oracle后台向客户端发送“消息”
- 请问Oracle8.0以上是否就是8i?
- 偶对Oracle七窍通了六窍,请各位推荐一些书记或文章
- 如何在Oracle8.1.6中建立数据库?
- 无法打开OEM,请各位大侠帮帮忙
- 数据恢复其实并不复杂?因为recover命令完全是由oracle自动完成的
select *
from aaa
where price>=3
不就行了吗
select * from aaa where price >= 4;2.having一般和group by一起用3.应该是:
select country, count(price) from aaa where price >= 2 group by country;
from aaa
where price>=3 order by country
如果要查出 水果种类有两种以上的国家的信息,怎么写呢
select country
from aaa
group by country
having count(*)>=2我是认为这么写,但是是错的,因为select后面没有用聚合函数。怎么整、?
select country from aaa group by country
having count(fruitname)>1如果你上题中要按国家分组,查出每个国家价格大于3的水果名
这么写
select country,ltrim(max(fruitname),',') fruitname from
( select country,sys_connect_by_path(fruitname,',') fruitname from(
select a.*,row_number()over(partition by country order by price) rn from aaa a where price>=3)
start with rn=1
connect by country=prior country and rn-1=prior rn)
group by countryCOUNTRY FRUITNAME
china orange,banana
japan cherry你还是先详细看看group by 和聚合函数那块的说明吧
这句语法上应该没有错误,不过如果国家有重复水果的话。数据上是有问题的,应该改为
select country
from aaa
group by country
having count(distinct(FruitName))>=2
其实我之所以认为有错,是做了这个:有表
表bbb:
id user num
1 a 3
2 a 4
3 b 5
4 b 7 查询出现过2次的user:
select user
from bbb
group by user
having count(*)=2这样是正确的,但是如果改为:
select *
from bbb
group by user
having count(*)=2就会报错了,NOT A GROUP BY EXPRESSION,就改了个小星号,为什么就错了呢?
因为你按user 分组,所以前面的没有问题
改成* 后,显示所有字段,那么就必须为除了user 字段外的其他字段加上聚合函数
你没有,所以出错了
select* from bbb where user in(
select user
from bbb
group by user
having count(*)=2)
这样可以
解释的非常清楚,高手!!谢谢
我的另一个帖子的9楼的提问能帮忙看看吗
http://topic.csdn.net/u/20090804/13/d31087c6-1f05-441b-991d-b598e7a7e98e.html?seed=416037155&r=58845215#r_58845215
你这边不写orderby country 也是对的,不是很明白orderby的作用,这么一写是不是china的行排在japan的行前面??
再问你隔:
apple china 1
banana china 2
orange china 3
apple japan 2
cherry japan 5我想查出大于平均价格的所有记录,我这么写:
SELECT *
FROM aaa
where price>avg(price)
报错了:为什么啊,感觉很简单啊
我是新手 ,谢谢帮助O(∩_∩)O
因为avg()是聚合函数。你那一句是分组查询,还要把这个条件放到having后才不会报错
这个应该用子查询
select * from aaa where price>(select avg(price) from aaa)
select * from aaa a where price>(
select avg(price) from aaa where country=a.country group by country)
你买本讲解比较详细的书,先学习学习,然后做些练习
学起来会快一些
用了聚合函数,就是等于group by了吗?但是我没有写group by啊
这里为什么要用子查询啊 不懂啊,为什么直接 price>avg(price)不行呢
没表达清楚..我是说你那句要写成分组函数,然后把price>avg(price)放到having后
或者就用子查询,我写的那句
会报错是因为你的语句是逐行查询的,条件后面的参数不能用到聚合函数
select country
from aaa
group by country
having price>avg(price) 还是不对啊
你都用了分组了这么还用price作条件
将price换成max(price)试试
总结下:
也就是where后面不能用到聚合函数
having后面的表达式必须除了常量外 ,一定要为聚合函数
对吧
比如group by country ,那么having后也可以用country
这样很难讲清楚,你多看看相关方面的知识点,自己想想就知道了