gpt4 book ai didi

php - 在一个查询中显示月 SUM 和年 SUM

转载 作者:行者123 更新时间:2023-11-30 22:06:04 30 4
gpt4 key购买 nike

我有两个单独的查询用于提取报告信息。一年效果很好。但是,如果一个月超过 1 年,则不会显示正确的响应。

这是我的两个查询:

select SUM(rpt_complete.total) total, YEAR(bk_schedule.date) as year,zip_codes.zip_code 
from rpt_complete
left join bk_schedule on rpt_complete.bk_id = bk_schedule.id
left join users_info on bk_schedule.users_info_id = users_info.id
left join zip_codes on users_info.zip_code = zip_codes.zip_code
group by zip_codes.zip_code, YEAR(bk_schedule.date)


select SUM(rpt_complete.total) total, MONTH(bk_schedule.date) as month, zip_codes.zip_code
from rpt_complete
left join bk_schedule on rpt_complete.bk_id = bk_schedule.id
left join users_info on bk_schedule.users_info_id = users_info.id
left join zip_codes on users_info.zip_code = zip_codes.zip_code
group by zip_codes.zip_code, MONTH(bk_schedule.date)

然后我循环遍历月份的结果(并在循环中包含年份):

@foreach($month_total as $month)
<tr>
<td>{{ $month->zip_code }}</td>
<td>{{ $month->month }}</td>
<td>${{ $month->total }}</td>
<td>
@foreach($year_total as $year)
@if($month->zip_code == $year->zip_code)
{{ $year->year }}
@endif
@endforeach
</td>
<td>
@foreach($year_total as $year)
@if($month->zip_code == $year->zip_code)
${{ $year->total }}
@endif
@endforeach
</td>
</tr>
@endforeach

因此,如果我有 2014 年 12 月和 2015 年 12 月的总计,并且我的表格在 2014 年 12 月过滤...2014 年 12 月和 2015 年 12 月将显示在这些结果中。

我想我很难弄清楚如何将月份和年份处理到将数据加在一起的位置。

编辑:这是 MySQL。具体来说,我正在使用 Laravel 来输出我的 SQL。

这是我的表格:http://i.imgur.com/RClRDaW.png

如您所见,36066 由于处于两个不同的月份而有两个条目:但是,它们也在 2 个不同的年份。年份部分是什么让我的 table 掉下来。您可以看到它是一起计算的,而不是仅按月计算并计算该月条目的年份。

我的预期结果是 36066 1 个月(1 月)只显示 2017 年的总数,36066 10 个月(10 月)只显示 2016 年的总数,因为这两个月属于那一年。

所以,我需要这样的结果:http://i.imgur.com/jlFhR7x.png

我希望如此清晰。

最佳答案

YEAR(bk_schedule.date) 添加到第二个查询的 GROUP BY

group by zip_codes.zip_code, MONTH(bk_schedule.date), YEAR(bk_schedule.date)

如果你有错误就包含

YEAR(bk_schedule.date)

进入您选择的领域

关于php - 在一个查询中显示月 SUM 和年 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733238/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com