2014-02-15 38 views
0

我正在写一个考勤/ PTO跟踪应用程序,并且我绝对难以确定如何确定在两个给定日期时间(开始和结束)之间工作的代理的最小数量。这将用于确定代理是否可以请求关闭(如果他们将在任何时候独处,他们将被自动拒绝)。鉴于以下数据库模式:确定在时间范围内可用的最小用户数

metadata = MetaData(engine) #< engine is an already established mysql conn 

# Users table declaration 
self.Users = Table(
    'Users', metadata, 
    Column(u'ID', Integer(), primary_key=True, nullable=False, 
      index=True), 
    Column(u'Username', Unicode(16), nullable=False, index=True), 
    Column(u'Firstname', Unicode(16), nullable=False), 
    Column(u'Lastname', Unicode(50), nullable=False, index=True), 
    Column(u'Nickname', Unicode(16), nullable=False), 
    Column(u'Email', Unicode(16), nullable=False), 
    Column(u'AvayaID', Integer(), nullable=False, index=True), 
    Column(u'ManagerID', Integer(), nullable=False, index=True), 
    Column(u'ShiftID', Integer(), ForeignKey('Shifts.ID'), nullable=False), 
    Column(u'DaysID', Integer(), ForeignKey('Days.ID'), nullable=False), 
) 

# Shifts table declaration - Shift times 
self.Shifts = Table(
    'Shifts', metadata, 
    Column(u'ID', Integer(), primary_key=True, nullable=False, index=True), 
    Column(u'Start', DateTime()), 
    Column(u'End', DateTime()), 
) 

# Days table declaration - Days of week worked 
self.Days = Table(
    'Days', metadata, 
    Column(u'ID', Integer(), primary_key=True, nullable=False), 
    Column(u'Sat', Integer(1), nullable=False), 
    Column(u'Sun', Integer(1), nullable=False), 
    Column(u'Mon', Integer(1), nullable=False), 
    Column(u'Tue', Integer(1), nullable=False), 
    Column(u'Wed', Integer(1), nullable=False), 
    Column(u'Thu', Integer(1), nullable=False), 
    Column(u'Fri', Integer(1), nullable=False), 
    ) 

Users表有两个外键,对应于ShiftsShiftID并在DaysDaysIDShifts表只保存可能的移位开始/结束时间,Days表包含可能的工作组合。整个表加入我产生类似的结构:

{ 
    'Username' : str, #< Agent username 
    'ManagerID' : int, #< Agent manager ID 
    'Shift.Start' : time,#< Agent shift start time 
    'Shift.End' : time, #< Agent shift end time 
    'Days.Sat' : bool, #< If agent works this day normally 
    'Days.Sun' : bool, #< If agent works this day normally 
    'Days.Mon' : bool, #< If agent works this day normally 
    'Days.Tue' : bool, #< If agent works this day normally 
    'Days.Wed' : bool, #< If agent works this day normally 
    'Days.Thu' : bool, #< If agent works this day normally 
    'Days.Fri' : bool, #< If agent works this day normally 
} 

我需要确定的代理选择代理的转变过程中,将提供的最低金额。

我在这里遇到了一些问题,其中一个是我必须考虑过夜班次(start_time> end_time),另一个只是围绕编写这个查询来包装我的头脑。我在想,我的数据库模式是这里的问题,但我想不出更好的结果,Google也没有帮助。

+0

如果您对答案感到满意,请接受它。 – ACV

回答

1

如果我正确地理解了你,你想关联日期,用户和班次。我会保持你的User表,然后添加如下内容:

self.Schedule = Table(
    'schedule', metadata, 
    Column(u'id', Integer(), primary_key=True, nullable=False, index=True), 
    Column(u'User_ID', Integer(), nullable=False, ForeignKey('User.ID')), 
    Column(u'Date', DateTime(), nullable=False), 
    Column(u'Shift', Integer(), nullable=False) 
    UniqueConstraint('User_ID', 'Date', 'Shift', name='schedule_constraint') 
) 

该表将存储记录的每个班次每一个员工在工作。它有一个限制,即日期,用户和班次的每个组合都必须是唯一的。这确保同一个人不能每天多次工作同一班次,但他们仍然可以每天工作多于一班,或多于一天。然后,你可以查询计划的工作这样的转变的用户数量(如1,2,3)

session.Query(Schedule).\ 
     filter(Schedule.Date == '2014-02-18').\ 
     filter(Schedule.Shift == '1').\ 
     count() 

简言之我假设移位的编号。如果它们是可变的,那么可以很容易地修改它以包括开始和结束时间,并在查询中使用>=<=语句。

注意:我遵循约定,但请注意,在SQLAlchemy Ver。 0.9 Integer列类型声明不带括号,请参阅此问题:Error in SQLAlchemy with Integer: "object() takes no parameters"

相关问题