pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedReturn select statement with sql case statement
8+ messages / 5 participants
[nested] [flat]
* Return select statement with sql case statement
@ 2018-07-04 12:48 hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: hmidi slim @ 2018-07-04 12:48 UTC (permalink / raw)
To: pgsql-general
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.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
@ 2018-07-04 14:37 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Ron @ 2018-07-04 14:37 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
@ 2018-07-04 15:32 ` hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 16:17 ` Re: Return select statement with sql case statement legrand legrand <legrand_legrand@hotmail.com>
2018-07-04 22:03 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
0 siblings, 2 replies; 8+ messages in thread
From: hmidi slim @ 2018-07-04 15:32 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general
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
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
@ 2018-07-04 16:17 ` legrand legrand <legrand_legrand@hotmail.com>
1 sibling, 0 replies; 8+ messages in thread
From: legrand legrand @ 2018-07-04 16:17 UTC (permalink / raw)
To: pgsql-general
Hello,
sorry your description is not clear ...
why do you use a GROUP BY on product without aggregation function min, max,
sum ?
where is defined numberOfPremiumDays ?
may be using UNION can solve your problem:
select
numberOfPremiumDays,
product_id,
premium_price,
period_price
from product, PremiumDays
where occupation_type_id = 1
and numberOfPremiumDays = date_part('day',
('2018-11-05'::timestamp) - ('2018-11-01'::timestamp))
UNION ALL
select
numberOfPremiumDays,
product_id,
classic_price,
period_price
from product1, PremiumDays
where occupation_type_id = 1
and numberOfPremiumDays != date_part('day',
('2018-11-05'::timestamp) - ('2018-11-01'::timestamp))
Regards
PAscal
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
@ 2018-07-04 22:03 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 22:08 ` Re: Return select statement with sql case statement Adrian Klaver <adrian.klaver@aklaver.com>
1 sibling, 1 reply; 8+ messages in thread
From: Ron @ 2018-07-04 22:03 UTC (permalink / raw)
To: pgsql-general
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
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 22:03 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
@ 2018-07-04 22:08 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-04 22:18 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Adrian Klaver @ 2018-07-04 22:08 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; pgsql-general
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:(
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 22:03 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 22:08 ` Re: Return select statement with sql case statement Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-04 22:18 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 23:16 ` Re: Return select statement with sql case statement David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Ron @ 2018-07-04 22:18 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; 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.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Return select statement with sql case statement
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` Re: Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 22:03 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
2018-07-04 22:08 ` Re: Return select statement with sql case statement Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-04 22:18 ` Re: Return select statement with sql case statement Ron <ronljohnsonjr@gmail.com>
@ 2018-07-04 23:16 ` David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 8+ messages in thread
From: David G. Johnston @ 2018-07-04 23:16 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general
On Wednesday, July 4, 2018, Ron <ronljohnsonjr@gmail.com> wrote:
>
> Ah, didn't notice that. Then... dynamic sql constructed by the
> programming language executing the query?
>
That, the UNION idea, or pull the common stuff into the from clause and
write two left joins then coalesce whichever one provided the row. In
short, the OP cannot do what they thought they needed to do but hasn't
really provided any info for others to make alternative suggestions.
David J.
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2018-07-04 23:16 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-04 12:48 Return select statement with sql case statement hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 14:37 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 15:32 ` hmidi slim <hmidi.slim2@gmail.com>
2018-07-04 16:17 ` legrand legrand <legrand_legrand@hotmail.com>
2018-07-04 22:03 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 22:08 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-04 22:18 ` Ron <ronljohnsonjr@gmail.com>
2018-07-04 23:16 ` David G. Johnston <david.g.johnston@gmail.com>
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