数据库
首页 > 数据库> > SQL连表更新多个平均值字段

SQL连表更新多个平均值字段

作者:互联网

UPDATE res2_product AS a LEFT JOIN (SELECT
    pro_id,
    IF(
        SUM( transaction_date > DATE_SUB( CURRENT_DATE, INTERVAL 8 DAY ) ), 
        SUM( IF ( transaction_date > DATE_SUB( CURRENT_DATE, INTERVAL 8 DAY ), transaction_quantity, 0 ) ) / SUM( transaction_date > DATE_SUB( CURRENT_DATE, INTERVAL 8 DAY ) ),
        0)
         AS avg_stock_7,
    AVG( transaction_quantity ) AS avg_stock_30 
FROM
    get2_transaction_daily 
WHERE
    transaction_date > DATE_SUB( CURRENT_DATE, INTERVAL 31 DAY ) GROUP BY pro_id) AS b ON a.pro_id = b.pro_id SET a.avg_stock_7 = IFNULL(b.avg_stock_7, 0), a.avg_stock_30 = IFNULL(b.avg_stock_30, 0)

标签:transaction,平均值,pro,连表,SUB,SQL,DATE,avg,stock
来源: https://blog.csdn.net/chengxuyuanlaow/article/details/121648842