Home > Blockchain >  Postgresql - execute only certain IF statements
Postgresql - execute only certain IF statements

Time:11-10

I would like to put toghether the reslt of some queries by using UNION. But I want to execute only some of the queries... in the sample below the part ('Daily','Yearly') will be filled in automatically.

something along the lines of:

(if 'Daily' in ('Daily','Yearly') then
my first query goes here...
end if)
UNION ALL
(if 'Monthly' in ('Daily','Yearly') then
my second query goes here...
end if)
UNION ALL
(if 'Yearly' in ('Daily','Yearly') then
my third query goes here...
end if)

In this sample, the result of the second query should not be present in the output.

on a side note: I do already have the 3 working queries, what i am missing is how to execute them dynamically and put them toghether to a single output.

CodePudding user response:

Try to put these conditions in the appropriate "Where" sections. Engine have to skip queries if the literal conditions are not met.

my first query goes here...
Where 'Daily' in ('Daily','Yearly')
UNION ALL
my second query goes here...
Where 'Monthly' in ('Daily','Yearly')
UNION ALL
my third query goes here...
Where 'Yearly' in ('Daily','Yearly')
  • Related