If else Condition in PostgreSql

103 views Asked by At

I have Query which need to be modified like if I pass zone=All then it should display all records else other condition. This query I have to use in SSRS Reports, it works in this condition.

select distinct zone, wardno,deptname, 
       cybertec_view_requests.statusname, 
       categoryname,opendate,probdesc 
from cybertec_view_requests
left join cybertec_view_requests_comments on 
cybertec_view_requests_comments.reqid = cybertec_view_requests.id_
where zone= ? and  deptname=? and
opendate between  ? and  ?  and categoryname=?
  group by zone,wardno,deptname, cybertec_view_requests.statusname,
           categoryname,opendate,probdesc
  order by zone,wardno,deptname
1

There are 1 answers

3
Hiral Nayak On

try this

    Hear @zone is your variable to pass in the Query 

    SELECT DISTINCT zone, wardno,deptname, cybertec_view_requests.statusname, categoryname,opendate,probdesc 
    FROM            cybertec_view_requests
    LEFT JOIN       cybertec_view_requests_comments ON cybertec_view_requests_comments.reqid = cybertec_view_requests.id_
    WHERE           zone= CASE WHEN @zone = 'ALL' THEN zone ELSE @zone END and  deptname=? and
                    opendate between  ? and  ?  and categoryname=?
    GROUP BY        zone,wardno,deptname, cybertec_view_requests.statusname,
                    categoryname,opendate,probdesc
    ORDER BY        zone,wardno,deptname