0

I've got the following table (nodes) in a PostgreSQL DB and I'm looking to extract either the http OR https information. I am using COALESCE.

SELECT COALESCE(http,https) 
FROM oig.nodes 
WHERE owner_name = %s 
  AND node_type = COALESCE('full','api')
|---------------------|------------------|------------------|------------------|
|      owner          |     node_type    |       http       |      https       |
|---------------------|------------------|------------------|------------------|
|          test123    |         full     | http://1.1.1.1   |                  |
|---------------------|------------------|------------------|------------------|
|          testabc    |         api      |                  | http://1.1.1.2   |
|---------------------|------------------|------------------|------------------|
|          testabc    |         seed     |                  |                  |
|---------------------|------------------|------------------|------------------|

This works, but only if the node_type = full, if the node_type = api, it returns None.

4
  • 1
    Make sure you have NULLs not empty '' string Commented Aug 11, 2020 at 8:43
  • 1
    Maybe it's not a null value, but an empty string (''). Try nullif(http_node_url, '') Commented Aug 11, 2020 at 8:43
  • I check definitely is NULL value. I forgot to mention that there could be two or more owners with the same name. Commented Aug 11, 2020 at 8:48
  • As you see now, "it returns None" is a bit vague. We thought that you get a result row in which the value is null. Now it seems you are not getting any row at all. Always describe what happens very precisely, so we don't have to guess. Commented Aug 11, 2020 at 9:20

1 Answer 1

2

COALESCE('full','api') is equivalent to 'full' because 'full' is definitely not null.

Sign up to request clarification or add additional context in comments.

8 Comments

yes works if owner = test123, but if owner = testabc, it returns None.
because none row matches AND condition. Didn't you rather mean AND node_type in ('full','api')?
just tried that and same issue - SELECT COALESCE(http_node_url,https_node_url),owner_name FROM oig.nodes WHERE owner_name = %s AND node_type in ('full','api')
weird... in can be broken to two expressions separated by OR. Perhaps some whitespace or parenthesising issue... (may be try to extract it to selfstanding query, your column names don't match those in your ascii-art table)
When I try SELECT COALESCE(http_node_url,https_node_url),owner_name FROM oig.nodes WHERE owner_name = %s AND node_type = 'full' OR node_type = 'api' - it then only returns the first row from the table no matter what owner_)name I pass.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.