我得到以下错误“此子查询最多可以返回一条记录”

时间:2016-04-19 19:21:33

标签: ms-access

此代码用于提取项目状态,2个最近日期并返回上次报告3次和最近报告的所有项目。然而,我不断得到这个子查询只能返回一条记录并且查询需要永远运行的错误。请帮忙。

SELECT RedProjectHistorical.[Project Name]
FROM (
    select 
        'Project Name',
    max('Report Date') as maxrd
    from RedProjectHistorical
    group by RedProjecthistorical.'Project Name'
) AS RedProjectHistorical 
WHERE 3 = (
    SELECT 
       t2.[Internal Project Status Value] 
    from RedProjectHistorical as t2
    where 
        t2.[Project Name] = RedProjectHistorical.[Project Name] 
        and t2.[Report Date] = RedProjectHistorical.maxrd
) 
and 3 = (
    select top 1 t2.[Internal Project Status Value] 
    from RedProjectHistorical t2 
    where 
        t2.[Project Name] = RedProjectHistorical.[Project Name] 
        and t2.[Report Date] <RedProjectHistorical.maxrd 
    order by t2.[Report Date] desc
); `

1 个答案:

答案 0 :(得分:1)

尝试使用这样的存在条件:

exists(SELECT 1
  from RedProjectHistorical as t2
  where t2.[Project Name] = RedProjectHistorical.[Project Name] 
    and t2.[Report Date] = RedProjectHistorical.maxrd
    and t2.[Internal Project Status Value] = 3)