Exact result in Google Query, followed by partial match if exact result does not exist

跟風遠走 提交于 2019-12-12 05:28:16

问题


I'm using Google Query to search a Google Spreadsheet: https://jsfiddle.net/BaronGrivet/ctkpvuac/3/

Example Data:

Block Hill Road
Greenhill Road
Hill Road
Brownhill Road

Example Query:

'SELECT A WHERE LOWER(A) LIKE "%'+searchInput+'%" ORDER BY A ASC'

If someone searches for "Hill" it will return all of the rows.

However if someone searches for "Hill Road" it will still return all of the rows with "Hill Road" listed last.

Ideally I want it to either only return the exact match "Hill Road", or at least have "Hill Road" as the first response.

Is there a way to structure the query so that exact matches are either only shown or shown first?

Or would I have to script a fallback where I first query for an exact match and if that's not returned I query a partial match?


回答1:


Perhaps a step in the right direction:

={query(A:A,"select * where LOWER(A) LIKE 'hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")} 

to give the order:

Hill Road
Hill Road
Greenhill Road
Brownhill Road
Block Hill Road

(the exact match is duplicated), or possibly prepend a space to all entries in ColumnA and apply:

={query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")}

to give:

 Hill Road
 Block Hill Road
 Hill Road
 Greenhill Road
 Brownhill Road
 Block Hill Road

If you would rather not have the 'exact' and 'similar' duplicated then wrap in UNIQUE:

(a) without added spaces:

=unique({query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")})

output:

Hill Road
Greenhill Road
Brownhill Road
Block Hill Road

(b) with added spaces:

=unique({query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")})

output:

 Hill Road
 Block Hill Road
 Greenhill Road
 Brownhill Road

If choosing UNIQUE order asc may make better sense.



来源:https://stackoverflow.com/questions/44752790/exact-result-in-google-query-followed-by-partial-match-if-exact-result-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!