zoukankan      html  css  js  c++  java
  • 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' })
      }
    
  • 相关阅读:
    node中fs模块
    node生成excel,动态替换表格内容
    Postgresql存放数组形式的数据
    ubuntu下安装typescript
    随笔6
    excel文件导出相应数据统计内容
    随笔4
    随笔3.2
    随笔2
    随笔1
  • 原文地址:https://www.cnblogs.com/antyhouse/p/13262041.html
Copyright © 2011-2022 走看看