Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dgBue-0001pX-Cr for pgsql-sql@arkaria.postgresql.org; Fri, 11 Aug 2017 15:31:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dgBud-0001gK-Nq for pgsql-sql@arkaria.postgresql.org; Fri, 11 Aug 2017 15:31:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dgBtd-0008LK-27 for pgsql-sql@postgresql.org; Fri, 11 Aug 2017 15:30:45 +0000 Received: from sender-of-o51.zoho.com ([135.84.80.216]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dgBtZ-00016X-Cw for pgsql-sql@postgresql.org; Fri, 11 Aug 2017 15:30:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1502465435; s=zoho; d=lightpear.com; i=jonathan-lists@lightpear.com; h=Date:From:To:Cc:Message-Id:In-Reply-To:References:Subject:MIME-Version:Content-Type; l=4415; bh=4FWfjka8BhqE1b0pjy6QdcQidRTWQMkpWXvCLdH5iv4=; b=MOYn6NQ09GB7fiMeaqNdzFa7oGDRBQc33E+yz8mIw09sXRQIgY+8cCeeujNswVYp wur4knN7Ans01XRIHMgO3SGImVm1qXeiBxqnl8nw5vlq5NnR3c71IsMTLAjzvZ72/dx 7qr+KKJ+EhjGsDlyApwQQlj8JjFXC/hj6MtTeR+4= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1502465435630801.5369306346902; Fri, 11 Aug 2017 08:30:35 -0700 (PDT) Date: Fri, 11 Aug 2017 16:30:35 +0100 From: Jonathan Moules To: Achilleas Mantzios Cc: Message-Id: <15dd1ea396c.10c78e843106370.6070220804472515174@lightpear.com> 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> Subject: Re: Always getting back a row, even with no results MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_253596_1592296799.1502465374575" X-Priority: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org ------=_Part_253596_1592296799.1502465374575 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit 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)>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 ) > 0 THEN ( select count(*) from my_table where cat=2 ) ELSE 1 END; ------=_Part_253596_1592296799.1502465374575 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =

Do smth like along the lines (might want to add additiona= l code for ordering, this just happens to run correctly) :
select * from my_table where cat= =3D 500 UNION select null,null LIMIT CASE WHEN (select count(*) from m= y_table where cat=3D500)>0 THEN (select count(*) from my_table where= cat=3D500) ELSE 1 END ;
Of course you can write a function to do t= hat 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 sho= uld be the simplest place to solve this.

I tried your example; it se= ems 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 =3D 2
 
UNION
 
select<= br>            null = as id
ORDER BY
         =    id ASC
LIMIT
    CASE
  &nbs= p;         WHEN (
     = ;           select
  &= nbsp;               =       count(*)
       =         from
     &nb= sp;               &n= bsp;  my_table
          &n= bsp;     where
        &nbs= p;               cat= =3D 2
             &n= bsp;  ) > 0
          &n= bsp; THEN (
            &nb= sp;   select count(*) from my_table where cat=3D2
  =               )
 =            ELSE
  &nbs= p;             1
 &nbs= p;  END;
------=_Part_253596_1592296799.1502465374575--