Python django合并查询结果,在postgres上对合并后的查询结果进行查询
我正在尝试完成一个查询,以确定员工在不重复签入和签出的情况下,每天唯一的考勤的签入和签入次数,并选择最新的一个进行区分 本质上是组合2个查询结果,并对结果执行查询 下面就更容易解释了 我有两种型号:Python django合并查询结果,在postgres上对合并后的查询结果进行查询,python,django,postgresql,django-queryset,Python,Django,Postgresql,Django Queryset,我正在尝试完成一个查询,以确定员工在不重复签入和签出的情况下,每天唯一的考勤的签入和签入次数,并选择最新的一个进行区分 本质上是组合2个查询结果,并对结果执行查询 下面就更容易解释了 我有两种型号: class Employee(models.Model): fullname = models.CharField(max_length=30, blank=False) class Attendance(models.Model): CHECKIN = 1 CHE
class Employee(models.Model):
fullname = models.CharField(max_length=30, blank=False)
class Attendance(models.Model):
CHECKIN = 1
CHECKOUT = 2
ATTENDANCE_TYPE_CHOICES = (
(CHECKIN, "Check In"),
(CHECKOUT, "Check Out"),
)
employee = models.ForeignKey(Employee)
activity_type = models.IntegerField(choices = ATTENDANCE_TYPE_CHOICES, default=CHECKIN)
timestamp = models.DateTimeField(auto_now_add=True)
我有两个查询来获取考勤中员工的唯一签入和签出
unique_employee_attendance_checkin_today = Attendance.objects.filter(company=auth_employee.employee.company.id,
activity_type=Attendance.CHECKIN,
timestamp__date = today).order_by(
'employee_id','-timestamp').distinct('employee_id')
unique_employee_attendance_checkout_today = Attendance.objects.filter(company=auth_employee.employee.company.id,
activity_type=Attendance.CHECKOUT,
timestamp__date = today).order_by(
'employee_id','-timestamp').distinct('employee_id')
假设我有这些记录
Employee
{"id":1, "employee":"michael jackson",
"id":2, "fullname":"mariah carey",
"id":3, "fullname":"taylor swift"}
Attendance
{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08",
"id":2, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 10:13",
"id":3, "employee": 3,"activity_type": 1, timestamp: "2017-12-05 11:30",
"id":4, "employee": 2,"activity_type": 2, timestamp: "2017-12-05 15:13",
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30",
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13",
"id":7, "employee": 1,"activity_type": 2, timestamp: "2017-12-06 08:08"}
今天唯一的员工考勤签入-将输出此结果
{"id":4, "employee": 2,"activity_type": 2, timestamp: "2017-12-05 15:13",
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30"}
{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08",
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13"}
今天唯一员工考勤结账-将输出此结果
{"id":4, "employee": 2,"activity_type": 2, timestamp: "2017-12-05 15:13",
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30"}
{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08",
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13"}
第一个问题是:
我想结合这两个查询
unique_employee_attendance_checkin_today + unique_employee_attendance_checkout_today
及
我想为员工创建一个distinct,它将删除2条记录,因此输出结果如下
{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08",
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30"
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13"}
问题2如下:
然后根据“活动\类型=1签入”和“活动\类型=2签出”的结果执行查询
上面的问题是,我如何组合2个查询结果,并根据上述要求对组合查询执行查询。组合查询每个员工/日期的最新出勤率,无论类型如何,都可以完全忽略活动类型:
unique_employee_attendance_today = Attendance.objects.filter(
company=auth_employee.employee.company.id,
timestamp__date = today).order_by(
'employee_id','-timestamp'
).distinct('employee_id')
现在,您可以在嵌套查询中筛选出该集合的签入,如:
Attendance.objects.filter(
id__in=unique_employee_attendance_today,
activity_type=Attendance.CHECKIN
)
这只会导致一个数据库查询。您能否澄清为什么在查询中使用created_at,而您的模型只有一个字段时间戳。对不起,我编辑了它,我已将其更改为时间戳。因此,我仍然感到困惑。您的签入示例qs具有活动类型2签出。。。如果我理解正确,您希望按日期和员工筛选最新的活动,然后从中筛选出两种不同的活动类型?我编辑了示例记录,希望其更清晰,请参见id=4和6中的重复。有两个查询,分别是唯一的员工考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤考勤。我想合并结果,并对该结果执行另一个查询-这与员工id不同