简体   繁体   English

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

[英]Showing the month SUM and year SUM in one query

I have two separate queries I am using to pull information for a report. 我有两个单独的查询用于为报告提取信息。 A single year worked fine. 一年工作正常。 However, if a month has more than 1 year it is not showing the correct responses. 但是,如果一个月超过1年,则没有显示正确的答案。

Here are my two queries: 这是我的两个问题:

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)

Then I loop through the results of month as such (and include year in the loop): 然后我循环遍历月的结果(并包括循环中的年份):

@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

So if I have a total for December 2014 & December 2015 and my table is filtering on December 2014...December 2014 & 2015 will show in those results. 因此,如果我有2014年12月和2015年12月的总数,我的表格将于2014年12月过滤... 2014年和2015年12月将显示在这些结果中。

I guess I am having a hard time trying to figure out how to process the month and year to where it sums the data together. 我想我很难弄清楚如何处理将数据汇总在一起的月份和年份。

EDIT: This is MySQL. 编辑:这是MySQL。 Specifically I am using Laravel to output my SQL. 具体来说,我使用Laravel输出我的SQL。

Here is my table: http://i.imgur.com/RClRDaW.png 这是我的表: http//i.imgur.com/RClRDaW.png

as you can see 36066 has two entries due to being in two different months: However, they are also in 2 separate years. 正如你所看到的,由于处于两个不同的月份,36066有两个条目:但是,它们也是在两个不同的年份。 The year part is what is throwing my table off. 年份部分是摆脱我的桌子。 You can see it's calculating it all together rather than just by month and totaling the year for that months entry. 您可以看到它一起计算所有内容,而不仅仅按月计算,并计算该月份条目的总和。

My expected results are for 36066 1 month (January) to only show 2017 with that total and 36066 10 month (october) to only show the 2016 with that years total since those are the years that those two months belong to. 我的预期结果是1个月(1月)为36066,仅显示2017年的总数和36066 10个月(10月)仅显示2016年的总数,因为那些是这两个月所属的年份。

So, I need the results to look like this: http://i.imgur.com/jlFhR7x.png 所以,我需要看到如下结果: http//i.imgur.com/jlFhR7x.png

I hope that provides so clarity. 我希望这提供了如此清晰。

add YEAR(bk_schedule.date) into the GROUP BY of your 2nd query YEAR(bk_schedule.date)添加到第二个查询的GROUP BY

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

if you got error just include 如果你有错误只是包括

YEAR(bk_schedule.date)

into your select fields 进入您的选择字段

Try this: 尝试这个:

SELECT 
  zip_codes.zip_code, 
  substr(bk_schedule.`date`, 1,7) AS ym,
  SUM(rpt_complete.total) AS total
FROM  ...
LEFT JOIN ...
GROUP BY 1,2

Updated: 更新:

If you want both year and year-month shown on same line, better use script to loop, OR use results from 2 SQLs and display togather. 如果您希望同一行显示年和月,更好地使用脚本循环,或者使用来自2个SQL的结果并显示togather。 Otherwise try the following example (Which is not not encouraged): 否则请尝试以下示例(不鼓励这样做):

SELECT y.geo, ym.ym, y.hit year_hit, ym.hit month_hit
FROM (
  SELECT geo,year(ymd) y,sum(hit) hit FROM log_ip_hit 
  WHERE geo in ('US','CN')
  AND ymd BETWEEN '2015-07-01' AND '2016-03-31'
  GROUP BY 1,2
) AS y
JOIN (
  SELECT geo,substr(ymd,1,7) ym,sum(hit) hit FROM log_ip_hit 
  WHERE geo in ('US','CN','AU')
  AND ymd BETWEEN '2015-07-01' AND '2016-03-31'
  GROUP BY 1,2
) AS ym ON y.geo = ym.geo AND y.y = substr(ym.ym,1,4)

在此输入图像描述

After reading your updated question, I think the following is the best solution: 阅读更新后的问题后,我认为以下是最佳解决方案:

SELECT 
  zip_codes.zip_code, 
  year(bk_schedule.`date`) AS y,
  substr(bk_schedule.`date`, 5,2) AS m,
  SUM(rpt_complete.total) AS total
FROM  ...
LEFT JOIN ...
GROUP BY 1,2,3

And when you display the result, use loop and sum based on year + zip. 当您显示结果时,请使用基于年份+ zip的循环和总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM