将参数传递给MS Access数据库中的子查询

时间:2014-08-05 01:56:10

标签: sql ms-access

我试图让这个查询从我的department_id表中选择每个departments,然后将这些值(大约有30个左右)传递到子查询中,如此< / p>

SELECT Count(problems.id) AS Problems, 
departments.dname AS Company, 
departments.department_id AS CompanyID,
(SELECT COUNT(*) FROM problems WHERE problems.status BETWEEN 1 AND 8) AS totalopentickets, 
(SELECT COUNT(*) FROM problems WHERE status = 100) AS totalclosedtickets, 
(SELECT COUNT(problems.department) FROM problems WHERE problems.status = 100 AND problems.department = CompanyID) AS departmentclosed, 
(SELECT COUNT(problems.department) FROM problems WHERE problems.status BETWEEN 1 AND  8 AND problems.department = CompanyID) AS departmentopen
FROM problems 
INNER JOIN departments ON problems.department = departments.department_id
GROUP BY departments.dname, departments.department_id
ORDER BY Count(problems.id) DESC;

这个想法是它将获取所有这些值(1,2,3等),然后执行子查询

  

(SELECT COUNT(problems.department)FROM Problems WHERE problems.status BETWEEN 1 AND 8 AND problems.department = CompanyID)

使用这些值中的每一个。这适用于MySQL,但我不确定如何让它在Access中工作。每次它询问我一个参数,那么当我这样做时,我只得到一个参数值的值。

1 个答案:

答案 0 :(得分:1)

我刚刚设法通过不使用别名来解决这个问题,而是使用表和字段名称,如此

SELECT Count(problems.id) AS Problems, 
departments.dname AS Company, 
departments.department_id AS CompanyID,
(SELECT COUNT(*) FROM problems WHERE problems.status BETWEEN 1 AND 8) AS totalopentickets, 
(SELECT COUNT(*) FROM problems WHERE status = 100) AS totalclosedtickets, 
(SELECT COUNT(problems.department) FROM problems WHERE problems.status = 100 AND problems.department = departments.department_id) AS departmentclosed, 
(SELECT COUNT(problems.department) FROM problems WHERE problems.status BETWEEN 1 AND  8 AND problems.department = departments.department_id) AS departmentopen
FROM problems 
INNER JOIN departments ON problems.department = departments.department_id
GROUP BY departments.dname, departments.department_id
ORDER BY Count(problems.id) DESC;