此代码用于提取项目状态,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
); `
答案 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)