查询太长了

时间:2014-02-23 03:23:50

标签: sql vba ms-access

我有这个查询来计算数据库表上的数据并将其存储到文本框中,然后将其保存到另一个存储计数的表中。我首先将数据存储到我的文本框中,因为我需要将计数放在我的第二个数据库表上的特定位置。有没有办法简化这个查询? :

query = "SELECT (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance') as [Revenue Assurance]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department Like '%Compliance, Fraud Investigation Services%' OR (Department = 'Compliance, Fraud Investigation Services')) as [Compliance, Fraud Investigation Services]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department Like '%Compliance, Solutions and Services%' OR (Department = 'Compliance, Solutions and Services')) as [Compliance, Solutions and Services]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Head (Senior)')) as [txtRAHS]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Head')) as [txtRAH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Manager')) as [txtRASRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Manager')) as [txtRAMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Supervisor')) as [txtRASRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Supervisor')) as [txtRASUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Jr. Supervisor')) as [txtRAJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Staff')) as [txtRASRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Staff')) as [txtRASTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Jr. Staff')) as [txtRAJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Project Staff')) as [txtRAPS]" & _
'[CFIS]
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Head')) as [txtCFISH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Manager')) as [txtCFISSRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Manager')) as [txtCFISMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Supervisor')) as [txtCFISSRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Supervisor')) as [txtCFISSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Jr. Supervisor')) as [txtCFISJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Staff')) as [txtCFISSRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Staff')) as [txtCFISSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Jr. Staff')) as [txtCFISJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Project Staff')) as [txtCFISPS]" & _
'[CSS]
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Head')) as [txtCSSH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Manager')) as [txtCSSSRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Manager')) as [txtCSSMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Supervisor')) as [txtCSSSRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Supervisor')) as [txtCSSSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Jr. Supervisor')) as [txtCSSJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Staff')) as [txtCSSSRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Staff')) as [txtCSSSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Jr. Staff')) as [txtCSSJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Project Staff')) as [txtCSSPS]" & _
" From tblEmployees"

由于

2 个答案:

答案 0 :(得分:2)

select department, p_level, count(*) as num_employees
from tblemployees
group by department, p_level

按部门和p_level(所有组合)计算员工数量

如果需要在每行返回这些别名,请在数据库中创建另一个表,三列:

部, p_level, count_alias

将每个部门和p_level组合放在一行,将别名放到count_alias列中。

然后,当你运行我给你的查询时,将tblemployees加入上面的3列表(使用department和p_level)并返回count_alias。

答案 1 :(得分:0)

使用SQL IN子句

e.g。

SELECT
  P_Level,
  COUNT(Employee_Name) AS COUNT_Employee_Name,
FROM tblEmployees
WHERE Department = 'Revenue Assurance'
  AND P_Level IN ('Head (Senior)', 'Head', 'Sr. Manager', 'Manager', 'Sr. Supervisor', 'Supervisor' ... )
GROUP BY P_Level