我有一个Rails / PostgreSQL Web应用程序,其中的employee表具有以下格式:
# == Schema Information
#
# Table name: employees
#
# id :bigint(8) not null, primary key
# first_name :string
# last_name :string
# hired_datetime :datetime
# terminated_datetime :datetime
我想按月生成员工保留率图,如下所示(但要计算全部12个月,或者最后12个月):
如何生成查询(使用ActiveRecord或直接SQL)以生成该图所需的数据?
答案 0 :(得分:0)
如果您尝试保留年月组合,则建议使用date_trunc,否则建议使用date_part。 Here's the documentation.
import xml.etree.ElementTree as ET
xml = '''<r> <Learner>
<LearnRefNumber>16Learner</LearnRefNumber>
<PMUKPRN>87654321</PMUKPRN>
<CampId>1234ABCD</CampId>
<ULN>1061484016</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1999-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<PlanLearnHours>440</PlanLearnHours>
<PlanEEPHours>100</PlanEEPHours>
<MathGrade>NONE</MathGrade>
<EngGrade>D</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EDF</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>MCF</LearnFAMType>
<LearnFAMCode>3</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>FME</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>PPE</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record is not required for full time 16-19 (excluding apprenticeships) funded learners -->
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>cb5f0d25-cff4-4ea0-92f5-99378cce306d</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50023408</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-02-14</LearnStartDate>
<LearnPlanEndDate>2016-07-15</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>3</CompStatus>
<LearnActEndDate>2015-04-01</LearnActEndDate>
<WithdrawReason>98</WithdrawReason>
<Outcome>3</Outcome>
<SWSupAimId>c243182a-30af-4879-8f68-3eac708e6bb3</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
</Learner></r>
'''
root = ET.fromstring(xml)
dob_lst = root.findall('.//Learner/DateOfBirth')
for dob in dob_lst:
dob.text = 'NULL'
ET.dump(root)
从那里,您可以创建诸如date_trunc('month', timestamp '2001-02-16 20:38:40') 2001-02-01 00:00:00
date_part('month', timestamp '2001-02-16 20:38:40') 2
之类的聚合函数。
count
或者,在以后的查询中,如果要查找2018年12月的特定员工记录(包括第一名,最后一名等):
SELECT count(*), date_trunc('month', terminated_datetime)
FROM employees
GROUP BY date_trunc('month', terminated_datetime);
然后再次,您可能会看到所有在12月(无论年份如何)离职的员工:
SELECT *, date_trunc('month', terminated_datetime)
FROM employees
WHERE date_trunc('month', terminated_datetime) = '2018-12-01 00:00:00';