I have a query that returns avg(price)
select avg(price)
from(
select *, cume_dist() OVER (ORDER BY price desc) from web_price_scan
where listing_Type='AARM'
and u_kbalikepartnumbers_id = 1000307
and (EXTRACT(Day FROM (Now()-dateEnded)))*24 < 48
and price>( select avg(price)* 0.50
from(select *, cume_dist() OVER (ORDER BY price desc)
from web_price_scan
where listing_Type='AARM'
and u_kbalikepartnumbers_id = 1000307
and (EXTRACT(Day FROM (Now()-dateEnded)))*24 < 48
)g
where cume_dist < 0.50
)
and price<( select avg(price)*2
from( select *, cume_dist() OVER (ORDER BY price desc)
from web_price_scan
where listing_Type='AARM'
and u_kbalikepartnumbers_id = 1000307
and (EXTRACT(Day FROM (Now()-dateEnded)))*24 < 48
)d
where cume_dist < 0.50)
)s
having count(*) > 5
how to make it return 0 if no value is available?
use coalesce
Edit
Here’s an example of
COALESCEwith your query:IMHO
COALESCEshould not be use withAVGbecause it modifies the value.NULLmeans unknown and nothing else. It’s not like using it inSUM. In this example, if we replaceAVGbySUM, the result is not distorted. Adding 0 to a sum doesn’t hurt anyone but calculating an average with 0 for the unknown values, you don’t get the real average.In that case, I would add
price IS NOT NULLinWHEREclause to avoid these unknown values.