2012-06-20 404 views
1

使用MySQL,我計算了幾年時間跨度內發生的幾個事件(字段)。然後,我會逐年按欄顯示。按年份分組時,我的查詢功能非常完美。我現在想添加一個顯示年份總和的最後一列。我如何在列中查詢列?sql多列加上每列的總和

Event 2008 2009 2010 2011 total 
    A  0  2 0  1  3 
    B  1  2 3  0  6 
etc. 

這裏是真正的查詢:

select 
    count(*) as total_docs, 
    YEAR(field_document_date_value) as doc_year, 
    field_document_facility_id_value as facility, 
    IF(count(IF(field_document_type_value ='LIC809',1, NULL)) >0,count(IF(field_document_type_value ='LIC809',1, NULL)),'-') as doc_type_LIC809, 
    IF(count(IF(field_document_type_value ='LIC9099',1, NULL)) >0,count(IF(field_document_type_value ='LIC9099',1, NULL)),'-') as doc_type_LIC9099, 
    IF(count(field_document_f1_value) >0,count(field_document_f1_value),'-') as substantial_compliance, 
    IF(count(field_document_f2_value) >0,count(field_document_f2_value),'-') as deficiencies_sited, 
    IF(count(field_document_f3_value) >0,count(field_document_f3_value),'-') as admin_outcome_809, 
    IF(count(field_document_f4_value) >0,count(field_document_f4_value),'-') as unfounded, 
    IF(count(field_document_f5_value) >0,count(field_document_f5_value),'-') as substantiated, 
    IF(count(field_document_f6_value) >0,count(field_document_f6_value),'-') as inconclusive, 
    IF(count(field_document_f7_value) >0,count(field_document_f7_value),'-') as further_investigation, 
    IF(count(field_document_f8_value) >0,count(field_document_f8_value),'-') as admin_outcome_9099, 
    IF(count(field_document_type_a_value) >0,count(field_document_type_a_value),'-') as penalty_type_a, 
    IF(count(field_document_type_b_value) >0,count(field_document_type_b_value),'-') as penalty_type_b, 
    IF(sum(field_document_civil_penalties_value) >0,CONCAT('$',sum(field_document_civil_penalties_value)),'-') as total_penalties, 
    IF(count(field_document_noncompliance_value) >0,count(field_document_noncompliance_value),'-') as total_noncompliance 

from rcfe_content_type_facility_document 

where YEAR(field_document_date_value) BETWEEN year(NOW()) -9 AND year(NOW()) 
    and field_document_facility_id_value = :facility 

group by doc_year 
+0

我可以看到查詢嗎? – odiszapc

+0

我只是試圖包括我的查詢,但不認爲它花了。我如何獲得查詢給你? –

+0

你可以將它編輯成你的問題,不用擔心格式化 - 有人可以幫你修復它。 – nevets1219

回答

0

你不能GROUP連續兩次在SELECT,所以你只能在一年中總計算行。你可以UNION兩個SELECT(一個按年份分組,一個分組 - 總計)來克服這個限制,但我認爲如果有的話,最好從年份結果中統計總數。

簡單的例子:

SELECT by_year.amount, years.date_year FROM 

-- generating years pseudo table 
(
    SELECT 2008 AS date_year 
    UNION ALL SELECT 2009 
    UNION ALL SELECT 2010 
    UNION ALL SELECT 2011 
) AS years 

-- joining with yearly stat data 
LEFT JOIN 
(
    SELECT SUM(value_field) AS amount, YEAR(date_field) AS date_year FROM data 
    GROUP BY YEAR(date_field) 
) AS by_year USING(date_year) 

-- appending total 
UNION ALL SELECT SUM(value_field) AS amount, 'total' AS date_year FROM data 
+0

謝謝 - 聯盟的作品。不幸的是,我無法訪問腳本。我的另一個問題是,有些年份沒有數據,因此它們不會返回任何值。我需要找出一種方法來顯示那些「空缺」的年份。我似乎無法理解如何使用LEFT OUTER JOIN來做到這一點,這應該做到這一點。 –

+0

增加空白年的例子 – vearutop

+0

另外,vearutop,謝謝你的澄清,顯示幾年沒有任何價值。它工作完美。你搖滾! –