提取数据总结金额列,以使结果由客户,月和年分组

发布于 2025-01-17 15:17:36 字数 1010 浏览 1 评论 0原文

我有一个客户表和一个客户交易表。

这是下面的客户表。

输入图片此处描述

这是下面的客户交易表。

输入图片此处描述

这是我想要实现的目标:

在此处输入图像描述

我希望能够对金额列进行求和,以便结果按客户分组,并且月份和年份。

这就是我到目前为止所做的:

我为客户客户交易创建了一个模型。我在客户事务模型中具有这种关系:

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

我还有一个客户控制器,其中有获取数据的逻辑。在我的 CustomerController 文件中:

    public function index()
    {
        $transactions = CustomerTransaction::with('customer')->get();

        dd($transactions);
    }

I have a customers table and a customer transactions table.

This is the customers' table below.

enter image description here

This is the customer transactions table below.

enter image description here

This is what I want to achieve below:

enter image description here

I want to be able to sum up the amount column such that the result is grouped by customers and month and year.

This is what I've done so far:

I created a model for both Customers and Customers transactions. I have this relationship in the Customer transactions model:

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

I also have a Customer controller where I have the logic to fetch the data. In my CustomerController file:

    public function index()
    {
        $transactions = CustomerTransaction::with('customer')->get();

        dd($transactions);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2025-01-24 15:17:36

我能够解决它。在 customercontroller 文件中,这就是我现在在索引方法中所拥有的:

$transactions = CustomerTransaction::with('customer')
->selectRaw('customer_id, sum(amount) as amount, MONTH(date_created) as month, YEAR(date_created) as year')
->groupBy('customer_id', 'month', 'year')
->get();

然后,在我看来,我有:

                    <table class="table">
                        <tr>
                            <th>S/N</th>
                            <th>Name</th>
                            <th>Amount</th>
                            <th>Year month</th>
                        </tr>
                        @foreach ($transactions as $transaction)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $transaction->customers->name }}</td>
                                <td>{{ $transaction->amount }}</td>
                                <td>{{ $transaction->year }}-{{ sprintf('%02d', $transaction->month) }}</td>
                            </tr>
                        @endforeach
                    </table>

I was able to resolve it. In the CustomerController file, this is what I now have in the index method:

$transactions = CustomerTransaction::with('customer')
->selectRaw('customer_id, sum(amount) as amount, MONTH(date_created) as month, YEAR(date_created) as year')
->groupBy('customer_id', 'month', 'year')
->get();

Then in my view, I have this:

                    <table class="table">
                        <tr>
                            <th>S/N</th>
                            <th>Name</th>
                            <th>Amount</th>
                            <th>Year month</th>
                        </tr>
                        @foreach ($transactions as $transaction)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $transaction->customers->name }}</td>
                                <td>{{ $transaction->amount }}</td>
                                <td>{{ $transaction->year }}-{{ sprintf('%02d', $transaction->month) }}</td>
                            </tr>
                        @endforeach
                    </table>
自我难过 2025-01-24 15:17:36

我能够编写SQL查询:

select ROW_NUMBER() OVER(ORDER BY (select null)) as id,
name as `Customer name`,
sum(amount) as `Total amount`,
concat(YEAR(date_created), '-',
lpad(MONTH(date_created), 2, '0')) as `Year month`
from customer_transactions
join customers on customers.id=customer_transactions.customer_id
group by `customer_id`, `Year month`
order by `customer_id`, `Year month`

I was able to write the SQL query:

select ROW_NUMBER() OVER(ORDER BY (select null)) as id,
name as `Customer name`,
sum(amount) as `Total amount`,
concat(YEAR(date_created), '-',
lpad(MONTH(date_created), 2, '0')) as `Year month`
from customer_transactions
join customers on customers.id=customer_transactions.customer_id
group by `customer_id`, `Year month`
order by `customer_id`, `Year month`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文