0

How can i shorten the following select statement?

create or replace view V_AMV_PLG_QUOTES_50_MS as 
select 
    sum(NVL(ASK_SIZE,0))    as ASK_VOLUME, 

CASE WHEN 
          sum(CASE WHEN NVL(ASK_SIZE,0)=0 THEN CASE WHEN BASE_CURR_ASK_PRICE=0 THEN 0 ELSE 1 END ELSE ASK_SIZE END)=0 THEN 1 
     ELSE sum(CASE WHEN NVL(ASK_SIZE,0)=0 THEN CASE WHEN BASE_CURR_ASK_PRICE=0 THEN 0 ELSE 1 END ELSE ASK_SIZE END) END     as ASK_PRICE,


    EXCHANGE_SK         as EXCHANGE_SK, 
    PRODUCT_SK          as PRODUCT_SK, 
    BUSINESS_DATE       as BUSINESS_DATE 
from 
    S_AMV_PLG_QUOTE_AGG quotes 
where NVL(BASE_CURR_ASK_PRICE ,0) > 0
group by 
    EXCHANGE_SK, 
    PRODUCT_SK, 
    BUSINESS_DATE 

Notice the very long calculation appears twice.. Can i insert it into a variable somehow? And just use the CASE operator on the Variable? (Notice we have a sum function there..)

6
  • Notice I want to sum all this long thing, And if the sum equals 0 i want to use 1, otherwise use the result of the sum.. Commented May 11, 2015 at 13:55
  • 1
    you should be able to use a DECODE for that kind of logic. are you worried about performance or code maintenance or what?
    – Randy
    Commented May 11, 2015 at 14:00
  • Code maintenance.. Looks awefull the original sum query is much longer i removed the most of it... Commented May 11, 2015 at 14:17
  • Note that sum(NVL(ASK_SIZE,0)) is better expressed as Coalesce(Sum(ask_size),0) Commented May 11, 2015 at 14:40
  • Also, "NVL(BASE_CURR_ASK_PRICE ,0) > 0" is the same as "BASE_CURR_ASK_PRICE > 0" Commented May 11, 2015 at 14:41

2 Answers 2

0

You can't use create and use a variable within a view. If you just want to avoid repeating the nested case statement, you could move it (and the NVL) into an in-line view:

select 
    sum(MY_ASK_SIZE)    as ASK_VOLUME, 
    CASE WHEN sum(MY_ASK_PRICE)=0 THEN 1 
        ELSE sum(MY_ASK_PRICE)
        END             as ASK_PRICE,
    EXCHANGE_SK         as EXCHANGE_SK, 
    PRODUCT_SK          as PRODUCT_SK, 
    BUSINESS_DATE       as BUSINESS_DATE 
from (
    select
        EXCHANGE_SK, PRODUCT_SK, BUSINESS_DATE,
        NVL(ASK_SIZE,0) as MY_ASK_SIZE,
        CASE WHEN NVL(ASK_SIZE,0)=0 THEN
                CASE WHEN BASE_CURR_ASK_PRICE=0 THEN 0 ELSE 1 END
            ELSE ASK_SIZE
            END AS MY_ASK_PRICE
    from  S_AMV_PLG_QUOTE_AGG quotes 
    where NVL(BASE_CURR_ASK_PRICE ,0) > 0
)
group by 
    EXCHANGE_SK, 
    PRODUCT_SK, 
    BUSINESS_DATE;

The NVL and case are performed once, and aliased as MY_ASK_SIZE and MY_ASK_PRICE respectively; and those aliases are then used in the outer query, including within the sum.

If you want to avoid repeating the SUM too, move that into the in-line view as well, along with the group-by clause:

select 
    ASK_VOLUME          as ASK_VOLUME, 
    CASE WHEN ASK_PRICE=0 THEN 1
        ELSE ASK_PRICE
        END             as ASK_PRICE,
    EXCHANGE_SK         as EXCHANGE_SK, 
    PRODUCT_SK          as PRODUCT_SK, 
    BUSINESS_DATE       as BUSINESS_DATE 
from (
    select
        EXCHANGE_SK, PRODUCT_SK, BUSINESS_DATE,
        SUM(NVL(ASK_SIZE,0)) as ASK_VOLUME,
        SUM(CASE WHEN NVL(ASK_SIZE,0)=0 THEN
                CASE WHEN BASE_CURR_ASK_PRICE=0 THEN 0 ELSE 1 END
            ELSE ASK_SIZE
            END) AS ASK_PRICE
    from  S_AMV_PLG_QUOTE_AGG quotes 
    where NVL(BASE_CURR_ASK_PRICE ,0) > 0
    group by 
        EXCHANGE_SK, 
        PRODUCT_SK, 
        BUSINESS_DATE
);

Most of your column aliases are redundant but I've stuck with your pattern.

This is largely cosmetic though; Oracle's optimiser is likely to be clever enough to only perform the actual calculation once anyway, so I wouldn't expect to see much (or any) performance difference.

0

I used creation of a simple ifZeroReturnOne function:

CREATE OR REPLACE FUNCTION ifZeroReturnOne (expr IN NUMBER)
RETURN NUMBER
IS retval NUMBER;
BEGIN
SELECT CASE WHEN expr=0 THEN 1
ELSE expr END
INTO retval
FROM DUAL;
RETURN (retval);
END;

Than I used in the query:

select sum(NVL(ASK_SIZE,0))    as ASK_VOLUME, 

ifZeroReturnOne (sum(CASE WHEN NVL(ASK_SIZE,0)=0 THEN CASE WHEN BASE_CURR_ASK_PRICE=0 THEN 0 ELSE 1 END ELSE ASK_SIZE END)    as ASK_PRICE,


EXCHANGE_SK         as EXCHANGE_SK, 
etc...............

Let me know if you have better suggestions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.