简体   繁体   English

选择MAX时不是单组组功能

[英]not a single-group group function with MAX in select

Select sg_gameno, Max(sg_Year), sg_end, sg_hostcity, country_olympic_name
  from Summergames s, Country co
 where s.country_isocode = co.country_isocode 

Don't know whats wrong with this. 不知道这有什么不对。 I want to get the lastest year. 我想得到最新的一年。 Should i use MAX or something else. 我应该使用MAX还是其他东西。

In Oracle, you can't have aggregate functions and individual columns in the SELECT list, unless the individual columns are included in GROUP BY clause. 在Oracle中,除非GROUP BY子句中包含各个列,否则SELECT列表中不能包含聚合函数和单个列。

You can use RANK or DENSE_RANK function to rank the records based on thet year and then select the from the resultset the top ranked rows. 您可以使用RANK或DENSE_RANK函数根据年份对记录进行排名,然后从结果集中选择排名最高的行。

select * from (
    select sg_gameno, sg_Year, sg_end, sg_hostcity, country_olympic_name,
           rank() over (order by sg_year desc) as ranking
      from Summergames s, Country co
     where s.country_isocode = co.country_isocode
     )
  where ranking = 1;

You can also use following query to get the same result. 您还可以使用以下查询来获得相同的结果。 You will have to select the one that performs best for you. 您必须选择最适合您的那个。

select sg_gameno, sg_Year, sg_end, sg_hostcity, country_olympic_name
  from Summergames s, Country co
 where s.country_isocode = co.country_isocode
   and sg_Year = (select max(sg_Year)
                    from Summergames s, Country co
                   where s.country_isocode = co.country_isocode);

If you want to aggregate one column ( sg_year ) and to not aggregate others, you need a GROUP BY clause. 如果要聚合一列( sg_year )而不聚合其他列,则需要GROUP BY子句。

Select sg_gameno, Max(sg_Year), sg_end, sg_hostcity, country_olympic_name
  from Summergames s, 
       Country co
 where s.country_isocode = co.country_isocode 
 group by sg_gameno, sg_end, sg_hostcity, country_olympic_name

is syntactically valid. 在语法上是有效的。 Whether it provides you the results you want is another question-- you'd need to tell us what your tables look like, what data is in them, what result you want, etc. 它是否为您提供您想要的结果是另一个问题 - 您需要告诉我们您的表格是什么样的,其中包含哪些数据,您想要的结果等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM