pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jonathan Moules <jonathan-lists@lightpear.com>
To: Achilleas Mantzios <achill@matrix.gatewaynet.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Always getting back a row, even with no results
Date: Fri, 11 Aug 2017 16:30:35 +0100
Message-ID: <15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com> (raw)
In-Reply-To: <893edec6-fc48-2635-aceb-11231c276424@matrix.gatewaynet.com>
References: <15dd19520d2.c2f4c716103038.4559674295652037989@lightpear.com>
	<893edec6-fc48-2635-aceb-11231c276424@matrix.gatewaynet.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>


Do smth like along the lines (might want to add additional code for ordering, this just happens to run correctly) :
 select * from my_table where cat = 500 UNION select null,null LIMIT CASE WHEN (select count(*) from my_table where cat=500)&gt;0 THEN (select count(*) from my_table where cat=500) ELSE 1 END ;
 Of course you can write a function to do that for you, but what made you want this in the first place? Maybe this is bad design ?



Unfortunately this is a constraint of the environment; not good design, but SQL seems like it should be the simplest place to solve this.

I tried your example; it seems the ordering is required, otherwise the results always include the null. So the below works for anyone who finds this in the future. Thanks


select
            id
from
            my_table
where
            cat = 2
 
UNION
 
select
            null as id
ORDER BY
            id ASC
LIMIT
    CASE
            WHEN (
                select
                        count(*)
                from
                        my_table
                where
                        cat = 2
                ) &gt; 0
            THEN (
                select count(*) from my_table where cat=2
                )
            ELSE
                1
    END;

view thread (9+ messages)  latest in thread

Message-ID: <15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com>
Permalink:  ../15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com/
Also on:    postgresql.org/message-id/15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jonathan-lists@lightpear.com, achill@matrix.gatewaynet.com
  Subject: Re: Always getting back a row, even with no results
  In-Reply-To: <15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox