lovasoa / SQLpage

SQL-only webapp builder, empowering data analysts to build websites and applications quickly
https://sql.ophir.dev
MIT License
882 stars 62 forks source link

hide a map if no records produced #346

Closed amrutadotorg closed 1 month ago

amrutadotorg commented 1 month ago

Hi, it is possible not to generate a map if the query produces 0 records?

here is my example thank you

SELECT 
    'title' AS component,
    'map' AS id;

SELECT 
    'map' AS component,
    1 AS zoom,
    '' AS attribution;
SELECT
    (meta_value->>'title')::text AS title,
    (meta_value->>'description')::text AS description_md,
    (meta_value->>'latitude')::numeric AS latitude,
    (meta_value->>'longitude')::numeric AS longitude,
    'users' AS icon,
    20 AS size
FROM
    post_meta
WHERE
    post_id = $id::int
    AND meta_key = 'geo'
    AND meta_value->>'latitude' IS NOT NULL
    AND meta_value->>'longitude' IS NOT NULL;
lovasoa commented 1 month ago

Yes! You can add a where condition to the first two SELECT statements

amrutadotorg commented 1 month ago

awesome! it works now

SELECT 
    'title' AS component,
    'map' AS id
WHERE EXISTS (
    SELECT 1
    FROM post_meta
    WHERE post_id = $id::int
    AND meta_key = 'geo'
    AND meta_value->>'latitude' IS NOT NULL
    AND meta_value->>'longitude' IS NOT NULL
);

SELECT 
    'map' AS component,
    1 AS zoom,
    '' AS attribution
WHERE EXISTS (
    SELECT 1
    FROM post_meta
    WHERE post_id = $id::int
    AND meta_key = 'geo'
    AND meta_value->>'latitude' IS NOT NULL
    AND meta_value->>'longitude' IS NOT NULL
);

SELECT
    (meta_value->>'title')::text AS title,
    (meta_value->>'description')::text AS description_md,
    (meta_value->>'latitude')::numeric AS latitude,
    (meta_value->>'longitude')::numeric AS longitude,
    'users' AS icon,
    20 AS size
FROM
    post_meta
WHERE
    post_id = $id::int
    AND meta_key = 'geo'
    AND meta_value->>'latitude' IS NOT NULL
    AND meta_value->>'longitude' IS NOT NULL;