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' })
  }
原文地址:https://www.cnblogs.com/antyhouse/p/13262041.html