簡體   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 

不知道這有什么不對。 我想得到最新的一年。 我應該使用MAX還是其他東西。

在Oracle中,除非GROUP BY子句中包含各個列,否則SELECT列表中不能包含聚合函數和單個列。

您可以使用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;

您還可以使用以下查詢來獲得相同的結果。 您必須選擇最適合您的那個。

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);

如果要聚合一列( 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

在語法上是有效的。 它是否為您提供您想要的結果是另一個問題 - 您需要告訴我們您的表格是什么樣的,其中包含哪些數據,您想要的結果等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM