How to avoid repeat where clause in oracle sql [message #586243] |
Tue, 04 June 2013 14:53 |
|
murman
Messages: 1 Registered: June 2013 Location: bangalore
|
Junior Member |
|
|
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
*BlackSwan added {code} tags & formatted SQL. do so yourself in the future.
[Updated on: Tue, 04 June 2013 14:59] by Moderator Report message to a moderator
|
|
|
|
|