我如何获得计数数据的频率,例如下面的结果,我想要这样的结果来创建仪表板
## Data in year 2014
Table 1
Month_name | Count_value
month1 0
month2 12
month3 15
month4 5
month5 6
month6 12
month7 9
month8 50
month9 0
month10 0
month11 0
month12 0
我的数据库结构类似cust_name | doc_date C001 13.02.2014 C098 17.06.2014 C099 05.06.2014我想按月对它进行计数。
试试这个:
select x.mo,
count(*) as freq
from ( select '01' as mo from RDB$DATABASE union all select '02' from RDB$DATABASE union all select '03' from RDB$DATABASE union all
select '04' from RDB$DATABASE union all select '05' from RDB$DATABASE union all select '06' from RDB$DATABASE union all
select '07' from RDB$DATABASE union all select '08' from RDB$DATABASE union all select '09' from RDB$DATABASE union all
select '10' from RDB$DATABASE union all select '11' from RDB$DATABASE union all select '12'from RDB$DATABASE ) x
left join table_1 t
on x.mo = substring(t.doc_date,3,2)
where t.doc_date like '%2014'
group by x.mo
order by 1
这确实假定doc_date字段像您发布的那样以DD.MM.YYYY格式存储。
我使用了带内联视图的左连接,并选择了“ 01”至“ 12”,以便没有文档的月份仍会返回(您想要的输出显示带有零的月份)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句