[英]Performing some arithmetic operations inside a MySQL query on a subset of records
I have a MySQL table containing attendance records. 我有一个包含考勤记录的MySQL表。 Relevant columns identify attendees, sessions and attendance status (confirmed/declined/no-response).
相关列标识与会者,会话和出席状态(已确认/已拒绝/无响应)。 I also have a second table containing attendees' personal info, including the date they joined the group.
我还有第二张表,其中包含与会者的个人信息,包括他们加入该组的日期。
I am trying to build a single query that will output a list of all attendees, the number of times they attended sessions, and their regularity, expressed as the percentage of sessions they attended (against the total number of sessions). 我正在尝试构建一个查询,该查询将输出所有与会者的列表,他们参加会议的次数及其规律性,以他们参加会议的百分比(相对于会议总数)表示。 The problem is that total of sessions should only contains the sessions since the attendee joined the group.
问题在于,自与会者加入组以来,会话总数仅应包含会话。
So, for the first part, my query is: 因此,对于第一部分,我的查询是:
SELECT name, count(*)
FROM attendance, members
WHERE attendance.attend = 2 and
attendance.member_number = members.number
GROUP BY attendance.member_number, attendance.attend
HAVING count(*) > 0
ORDER BY count(*) desc
This returns all attendees' names, plus the number of sessions each had attended. 这将返回所有与会者的姓名,以及每个与会者已参加的会议数。 However, I'd also like to add that third column.
但是,我也想添加第三列。
Thus, for each record, I first have to find out what was the members.member_since date, then query only the group of attendance records for each member, for which the dates are greater than member_since, get their count, then from that subset get the count where atteneance.attend = 2, then calculate what percentage of that subset count represents the second count (of records having attend = 2). 因此,对于每条记录,我首先必须找出什么是member.member_since日期,然后仅查询日期大于member_since的每个成员的出勤记录组,获取其计数,然后从该子集中获取atteneance.attend = 2的计数,然后计算该子集计数的百分比代表第二个计数(具有出席人数的记录= 2)。
How do I write my query? 如何编写查询?
Data looks like this: 数据如下所示:
Table members: 表成员:
number, name, join_date
001, Peter James, 2016-07-26
002, John Smith, 2014-06-04
TABLE attendance: 表出席:
member_number, attend, date
001, 2, 2019-05-11
001, 1, 2019-05-04
001, 2, 2019-04-26
001, 2, 2019-04-18
002, 2, 2014-08-01
002, 1, 2016-08-01
002, 1, 2019-05-04
The desired result would be to get the following: 期望的结果将是获得以下结果:
Name, Attended, percentage
Peter James, 3, 75%
John Smith, 1, 14%
In other words, while there were 6 sessions, Peter James has only been member for the last four, and attended 3 of them (75%), while John Smith has been a member since the beginning, so his attendance is one out of 6. 换句话说,虽然有6次会议,但彼得·詹姆斯只参加了最近四次会议,并参加了其中的3次(75%),而约翰·史密斯从一开始就一直是成员,所以他的出席是六分之一。
The structure is somewhat simplified (there is a 'sessions' table with details about sessions, including the date, and the 'attendance' table refers to its key column, 'att_number', rather than the actual date), but the point is the same. 该结构有所简化(有一个“会话”表,其中包含有关会话的详细信息,包括日期,“出勤”表引用其关键列“ att_number”,而不是实际日期),但重点是相同。
Join the tables just on the dates to get the total number of sessions since the member joined. 仅在日期上加入表,以获取自成员加入以来的会话总数。 Then use a conditional
SUM()
to get the number of sessions that the member attended. 然后,使用条件
SUM()
获取成员参加的会话数。 Divide them to get the percentage. 除以得到百分比。
SELECT m.name, SUM(m.number = a.member_number AND a.attend = 2) AS attendance, 100*SUM(m.number = a.member_number AND a.attend = 2)/COUNT(*) AS percent
FROM members AS m
JOIN attendance AS a ON a.date >= m.member_since
GROUP BY m.number
ORDER BY attendance DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.