将subselect值与master select中的值进行比较

时间:2009-02-06 03:47:12

标签: sql ms-access subquery

在MS Access中,我有一个查询,我想在外部查询中使用列作为内部查询中的条件:

SELECT P.FirstName, P.LastName, Count(A.attendance_date) AS CountOfattendance_date,
       First(A.attendance_date) AS FirstOfattendance_date,
       (SELECT COUNT (*) 
          FROM(SELECT DISTINCT attendance_date 
                FROM tblEventAttendance AS B 
                WHERE B.event_id=8 
                  AND B.attendance_date >= FirstOfattendance_date)
       ) AS total
FROM tblPeople AS P INNER JOIN tblEventAttendance AS A ON P.ID = A.people_id
WHERE A.event_id=8
GROUP BY P.FirstName, P.LastName
;

关键点是FirstOfattendance_date - 我希望subselect中的深度比较使用master select的每次迭代中的值。显然这不起作用,它在我尝试运行它时会询问FirstOfattendance_date的值。

我想在不使用VB代码的情况下这样做......有什么想法吗?

2 个答案:

答案 0 :(得分:1)

怎么样:

SELECT 
     p.FirstName,
     p.LastName,
     Count(a.attendance_date) AS CountOfattendance_date,
     First(a.attendance_date) AS FirstOfattendance_date,
     c.total
FROM (
     tblPeople AS p 
INNER JOIN tblEventAttendance AS a ON 
     a.people_id = p.ID) 
INNER JOIN (SELECT people_id, Count (attendance_date) As total
            FROM (
                SELECT DISTINCT people_id,attendance_date
                FROM tblEventAttendance) 
            Group By people_id) AS c ON 
     p.ID = c.people_id
GROUP BY 
     p.ID, c.total;

答案 1 :(得分:0)

你能改变吗?

B.attendance_date> = FirstOfattendance_date

B.attendance_date> = First(A.attendance_date)