sql怎么在某個判斷條件下去計算總和


比如這里我需要判斷時間>0時計算所有得去重后的id的總和

這一行:

count(distinct(case when (t.pay_time > 0) then t.user_id else null end)) AS buyerQuality 

完整代碼:

 async rechargeSum(time) {
    // orderSumQuality是所有的訂單數(包括已支付和未支付)
    return await this.app.model.query(
      `select count(1) 'orderSumQuality',
      IFNULL(sum(case when t.pay_time > 0 then 1 else 0 end), 0) AS orderQuality,
      IFNULL(sum(case when t.pay_time > 0 then t.income else 0 end), 0) AS realGoldCoin,
      IFNULL(sum(case when t.pay_time > 0 then t.extra_income else 0 end), 0) AS presentGoldCoin,
      IFNULL(sum(case when t.pay_time > 0 then t.order_amount else 0 end), 0) AS orderTotolSum,
      IFNULL(sum(case when t.pay_time > 0 then t.amount else 0 end), 0) AS amountReceived,
      count(distinct(case when (t.pay_time > 0) then t.user_id else null end)) AS buyerQuality 
      FROM hx_user_recharge_record AS t ${time}`,
      { type: 'SELECT' })
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM