我有一个查询返回avg(price) R3 A4 f8 h! M( I* F' l6 }
select avg(price) from( select *,cume_dist() OVER (ORDER BY price desc) from web_price_scan where listing_Type='AARM and u_kbalikepartnumbers_id = and (EXTRACT(Day FROM (Now()-dateEnded)))*24 ( select avg(price)* 0.505050 from(select *,cume_dist() OVER (ORDER BY price desc) from web_price_scan where listing_Type='AARM and u_kbalikepartnumbers_id = and (EXTRACT(Day FROM (Now()-dateEnded)))*24 5如果没有可用值,如何返回0? ) @! k/ q9 x$ H7 k6 T ) F, u* X! G/ m, L解决方案: 3 U/ R4 _/ V' d5 ]. s$ r
使用合并 / `1 Z' ^2 q% Q4 ]4 XCOALESCE(value [,...])The COALESCE function returns the first of its arguments that is notnull.% p* I8 E% S. m7 v; p; K
Null is returned only if all arguments are null. It is often/ K$ g9 \' E4 B. [
used to substitute a default value for null values when data is' S! P0 X# e- A& ?' |/ K
retrieved for display.& t+ G$ I% @2 S( `( Q8 ] 编辑! t, X' G* o3 w1 D
这是COALESCE查询示例:! A9 h! \- {/ H5 |. S" |3 Y1 s; I4 r; }
SELECT AVG( price )FROM( SELECT *,cume_dist() OVER ( ORDER BY price DESC ) FROM web_price_scan WHERE listing_Type = 'AARM AND u_kbalikepartnumbers_id = AND ( EXTRACT( DAY FROM ( NOW() - dateEnded ) ) * 24 ( SELECT AVG( COALESCE( price,0 ) )* 0.505050 FROM ( SELECT *,cume_dist() OVER ( ORDER BY price DESC ) FROM web_price_scan WHERE listing_Type='AARM AND u_kbalikepartnumbers_id = AND ( EXTRACT( DAY FROM ( NOW() - dateEnded ) ) * 24 5IMHOCOALESCE不应与之一起使用,AVG因为它会修改值。NULL这意味着未知,没有别的。这不像在里面用。SUM。在这个例子中,如果我们替换它,AVG为SUM,结果不会扭曲。总和加0不会伤害任何人,但当未知值计算为0时,你不会得到真正的平均值。- c) K9 c$ d5 k' o
在那种情况下,我将添加price IS NOT NULLinWHERE避免这些未知值。