简体   繁体   中英

Changing a CASE function to a DECODE function in Oracle SQL

SELECT 
        SUM (
            CASE
                WHEN pop.usertype=1 THEN pop.useramount
                WHEN pop.usertype=2 THEN 0-pop.useramount
            END )        
    FROM
        popular pop
;

How would I translate this statement into the DECODE function?

I've tried this (and other slight variations) - but I seem to always get a different result..

SELECT 
        SUM (
            DECODE(pop.usertype,'1', 'pop.useramount',
                                '2', '0-pop.useramount')
            )        
    FROM
        popular pop
;

I'm trying to understand the DECODE function better, but I'm really confused as to why this doesn't work?

Try to remove ' as below

SELECT 
        SUM (
            DECODE(pop.usertype,'1', pop.useramount,
                                '2', 0-pop.useramount)
            )        
    FROM
        popular pop
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM