简体   繁体   中英

NVL function in Oracle

I'm using Oracle and I want to replace null values with 0 but it does not work. This is my query:

with pivot_data as (
  select (NVL(d.annulation,0)) as annulation ,
    d.id_cc1_annulation as nature ,
    t.mois as mois
  from datamart_cnss d , ref_temps t 
  where t.id_temps = d.id_temps
)
select * from pivot_data
PIVOT ( sum(annulation)
        for nature in (2 as debit ,1 as redit) 
) 
order by mois asc;

I guess (because of missing example), your idea is to show no nulls in the result after pivoting. If so, your query (I guess) doesn't always return both nature values 1 and 2.

The NVL operator in this case works fine, but you put it in a wrong place. This is the place which generates your NULL because of no rows found for the given criteria:

PIVOT ( sum(annulation)

If you enhance this sum result with the love of NVL - I am pretty sure it will work as you expect.

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