如何得到计数数据的频率,如下面的结果 我想要这样的结果来创建仪表板
## 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 我想逐月计算它。
答案 0 :(得分:1)
试试这个:
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格式存储。
我使用左边连接w /内联视图选择'01'到'12',这样没有文档的月份仍会返回(你想要的输出显示几个月为零)