子查询错误中有多个记录

时间:2014-05-13 14:00:27

标签: sql ms-access subquery

我正在使用MS Access 2010。

我正在尝试通过连接同一个表的不同部分来创建一个包含三列的表

数据集是

ID Date      Job Title                                              Count of ID
-- --------- ------------------------------------------------------ -----------
 1 13-May-14 Security Guards                                                 15
 2 13-May-14 Interpreters and Translators                                    22
 3 13-May-14 Laborers and Freight, Stock, and Material Movers, Hand           1
 4 13-May-14 Maintenance and Repair Workers, General                         23
 5 13-May-14 Protective Service Workers, All Other                           54
 6 13-May-14 Logisticians                                                    65
 7 02-Apr-14 Security Guards                                                 88
 8 02-Apr-14 Interpreters and Translators                                    22
 9 02-Apr-14 Laborers and Freight, Stock, and Material Movers, Hand          44
10 02-Apr-14 Maintenance and Repair Workers, General                         77
11 02-Apr-14 Protective Service Workers, All Other                           66
12 02-Apr-14 Logisticians                                                     8

,查询是

SELECT ctrjob.[Job Title], ctrjob.[Count of ID], 

(Select ctrjob.[Count of ID] from  ctrjob 
where ctrjob.[Job Title] = ctrjob.[Job Title] and ctrjob.[Date] = #4/2/2014#)

FROM ctrjob where [ctrjob].[Date] = #5/13/2014# ; 

我希望我的已完成的表创建一个表格,将2014年5月13日每个职位的承包商数量放在2014年4月2日的数字旁边。目标是比较工人数量的变化。

但我不断收到错误消息

  

子查询最多可以返回一条记录

但我想要的是它从另一个日期返回一个相应的记录。每次执行时,它应仅返回2014年4月2日作业类别X中的数字。

我不想使用转换/交叉表,因为完整的数据集包含太多日期,我想使用表单最后切换日期。所有日期的职位列表都是相同的。我正在使用MS Access 2010。

1 个答案:

答案 0 :(得分:1)

使用连接代替子查询:

SELECT a.[Job Title], a.[Count of ID], b.[Count of ID]
FROM ctrjob a INNER JOIN
     ctrjob b ON a.[Job Title] = b.[Job Title]
WHERE a.Date = #4/2/2014# AND
      b.Date = #5/13/2014#