按组求和和计数票务

huangapple go评论75阅读模式
英文:

groupby sum and count tickets

问题

在我的练习中,我必须统计销售的票数并累加总收入。

$tickets = Ingresostaquilla::groupBy('tipo')
    ->selectRaw('count(*) as quantity, tipo')
    ->get();

我已经按票种进行了计数,但我不知道如何累加每种票的总金额。

模型表格:

我应该使用 SUM,但如何在 groupBy 中实现呢?

英文:

I am learning to use groupBy.

In my exercise, I must count the number of tickets that were sold and add the total money collected.

$tickets = Ingresostaquilla::groupBy('tipo')
    ->selectRaw('count(*) as quantity, tipo')
    ->get();

按组求和和计数票务

I did the count by type of ticket, but I don't know how to make it add up to the total amount of money that each ticket collects.

Model table:

按组求和和计数票务

I should use SUM, but how do I do it with groupBy?

答案1

得分: 1

您可以尝试这个查询,它将对每种类型的门票筹集到的金额进行求和。
英文:
$tickets = Ingresostaquilla
        ->selectRaw('tipo, sum(cantidad) as quantity, sum(total)')
        ->groupBy('tipo')
        ->get();  

You can try this query, it will sum the amount of money raised for each type of ticket.

huangapple
  • 本文由 发表于 2023年7月14日 05:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定