问题陈述:给定一个初始条形位置,返回一个条形列表,其中从一个条形到另一个条形的行进距离最短,而不是两次访问同一个条形图。
表BAR_POS存储了我所在区域的以下栏信息:
我创建了一个函数DIST_SCORE(),它可以近似评估给定一对纬度和经度值的距离。
我甚至计算了一个临时视图,它存储了所有条形对之间的所有距离(BAR_POS表中只有~100条):
WITH RECURSIVE result_tb(id, dist, counter) AS (
SELECT D.idA+D.idB-14989798357, MIN(D.dist), 0
FROM dist_matrix AS D
WHERE D.idA = '14989798357' OR D.idB = '14989798357'
UNION ALL
SELECT D.idA+D.idB-result_tb.id, MIN(D.dist), result_tb.counter+1
FROM dist_matrix AS D, result_tb
WHERE D.idA = result_tb.id OR D.idB = result_tb.id
)
SELECT R.id, B.name
FROM result_tb AS R, bar_pos AS B
WHERE R.id = B.id
我想过使用递归查询,但它不允许在递归select语句中使用MIN(),一个聚合函数,如文档中所述。所以以下尝试不起作用!但我希望它能让你了解我想要实现的目标。
假设初始条具有unique_id = 14989798357:
sqlite> .dump bar_pos
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE bar_pos(id INT,name TEXT,lat REAL,lon REAL);
INSERT INTO "bar_pos" VALUES(1127248835,'The Lobby Bar',47.6142099,-122.3198397);
INSERT INTO "bar_pos" VALUES(1194793097,'Comet Tavern',47.6142655,-122.3197336);
INSERT INTO "bar_pos" VALUES(1250696405,'Poco Wine Room',47.6154362,-122.3135746);
INSERT INTO "bar_pos" VALUES(1332014994,'The Saint',47.6176017,-122.3265279);
INSERT INTO "bar_pos" VALUES(1332014999,'Speckled & Drake',47.6166484,-122.3275833);
INSERT INTO "bar_pos" VALUES(1332015004,'Knee High Stocking Co.',47.616968,-122.327413);
INSERT INTO "bar_pos" VALUES(1387947219,'Unicorn Bar',47.6142292,-122.3171081);
INSERT INTO "bar_pos" VALUES(1539274991,'Liberty',47.6235665,-122.3128271);
INSERT INTO "bar_pos" VALUES(1607121865,'Bathtub',47.6130973,-122.3449673);
INSERT INTO "bar_pos" VALUES(1735895574,'Artusi',47.615173,-122.3143967);
INSERT INTO "bar_pos" VALUES(1836754564,'Pioneer Square Saloon',47.6016146,-122.3354445);
INSERT INTO "bar_pos" VALUES(1999272109,'Nitelite',47.611501,-122.3410283);
INSERT INTO "bar_pos" VALUES(2053744069,'Contour',47.6034597,-122.3353422);
INSERT INTO "bar_pos" VALUES(2077248890,'Linda''s',47.6150977,-122.3230019);
INSERT INTO "bar_pos" VALUES(2131716660,'Julia''s',47.621253,-122.3206536);
那么有一种纯粹的SQLite解决这个问题的方法吗?我知道我可以使用其他编程语言轻松解决这个问题,但我现在正在学习SQLite,并希望尽可能多地从SQLite学习。
以下是BAR_POS表转储的前几行:
{{1}}