Table : incident
----------------
incident_id usr_id item_id Inc_Date
10059926 191 61006 8-22-2015
10054444 222 3232 6-7-2015
Table: act_reg
--------------
act_reg_id act_type_id incident_id usr_id act_type_sc
454244 1 10059926 191 ASSIGN
471938 115 10059926 191 TRAVEL TIME
473379 40 10059926 191 FOLLOW UP
477652 115 10059926 191 TRAVEL TIME
489091 504 10059926 191 ADD_ATTCHMNTS
477653 504 10054444 222 ADD_ATTCHMNTS
Parameter: @attach (value=1, Label=Yes & Value=0, Label=No)
Result (While I am selecting 'Yes' in dropdown)
----------------------------------------------
incident_id usr_id item_id
10059926 191 61006
10054444 222 3232
My Query:
SELECT incident.incident_id,incident.usr_id,incident.item_id
FROM incident
where exists (select * from act_reg
where incident.incident_id = act_reg.incident_id
and act_reg.act_type_sc (case when @attach=1 and act_reg.act_type_sc='ADD_ATTCHMNTS' then NULL else act_reg.act_type_sc end )
)
Please help me on this.
You should change your query to the following :-
SELECT incident.incident_id,incident.usr_id,incident.item_id
FROM incident
left join act_reg
on incident.incident_id = act_reg.incident_id and act_reg.act_type_sc = 'ADD_ATTCHMNTS'
where
((@attach = 1 and act_reg.act_reg_id is not null) or
(@attach = 0 and act_reg.act_reg_id is null))
group by incident.incident_id,incident.usr_id,incident.item_id