Re: Return select statement with sql case statement

From: Ron <ronljohnsonjr(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Return select statement with sql case statement
Date: 2018-07-04 22:18:33
Message-ID: bc9b1680-5e17-7dd5-8d33-3718ced6c664@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 07/04/2018 05:08 PM, Adrian Klaver wrote:
> On 07/04/2018 03:03 PM, Ron wrote:
>> On 07/04/2018 10:32 AM, hmidi slim wrote:
>>> Actually, I need the use of case because based on the
>>> numberOfPremiumDays there are different type of treatment:
>>> select numberOfPremiumDays
>>>             case  when numberOfPremiumDays = date_part('day',
>>> ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then
>>>                 select product_id,
>>>                 premium_price,
>>>                 period_price
>>>                 from product
>>>                 where occupation_type_id = 1
>>>                 group by product_id, occupation_type_id
>>>            else
>>>                 select product_id,
>>>                 classic_price,
>>>                 period_price
>>>                 from product1
>>>                 where occupation_type_id = 1
>>>                 group by product_id, occupation_type_id
>>>
>>
>> Then try:
>> select product_id,
>> case when numberOfPremiumDays = date_part('day',
>> ('2018-11-05'::timestamp) - ('2018-11-01'::timestamp)) then
>>             premium_price
>>         else
>>             period_price
>>         end as the_price
>> from product
>> where occupation_type_id = 1
>> order by product_id, occupation_type_id
>
> The issue with the above is that table changes from product to product1 in
> the OP's desired behavior so the price switch alone will not work:(

Ah, didn't notice that.  Then... dynamic sql constructed by the programming
language executing the query?

--
Angular momentum makes the world go 'round.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Melvin Davidson 2018-07-04 22:38:13 Re: Cloning schemas
Previous Message Adrian Klaver 2018-07-04 22:08:37 Re: Return select statement with sql case statement