Mysql sql多列加上每列的总和
使用MySQL,我计算数年时间跨度内几个事件(字段)的发生率。然后,我会按年份在列中显示它。当按年份分组时,我的查询工作正常。现在,我想添加最后一列,显示年份的总和。我如何包含列总数查询Mysql sql多列加上每列的总和,mysql,Mysql,使用MySQL,我计算数年时间跨度内几个事件(字段)的发生率。然后,我会按年份在列中显示它。当按年份分组时,我的查询工作正常。现在,我想添加最后一列,显示年份的总和。我如何包含列总数查询 Event 2008 2009 2010 2011 total A 0 2 0 1 3 B 1 2 3 0 6 etc. 以下是真正的疑问: select count(*) as total_d
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
您不能在
选择中对行进行两次分组
,因此您只能计算一年中的行数或总计行数。您可以UNION
twoSELECT
(一个按年份分组,第二个未分组-total)来克服此限制,但我认为最好在脚本中从年份结果中计算总计(如果有)
简化示例:
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
使用ROLLUP是您的朋友:
使用原始查询,只需将其添加到最后一行:
GROUP BY doc_year WITH ROLLUP
这将向查询的结果集中添加最后一个累积行。我只是尝试包含我的查询,但不认为需要花费时间。我该如何向您发送查询?您只需将其编辑到您的问题中,不必担心格式问题-有人可以帮您解决。编辑您的问题并使用代码标记将查询插入其中。我无法了解您的查询与您的小示例之间的关系:s您能澄清它吗?谢谢您-工会有效。不幸的是,我没有访问脚本的权限。我的另一个问题是,有些年份没有数据,因此它们不会返回任何值。我需要找出一种方法来显示那些“空闲”的年份。我似乎无法理解如何使用左外连接来实现这一点,而左外连接应该可以实现这一点。添加了空年份的示例。所以,vearutop,感谢您的澄清,以显示没有任何值的年份。它工作得很好。你太棒了!!!