我试图在一个查询中使用多个Count和Sum命令。然而,当我使用以下命令时,发生了什么:SUM(amount AND type = "dispute") as charge
它实际上计算记录的数量,而不是合计数量。我想做的是多次使用SUM和where子句,例如:
`$query = DB::table('cash_table')
->where('transaction_created', '>=', Carbon::now()->startOfWeek())
->select(DB::raw('
SUM(amount) as transaction_amount,
COUNT(amount) as transaction_count,
SUM(amount) WHERE type = "dispute" as charge_amount
COUNT(amount) WHERE type = "dispute" as charge_count
SUM(loan_amount) WHERE type = "loan" as loan_amount
COUNT(loan_amount) WHERE type = "loan" as loan_count
'))
->groupBy('transaction_created')
->get();`
前两个“transaction_amount & transaction count "工作正常,因为没有where子句。
2条答案
按热度按时间axkjgtzd1#
试试下面!
agxfikkp2#
请尝试以下内容: