postgresql return 0 if returned value is null

后端 未结 3 1220
失恋的感觉
失恋的感觉 2021-01-30 12:32

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         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 12:51

    I can think of 2 ways to achieve this:

    • IFNULL():

      The IFNULL() function returns a specified value if the expression is NULL.If the expression is NOT NULL, this function returns the expression.

    Syntax:

    IFNULL(expression, alt_value)

    Example of IFNULL() with your query:

    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 IFNULL( price, 0 ) > ( SELECT AVG( IFNULL( price, 0 ) )* 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 IFNULL( price, 0 ) < ( SELECT AVG( IFNULL( price, 0 ) ) *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
    
    • COALESCE()

      The COALESCE() function returns the first non-null value in a list.

    Syntax:

    COALESCE(val1, val2, ...., val_n)

    Example of COALESCE() with your query:

    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 COALESCE( price, 0 ) > ( SELECT AVG( COALESCE( price, 0 ) )* 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 COALESCE( price, 0 ) < ( SELECT AVG( COALESCE( price, 0 ) ) *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
    

提交回复
热议问题