我有一个客户表,包含以下列。
Admit_date Gender Homeless Unemployed Deleted
4/2/2012 Male Yes Yes 0
1/1/2011 Female Yes Yes 0
12/2/2011 Male No No 0
5/23/2009 Female Yes Yes 0
4/3/2009 Male No No 0
7/4/2010 Male Yes Yes 0
9/2/2010 Male Yes Yes 0
我需要展示每年每个组的百分比。我认为这将需要一个数据透视表:
2009 2010 2011 2012
Admitted 2 2 2 1
Male 50% 100% 50% 100%
Female 50% 0 50% 0%
Homeless 50% 100% 50% 100%
Unemployed 50% 100% 50% 100%
这个查询会给出每年的计数:
select year(admit_date_c) as Year_of_Admit,count((admit_date_c)) as Admitted
from clients where deleted = '0'
group by year(admit_date_c)
Year_of_Admit Admitted
2009 2
2010 2
2011 2
2012 1
我尝试使用案例计数进行多次查询,但无法弄清如何获取性别、无家可归和失业人员的数量或百分比。一旦我有了这些数据,我认为我可以透视表来获得所需的显示结果。