I'm trying to use NULLIF to avoid division by zero error when using the over window function of postgres.
It throws "syntax error at or near "over"."
I'm using this query:
SELECT
dlo.nombre_cadena, dpr.marca_producto,
sum(rep.mes_33_uni) / NULLIF(sum(sum(rep.mes_33_uni)), 0) over (partition by dpr.marca_producto) AS porcentajep,
FROM
rpro_dim.reporte33_full rep
JOIN rpro_dim.dim_productos_retail dpr ON dpr.id_dim_prod_retail = rep.id_dim_prod_retail
JOIN rpro_dim.dim_locales dlo ON dlo.id_dim_local = rep.id_dim_localgroup by
dlo.nombre_cadena, dpr.marca_producto
order by dlo.nombre_cadena, dpr.marca_producto
With
NULLIF(sum(sum(rep.mes_33_uni)), 0) over (partition by dpr.marca_producto) -- error!
you are trying to execute nullif()
as a window function.
You should put the whole expression as an argument for nullif()
:
NULLIF(sum(sum(rep.mes_33_uni)) over (partition by dpr.marca_producto), 0)
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.