简体   繁体   English

加盟协助

[英]Join assistance

I have a table (Attendance Table) that records attendance statistics.我有一个记录出勤统计的表(出勤表)。 In each row, there are the following:在每一行中,有以下内容:

  • Event Date (date)活动日期(日期)
  • Member ID (int)会员 ID (int)
  • Event ID (int)事件 ID (int)
  • Attended (bit)出席(位)

Only if a member is marked in attendance will a row be present with a "1" for Attended.只有当成员被标​​记为出席时,一行才会出现“1”表示出席。 There are a few exceptions where someone clicked them by mistake and then toggled them back in which case the Row will contain a "0".有一些例外,有人错误地单击了它们,然后将它们切换回,在这种情况下,行将包含“0”。

Then I have another table (Dates Table) that has a list of all the possible Event Dates.然后我有另一个表(日期表),其中列出了所有可能的事件日期。

I'm looking for a way to show a listing of every possible date for a specific attendee to show if there were present or not.我正在寻找一种方法来显示特定与会者的每个可能日期的列表,以显示是否存在。 I've tried a few JOIN combinations but am having trouble getting values for dates that do not have an associated row in the Attendance table.我尝试了一些 JOIN 组合,但无法获取在出勤表中没有关联行的日期的值。

I know I could do this with a FOREACH LOOP in my App Code, but I would prefer to let the DB Engine do the work since it's much more efficient.我知道我可以在我的应用程序代码中使用 FOREACH LOOP 来做到这一点,但我更愿意让 DB Engine 来完成这项工作,因为它更有效率。

I would like to have output similar to:我希望输出类似于:

  • EventDate (From Dates Table) EventDate(来自日期表)
  • MemberID (From Attendance Table) MemberID(来自考勤表)
  • EventID (From Attendance Table) EventID(来自考勤表)
  • Attended (From Attendance Table) - This will be either a 0 or 1已出席(来自出勤表)- 这将是 0 或 1

Thanks in advance for your assistance.提前感谢你的帮助。

UPDATED:更新:

Here is an example of data in the Attendance Table for a specific member in the month of August.以下是特定成员在 8 月份的出勤表中的数据示例。

Attendance Table Sample考勤表样本

Here is an example of data in the Dates Table that shows all potential dates in month of August.以下是日期表中的数据示例,其中显示了 8 月份的所有可能日期。

Dates Table Sample日期表示例

If the Member wasn't present on a specific day, I want to be able to show this....there will be either a NULL record or a (0) in the Attended column of the Attendance Table if they were not present.如果该成员在特定日期不在场,我希望能够显示这一点....如果他们不在场,则出勤表的已参加列中将有一个 NULL 记录或 (0)。

I think you can use a left join with some aggregation.认为您可以使用带有一些聚合的left join Assuming you have tables for members and dates, then:假设您有成员和日期表,那么:

select e.*, m.memberid, coalesce(a.attended, 0) as attended
from members m cross join
     events e left join
     (select eventid, memberid, max(attended) as attended
      from attendance a
      group by eventid, memberid
     ) a
     on m.memberid = a.memberid and e.eventid = a.eventid;

If you don't have separate tables for members and events, you an use a subquery, such as (select distinct memberid from attendance) instead.如果您没有用于成员和事件的单独表,则可以使用子查询,例如(select distinct memberid from attendance)

I think I figured it out....我想我想通了......

SELECT tad.date, DAYNAME(tad.date) as Day,
    (SELECT COUNT(*)
    FROM attendance a
    WHERE a.MemberID = pMemberID
    AND a.ServiceTypeID = 1
    AND a.Date = tad.date) AS Attended_SS,

    (SELECT COUNT(*)
    FROM attendance a
    WHERE a.MemberID = pMemberID
    AND a.ServiceTypeID = 2
    AND a.Date = tad.date) AS Attended_AM,

    (SELECT COUNT(*)
    FROM attendance a
    WHERE a.MemberID = pMemberID
    AND a.ServiceTypeID = 3
    AND a.Date = tad.date) AS Attended_PM,

    (SELECT COUNT(*)
    FROM attendance a
    WHERE a.MemberID = pMemberID
    AND a.ServiceTypeID = 4
    AND a.Date = tad.date) AS Attended_WED

FROM t_AllDates tad
WHERE tad.date BETWEEN pStartDate AND pEndDate;

Which generates the following output (not perfect, but will work for me)生成以下输出(不完美,但对我有用)

Output输出

Thanks for the help.谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM