我有一个查询返回avg(price)0 A' F* h% t1 x5 C ?
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?0 Q, a a0 ? r9 n& x
% B: m/ V0 F1 ?+ G7 S2 B7 E% P& A解决方案: + I9 o( A1 ]' J: q: x9 i1 X
使用合并 * x# _- n0 N, b1 z v3 g5 l* rCOALESCE(value [,...])The COALESCE function returns the first of its arguments that is notnull. . h8 \' _7 h3 r Null is returned only if all arguments are null. It is often, Y# b5 ~+ t9 f8 Z$ C# @& Y
used to substitute a default value for null values when data is' w! Q! T8 p- q) m0 i
retrieved for display.: ~5 J# i5 U7 R, h 编辑- |9 t& Q0 _( W
这是COALESCE查询示例:. ?! s( J( n' a& T* S
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时,你不会得到真正的平均值。 ; [, @$ v. ~! o! F7 J9 C在那种情况下,我将添加price IS NOT NULLinWHERE避免这些未知值。