I have two tables on which i have performed inner join. I have a scenario where i need to fetch values from the first table based on the two conditions.
My query is
SELECT rm.ROUTE_ID
, rm.ROUTE_CODE
, rm.START_PLACE_ID
, rm.END_PLACE_ID
, rm.IS_ACTIVE
, rm.LINKED_ROUTE
FROM OPRS_ROUTE_MASTER rm
INNER JOIN OPRS_ROUTE_HALTS rh
ON rh.ROUTE_ID = rm.ROUTE_ID
WHERE rh.PLACE_ID = '51'
now i need to have two more conditions in the where clause.
case 1: when rm.START_PLACE_ID > 0 then i need to append AND rm.START_PLACE_ID to where clause
case 2: when rm.END_PLACE_ID > 0 then i need to append AND rm.START_PLACE_ID to where clause
if both are greater than zero then i need to append both the cases to the where clause.
Can some one please help me
I’d set the query up something like this:
If the table row has a positive value in the desired column (
START_PLACE_IDorEND_PLACE_ID), the column is compared to the desired value, otherwise it’s compared to itself.This avoids the use of alternation (the
ORoperator): usingORcan degrade the execution plan, forcing a table or index scan rather than a seek.