1

MySQLを使用して、数年の期間にわたるいくつかのイベント(フィールド)の発生をカウントしています。次に、これを年ごとの列に表示します。私のクエリは、年ごとにグループ化すると完璧に機能します。ここで、年の集計を表示する最後の列を追加します。列の合計クエリを含めるにはどうすればよいですか?

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  
4

2 に答える 2

0

GROUPで2回行することはできないSELECTため、1年または合計で行を数えることしかできません。この制限を克服するためにUNION2つSELECT(1つは年ごとにグループ化され、2つ目はグループ化されていない-合計)にすることができますが、もしあれば、スクリプトの年の結果から合計を数える方が良いと思います。

簡略化した例:

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
于 2012-06-20T06:32:48.463 に答える
0

WITH ROLLUPはあなたの友達です: http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

元のクエリを使用して、これを最後の行に追加するだけです。

GROUP BY doc_year WITH ROLLUP

これにより、クエリの結果セットに最終的な累積行が追加されます。

于 2016-01-07T23:23:38.057 に答える