sql - query for a scenario using other then Case statement -
i have table t1, columns custid, customername, q1, q2, q3, q4, deptid. , custid values 1,2,3...so on.
but q1, q2, q3, q4 values either 'y' or 'n' or null.
total table t1 is:
custid custname q1 q2 q3 q4 deptid 1 john y null null null 2 2 sammy n n y y 1 3 sameer n n y n 2 .... now when question 1 i.e field q1 =y q2, q3, q4 not asked null, , customer go deptid = 2.
- i.e if
q1= ydeptid= 2. - if
q1= n ,q2= ydeptid= 2. - if
q1= n ,q2= n ,q3= y ,q4= ndeptid= 2. - if
q1= n ,q2= n ,q3= y ,q4= ydeptid= 1. - if
q1= n ,q2= n ,q3= n message should display '' customer not assigned department ''.
note: if q1=n , q2=n , q3=n printing message not necessary
now want check along if given values in table exact , matching
deptid
i have used below code:
select p.custid (select custid,q1,q2,q3,q4, case when q1='y' 2 when q1='n' , q2='y' 2 when q1='n' , q2='n' , q3='y' , q4='n' 2 when q1='n' , q2='n' , q3='y' , q4='y' 1 when q1='n' , q2='n' , q3='n' 0 end deptid,deptassigned t1 ) p p.deptid<>p.deptassinged please me different query's , let me know if changes in above code
Comments
Post a Comment