问题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下做的。

解决方案 »

  1.   

    汗,按你要求根本不要分组了
    select * 
    from aaa 
    where price>=3
    不就行了吗
      

  2.   

    1.为什么不用
    select * from aaa where price >= 4;2.having一般和group by一起用3.应该是:
    select country, count(price) from aaa where price >= 2 group by country;
      

  3.   

    按国家顺序显示的话加个select * 
    from aaa 
    where price>=3 order by country
      

  4.   

    那我问题1这么问:
    如果要查出 水果种类有两种以上的国家的信息,怎么写呢
     
    select country
    from aaa 
    group by country 
    having count(*)>=2我是认为这么写,但是是错的,因为select后面没有用聚合函数。怎么整、?
      

  5.   


    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 和聚合函数那块的说明吧
      

  6.   

    分组查询前面的字段要用聚合函数(用来分组的那个字段除外),还有条件基本上都是写在from 后面的where子句里,少用having
      

  7.   


    这句语法上应该没有错误,不过如果国家有重复水果的话。数据上是有问题的,应该改为
     select country
     from aaa
     group by country
     having count(distinct(FruitName))>=2
      

  8.   


    其实我之所以认为有错,是做了这个:有表
    表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,就改了个小星号,为什么就错了呢?
      

  9.   

    因为分组查询中,前面查询的字段除了用来分组的字段外,都必须用聚合函数,否则系统不知道该取同一组数据中的哪一个
    因为你按user 分组,所以前面的没有问题
    改成* 后,显示所有字段,那么就必须为除了user 字段外的其他字段加上聚合函数
    你没有,所以出错了
    select* from bbb where user in(
    select user
    from bbb
    group by user
    having count(*)=2)
    这样可以
      

  10.   


    解释的非常清楚,高手!!谢谢
    我的另一个帖子的9楼的提问能帮忙看看吗
    http://topic.csdn.net/u/20090804/13/d31087c6-1f05-441b-991d-b598e7a7e98e.html?seed=416037155&r=58845215#r_58845215
      

  11.   


    你这边不写orderby country 也是对的,不是很明白orderby的作用,这么一写是不是china的行排在japan的行前面??
      

  12.   

    那是排序,china会排在japan前面。不过我这里加order by 主要是让查询结果中同一国家的所有列放到一起
      

  13.   


    再问你隔:
    apple      china    1
    banana     china    2
    orange     china    3
    apple      japan    2
    cherry     japan    5我想查出大于平均价格的所有记录,我这么写:
    SELECT *
    FROM aaa
    where price>avg(price)
    报错了:为什么啊,感觉很简单啊
      

  14.   

    报错如下:group function is not allowed here,是不是用了聚合函数一定要用group by呢?
    我是新手 ,谢谢帮助O(∩_∩)O
      

  15.   


    因为avg()是聚合函数。你那一句是分组查询,还要把这个条件放到having后才不会报错
    这个应该用子查询
    select * from aaa where price>(select avg(price) from aaa)
      

  16.   

    如果分国家的话
    select * from aaa a where price>(
    select avg(price) from aaa where country=a.country group by country)
    你买本讲解比较详细的书,先学习学习,然后做些练习
    学起来会快一些
      

  17.   


    用了聚合函数,就是等于group by了吗?但是我没有写group by啊
    这里为什么要用子查询啊 不懂啊,为什么直接  price>avg(price)不行呢
      

  18.   


    没表达清楚..我是说你那句要写成分组函数,然后把price>avg(price)放到having后
    或者就用子查询,我写的那句
    会报错是因为你的语句是逐行查询的,条件后面的参数不能用到聚合函数
      

  19.   

    我这样写了:
    select country
    from aaa
    group by country
    having price>avg(price) 还是不对啊
      

  20.   

    - -!
    你都用了分组了这么还用price作条件
    将price换成max(price)试试
      

  21.   


    总结下:
    也就是where后面不能用到聚合函数
    having后面的表达式必须除了常量外 ,一定要为聚合函数
    对吧
      

  22.   

    having后面的表达式里的参数不一定要用聚合函数,是分组查询结果中的字段也行
    比如group by country ,那么having后也可以用country
    这样很难讲清楚,你多看看相关方面的知识点,自己想想就知道了