pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Return select statement with sql case statement
Date: Wed, 4 Jul 2018 09:37:04 -0500
Message-ID: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com> (raw)
In-Reply-To: <CAMsqVxuBE0iHU=-_LCS2PyonGFvJwgrs6LLrzuGU53m7VqYBQA@mail.gmail.com>
References: <CAMsqVxuBE0iHU=-_LCS2PyonGFvJwgrs6LLrzuGU53m7VqYBQA@mail.gmail.com>

On 07/04/2018 07:48 AM, hmidi slim wrote:
> Hi,
> I need to use conditional expression in my query, So I want to make a 
> query like this:
> select numberOfPremiumDays
>             case  when numberOfPremiumDays = date_part('day', 
> ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then
>                 select product_id,
>                 price
>                 from product
>                 where occupation_type_id = 1
>                 group by product_id, occupation_type_id
>
> However, in the documentation I found that the return of case was a value, 
> not like in my case I want to return a select statement.
> How can I use a conditional expression in a sql query?
> Best Regards.

The CASE clause is used to return one of many choices.  Based on this 
example, you need to do this:

select numberOfPremiumDays,
        product_id,
        price
        from product
where occupation_type_id = 1
   and  numberOfPremiumDays = date_part('day', ('2018-11-05'::timestamp) - 
('2018-11-01'::timestamp))
group by product_id, occupation_type_id



-- 
Angular momentum makes the world go 'round.




view thread (8+ messages)  latest in thread

Message-ID: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com>
Permalink:  ../3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com/
Also on:    postgresql.org/message-id/3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: Return select statement with sql case statement
  In-Reply-To: <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.com>
  References: <CAMsqVxuBE0iHU=-_LCS2PyonGFvJwgrs6LLrzuGU53m7VqYBQA@mail.gmail.com> <3524c35d-e566-9bad-e4bd-d493f05f2d4d@gmail.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