Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to avoid repeat where clause in oracle sql

user10624672Jun 4 2013 — edited Jun 5 2013
Hi,

Please find my query below, I need a help to avoid duplication of **where** clause in my query.

In my below query, **JOIN** condition is same for both the queries and **WHERE** condition also same except this clause "and code.code_name="transaction_1"
In **IF ** condition only credit and debit is swapped on both queries, due to this **Credit and Debit** and this where clause "and code.code_name="transaction_1" I am duplicating the query. Can you please give some solution to avoid this duplication. I am using oracle 11g

select DAY as business_date,sum(amount) as AMOUNT, type_amnt as amount_type,test_code as code_seg
from
(
select table1_alias.date as DAY,code.code_numb as test_code,
CASE
WHEN qnty_item > 0 THEN 'credit'
ELSE 'debit'
END as type_amnt,
"25.55" as amount
from
code_table code,
table1 table1_alias
join table2 table2_alias on table1_alias.id = table2_alias.id
where
table1_alias.state="OK"
and table1_alias.type="R"
and code.code_type="Movie"
and code.code_name="transaction_1"
UNION ALL

select table1_alias.date as DAY,code.code_numb as test_code,
CASE
WHEN qnty_item > 0 THEN 'debit'
ELSE 'credit'
END as type_amnt,
"25.55" as amount
from
code_table code,
table1 table1_alias
join table2 table2_alias on table1_alias.id = table2_alias.id
where
table1_alias.state="OK"
and table1_alias.type="R"
and code.code_type="Movie"
and code.code_name="transaction_2"
)
group by DAY, test_code,type_amnt


Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 3 2013
Added on Jun 4 2013
14 comments
1,412 views