簡體   English   中英

在一個查詢中顯示月SUM和年SUM

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

我有兩個單獨的查詢用於為報告提取信息。 一年工作正常。 但是,如果一個月超過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年和2015年12月將顯示在這些結果中。

我想我很難弄清楚如何處理將數據匯總在一起的月份和年份。

編輯:這是MySQL。 具體來說,我使用Laravel輸出我的SQL。

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

正如你所看到的,由於處於兩個不同的月份,36066有兩個條目:但是,它們也是在兩個不同的年份。 年份部分是擺脫我的桌子。 您可以看到它一起計算所有內容,而不僅僅按月計算,並計算該月份條目的總和。

我的預期結果是1個月(1月)為36066,僅顯示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)

進入您的選擇字段

嘗試這個:

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

更新:

如果您希望同一行顯示年和月,更好地使用腳本循環,或者使用來自2個SQL的結果並顯示togather。 否則請嘗試以下示例(不鼓勵這樣做):

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)

在此輸入圖像描述

閱讀更新后的問題后,我認為以下是最佳解決方案:

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

當您顯示結果時,請使用基於年份+ zip的循環和總和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM