如果键在列表 "foo"、"bar" 中,则 postgresql 获取 hstore 数据的每行键数
postgresql get the per-row number of keys of hstore data if key is in List "foo", "bar"
我尝试计算每一行,键在 hstore 数据列中的位置。
array_length(akeys(tags), 1) as num_keys
这适用于所有标签。
在 table nodes
中有更多不同的标签 - 我将只在我的 SELECT.
中为每行标签编号
"name"=>"Campus", "amenity"=>"restaurant", "wheelchair"=>"yes"
仅计数 "name" -> 结果 1
SELECT
id,
st_x(ST_Transform(geom,4326)) AS lon,
st_y(ST_Transform(geom,4326)) AS lat,
array_length(akeys(tags), 1) as num_keys,
tags->'name' AS name,
tags->'amenity' AS amenity,
tags->'addr:street' AS street,
tags->'addr:housenumber' AS housenumber,
tags->'addr:postcode' AS postcode,
tags->'addr:city' AS city,
tags->'cuisine' AS cuisine,
tags->'beer_garden' AS beer_garden,
tags->'outdoor_seating' AS outdoor_seating,
tags->'smoking' AS smoking,
tags->'brewery' AS brewery,
tags->'website' AS website,
tags->'internet_access' AS wlan,
tags->'phone' AS phone,
tags->'email' AS email,
tags->'opening_hours' AS opening_hours
FROM
nodes
WHERE
tags->'amenity' IN ('pub','bar','nightclub','biergarten','cafe','restaurant')
AND
tags->'name' IS NOT NULL;
解决方法是:
SELECT
CASE WHEN 'email' = ANY (akeys(tags)) THEN 1 ELSE 0 END +
CASE WHEN 'opening_hours' = ANY (akeys(tags)) THEN 1 ELSE 0 END
AS count_tags
FROM
nodes
我尝试计算每一行,键在 hstore 数据列中的位置。
array_length(akeys(tags), 1) as num_keys
这适用于所有标签。
在 table nodes
中有更多不同的标签 - 我将只在我的 SELECT.
"name"=>"Campus", "amenity"=>"restaurant", "wheelchair"=>"yes"
仅计数 "name" -> 结果 1
SELECT
id,
st_x(ST_Transform(geom,4326)) AS lon,
st_y(ST_Transform(geom,4326)) AS lat,
array_length(akeys(tags), 1) as num_keys,
tags->'name' AS name,
tags->'amenity' AS amenity,
tags->'addr:street' AS street,
tags->'addr:housenumber' AS housenumber,
tags->'addr:postcode' AS postcode,
tags->'addr:city' AS city,
tags->'cuisine' AS cuisine,
tags->'beer_garden' AS beer_garden,
tags->'outdoor_seating' AS outdoor_seating,
tags->'smoking' AS smoking,
tags->'brewery' AS brewery,
tags->'website' AS website,
tags->'internet_access' AS wlan,
tags->'phone' AS phone,
tags->'email' AS email,
tags->'opening_hours' AS opening_hours
FROM
nodes
WHERE
tags->'amenity' IN ('pub','bar','nightclub','biergarten','cafe','restaurant')
AND
tags->'name' IS NOT NULL;
解决方法是:
SELECT
CASE WHEN 'email' = ANY (akeys(tags)) THEN 1 ELSE 0 END +
CASE WHEN 'opening_hours' = ANY (akeys(tags)) THEN 1 ELSE 0 END
AS count_tags
FROM
nodes