Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL查询:尝试使用其他参数提取最近的_Sql Server - Fatal编程技术网

Sql server SQL查询:尝试使用其他参数提取最近的

Sql server SQL查询:尝试使用其他参数提取最近的,sql-server,Sql Server,我在一个学校系统中工作,学生数据保存在数据库中。至少每年(有时一年多次)为该学生创建一行代表新“文档”的新数据。我正在尝试编写一个查询,如果该行满足其他条件(例如,它需要是“compsped”类型,并且需要具有“F”或“I”状态),则为该学生提取最新的信息行。当我运行我在下面编写的查询时,它运行得非常好,但似乎缺少一些数据。我认为它丢失一些数据的原因是因为它首先查找最新的文档,然后筛选出不符合其他条件的文档。相反,我希望它首先筛选出不符合其他条件的文档,并从该列表中提取最近的一行。我们使用的是S

我在一个学校系统中工作,学生数据保存在数据库中。至少每年(有时一年多次)为该学生创建一行代表新“文档”的新数据。我正在尝试编写一个查询,如果该行满足其他条件(例如,它需要是“compsped”类型,并且需要具有“F”或“I”状态),则为该学生提取最新的信息行。当我运行我在下面编写的查询时,它运行得非常好,但似乎缺少一些数据。我认为它丢失一些数据的原因是因为它首先查找最新的文档,然后筛选出不符合其他条件的文档。相反,我希望它首先筛选出不符合其他条件的文档,并从该列表中提取最近的一行。我们使用的是SQLServer20016。希望这是有道理的。如果没有,请提问。谢谢

SELECT evaluationreports1.Status, 
evaluationreports1.EvalDueDate, 
evaluationreports1.EvalRptDate, 
evaluationreports1.StudentID, 
evaluationreports1.TypeAbbrev
FROM PlansAnoka.dbo.evaluationreports evaluationreports1
WHERE evalrptdate = (select max(evalrptdate) from evaluationreports where studentid = evaluationreports1.studentid) 
AND (evaluationreports1.TypeAbbrev='CompSpEd') 
AND (evaluationreports1.Status In ('F','I'))

对现有查询的此修改将起作用:

SELECT evaluationreports1.Status, 
evaluationreports1.EvalDueDate, 
evaluationreports1.EvalRptDate, 
evaluationreports1.StudentID, 
evaluationreports1.TypeAbbrev
FROM PlansAnoka.dbo.evaluationreports evaluationreports1
WHERE evalrptdate = (
  select max(evalrptdate) 
  from evaluationreports i 
  where i.studentid = evaluationreports1.studentid 
  and i.TypeAbbrev='CompSpEd'
  and i.Status In ('F','I')
  )

另一种方法是使用

with cte as (
  select  *
      , rn = row_number() over (
              partition by studentid 
              order by evalrptdate desc
            )
    from PlansAnokt.dbo.evaluationreports t
    where t.TypeAbbrev='CompSpEd'
      and t.Status in ('F','I')
)
select *
  from cte
  where rn = 1
使用

with cte as (
  select  *
      , rn = row_number() over (
              partition by studentid 
              order by evalrptdate desc
            )
    from PlansAnokt.dbo.evaluationreports t
    where t.TypeAbbrev='CompSpEd'
      and t.Status in ('F','I')
)
select *
  from cte
  where rn = 1
或不带
cte

select *
  from (
    select  *
      , rn = row_number() over (
              partition by studentid 
              order by evalrptdate desc
            )
    from PlansAnokt.dbo.evaluationreports t
    where t.TypeAbbrev='CompSpEd'
      and t.Status in ('F','I')
      ) as cte
  where rn = 1

谢谢我试过你写的第一个建议,我得到了一个“靠近“)”的语法不正确”错误。“我将继续寻找,看看是否能找到答案。”bosstone75更正了第一次查询的错误。现在试一试。@SqlZim对你很好。我花了太多的时间在我的编辑器里修饰CTE,而你却抢先到了:-)哈哈!很好。第一个选择对我来说很有效,我可以把我的头绕在它周围。我是SQL新手,我还没有体验过行数,所以这有点超出我的理解范围!谢谢你的帮助@Bosstone很乐意帮忙!