简体   繁体   中英

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.

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.

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. Specifically I am using Laravel to output my SQL.

Here is my table: 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. 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.

So, I need the results to look like this: 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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