原始数据库中的Top-n

问题描述 投票:0回答:1

我使用名为“文件地理数据库”的 GIS 数据库,它具有最少的 SQL 支持。 不支持:

窗口函数
  • TOP、LIMIT、FETCH FIRST 或 ROWNUM
  • 相关子查询
  • 是否支持:

标量子查询
  • 派生表
  • 分组依据、排序依据、连接等
  • 用于文件地理数据库报告和分析的 SQL
  • ArcGIS 中使用的查询表达式的 SQL 参考

数据:

我有一个 CITIES 表。

如果支持 SQL 客户端,INSERT 语句将如下所示:

create table cities (objectid int, population_centre varchar2(255), population_2021 number(38,1), other_columns varchar2(255) ) insert into cities (objectid,population_centre,population_2021,other_columns) values (11,'Calgary',1305550,'a'); insert into cities (objectid,population_centre,population_2021,other_columns) values (23,'Edmonton',1151635,'b'); insert into cities (objectid,population_centre,population_2021,other_columns) values (31,'Halifax',348634,'c'); insert into cities (objectid,population_centre,population_2021,other_columns) values (32,'Hamilton',729560,'d'); insert into cities (objectid,population_centre,population_2021,other_columns) values (37,'Kelowna',181380,'e'); insert into cities (objectid,population_centre,population_2021,other_columns) values (40,'Kitchener',522888,'f'); insert into cities (objectid,population_centre,population_2021,other_columns) values (45,'London',423369,'g'); insert into cities (objectid,population_centre,population_2021,other_columns) values (51,'Montreal',3675219,'h'); insert into cities (objectid,population_centre,population_2021,other_columns) values (58,'Oshawa',335949,'i'); insert into cities (objectid,population_centre,population_2021,other_columns) values (59,'Ottawa–Gatineau',1068821,'j'); insert into cities (objectid,population_centre,population_2021,other_columns) values (65,'Quebec City',733156,'k'); insert into cities (objectid,population_centre,population_2021,other_columns) values (67,'Regina',224996,'l'); insert into cities (objectid,population_centre,population_2021,other_columns) values (76,'Saskatoon',264637,'m'); insert into cities (objectid,population_centre,population_2021,other_columns) values (82,'St. Catharines – Niagara Falls',242460,'n'); insert into cities (objectid,population_centre,population_2021,other_columns) values (83,'St. John''s',185565,'o'); insert into cities (objectid,population_centre,population_2021,other_columns) values (90,'Toronto',5647656,'p'); insert into cities (objectid,population_centre,population_2021,other_columns) values (92,'Vancouver',2426160,'q'); insert into cities (objectid,population_centre,population_2021,other_columns) values (94,'Victoria',363222,'r'); insert into cities (objectid,population_centre,population_2021,other_columns) values (98,'Windsor',306519,'s'); insert into cities (objectid,population_centre,population_2021,other_columns) values (99,'Winnipeg',758515,'t'); select * from cities OBJECTID POPULATION_CENTRE POPULATION_2021 OTHER_COLUMNS ---------- --------------------------------- ------------- 11 Calgary 1305550 a 23 Edmonton 1151635 b 31 Halifax 348634 c 32 Hamilton 729560 d 37 Kelowna 181380 e 40 Kitchener 522888 f 45 London 423369 g 51 Montreal 3675219 h 58 Oshawa 335949 i 59 Ottawa–Gatineau 1068821 j 65 Quebec City 733156 k 67 Regina 224996 l 76 Saskatoon 264637 m 82 St. Catharines 242460 n 83 St. John's 185565 o 90 Toronto 5647656 p 92 Vancouver 2426160 q 94 Victoria 363222 r 98 Windsor 306519 s 99 Winnipeg 758515 t

假数据库<>小提琴


问题:

在GIS软件中,我可以在WHERE子句窗口中编写SQL表达式来选择行。

尽管 SQL 支持有限,有没有办法选择人口最多的 4 个城市?

我怀疑答案是否定的。但我不禁想知道在早期数据库的 SQL 功能有限时它是如何完成的。例如,当 SQLite 首次发布时,如果它对 SQL 支持有限,那么 top-n 查询是如何执行的?

sql where-clause greatest-n-per-group arcgis
1个回答
1
投票
db<>fiddle

工具中使用。这是一种强力消除,如果 population_2021 字段中存在具有完全相同值的行,则可能无法正常工作。它依赖于标量子查询支持,但我不确定查询引擎允许多少嵌套,所以尝试一下:

select *
from cities
where population_2021 = (select max(population_2021) from cities)
or population_2021 = (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities))
or population_2021 = (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities)))
or population_2021 = (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities where population_2021 < (select max(population_2021) from cities))))
order by population_2021

© www.soinside.com 2019 - 2024. All rights reserved.