GitHub user wgmayer0 edited a comment on the discussion: Simplify this query 
and remove redundancy

Here's my solution:
```
SELECT jsonb_strip_nulls(
    jsonb_build_object(
        CASE WHEN a_label IS NOT NULL THEN a_label ELSE '' END, a_value,
        CASE WHEN b_label IS NOT NULL THEN b_label ELSE '' END, b_value,
        CASE WHEN c_label IS NOT NULL THEN c_label ELSE '' END, c_value,
        CASE WHEN d_label IS NOT NULL THEN d_label ELSE '' END, d_value,
        CASE WHEN e_label IS NOT NULL THEN e_label ELSE '' END, e_value,
        CASE WHEN f_label IS NOT NULL THEN f_label ELSE '' END, f_value
    )
) AS result
FROM cypher('hermech', $$
  MATCH (a:contact_name)
  WHERE id(a) = 10133099161583644
  OPTIONAL MATCH (a)-[:DIRECT_EMAIL_ADDRESS]->(b:direct_email_address)
  OPTIONAL MATCH (a)<-[:CONTACT_NAME]-(c:account_name)
  OPTIONAL MATCH (c)-[:MAIN_PHONE_NUMBER]->(d:main_phone_number)
  OPTIONAL MATCH (c)-[:MAIN_STREET_ADDRESS]->(e:main_street_address)
  OPTIONAL MATCH (c)-[:MAIN_DOMAIN]->(f:main_domain)
  RETURN
    head(labels(a)) AS a_label, a.value AS a_value,
    head(labels(b)) AS b_label, b.value AS b_value,
    head(labels(c)) AS c_label, c.value AS c_value,
    head(labels(d)) AS d_label, d.value AS d_value,
    head(labels(e)) AS e_label, e.value AS e_value,
    head(labels(f)) AS f_label, f.value AS f_value
$$) AS (
    a_label text, a_value text,
    b_label text, b_value text,
    c_label text, c_value text,
    d_label text, d_value text,
    e_label text, e_value text,
    f_label text, f_value text
);
```
If anyone can think of ways to simplify it that would be nice. I am essentially 
writing the values I want in 4 different places which seems a little 
ridiculous. As for my version: ```                                              
         version
---------------------------------------------------------------------------------------------------------------------
 PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by 
gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)``` `return pg_catalog.json_build_object(` might be nice if I could get 
it to work. Maybe I will try it in ChatGPT since I am doing something wrong

GitHub link: 
https://github.com/apache/age/discussions/2226#discussioncomment-14568482

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to