选择最新记录

时间:2017-04-07 18:44:04

标签: sql-server reportbuilder3.0

我有两张桌子(期刊和事件)。每个事件可能有多个日记帐分录。我想选择记录和最新的期刊数据。

底部的where部分是我希望看到的事件的过滤器。其中,我希望与最新的日记帐分录相关联的期刊值。

这是我在这里找到的代码的混合物,但是当我运行它时,我得到了一个"数据集' DataSet1'的查询执行失败。不幸的是,我无法访问日志文件以查看是否有线索。

感谢任何帮助。我想我可能会把它嵌套错误。

SELECT
b.IncidentNumber
,a.Subject
,a.CreatedDateTime
,b.SubCategory
,b.EffectiveDueDate
,b.NextActionDate
,b.ProfileFullName

FROM 
(
SELECT
  b.IncidentNumber
 ,a.Subject
 ,a.CreatedDateTime
 ,rn = row_number() OVER (PARTITION by b.IncidentNumber ORDER BY 
   a.CreatedDateTime DESC)
 ,b.SubCategory
 ,b.EffectiveDate
 ,b.NextActionDate
 ,b.ProfileFullName 


FROM
Journal a LEFT JOIN Incident b ON 
a.ParentRecordNumber = b.IncidentNumber

WHERE a.Category LIKE '%KANBAN%'
AND (b.Status LIKE' %Waiting%' OR b.status LIKE '%Active%')
AND b.SubCategory <> 'User Termination'
AND b.SubCategory <> 'Res Temp Termination'
AND a.Subject LIKE 'UP |%'
) X
WHERE rn = 1

2 个答案:

答案 0 :(得分:1)

少数事情:

  • 外部选择的最大值应来自内联视图别名&#34; X&#34;没有。或b。因为这些别名仅在内部查询的范围内。 (除了使用coorlation,但我认为只有1级)
  • 您需要right join而不是左侧或更改表格的顺序。我相信你想要所有的事件和最近的期刊;并非所有期刊和相关事件都存在。因此我改变了顺序。
  • 最后使用外连接时,只能对外连接的所有记录表进行限制。 Where子句标准OUTER连接表将导致外连接生成的空记录被排除。要解决此问题,您必须将限制条件移至联接或使用&#39;或&#39;检查null的语句(将其移动到连接更干净)。可以想象它在连接发生之前应用了限制,因此保留了事件中的空记录。否则外连接通过排除那些不在两个表中的记录来模拟INNER JOIN(或者在这种情况下是在事件中而不是在日记中)

SELECT x.IncidentNumber  --alias x not a/b as the from is aliased as 'X'
     , x.Subject
     , x.CreatedDateTime
     , x.SubCategory
     , x.EffectiveDueDate
     , x.NextActionDate
     , x.ProfileFullName
FROM (SELECT b.IncidentNumber
           , a.Subject
           , a.CreatedDateTime
           , rn = row_number() OVER (PARTITION by b.IncidentNumber 
                                     ORDER BY a.CreatedDateTime DESC)
           , b.SubCategory
           , b.EffectiveDate
           , b.NextActionDate
           , b.ProfileFullName 
       FROM Incident b --switched the order I think you want all incidents and if a journal exists it's value.
       LEFT JOIN Journal a  
         ON a.ParentRecordNumber = b.IncidentNumber
         -- Since A is on the if match found to B, we need to move this to the join or we lose the records created from the outer join.
        AND a.Category LIKE '%KANBAN%'
        AND a.Subject LIKE 'UP |%'
         --moved some where clause criteria to the join Since B is on the "all records side" of the outer join we can leave B in the where clause.
        WHERE (b.Status LIKE' %Waiting%' OR b.status LIKE '%Active%')
          AND b.SubCategory <> 'User Termination'
          AND b.SubCategory <> 'Res Temp Termination') X
WHERE rn = 1

如果您没有从此处获取记录,那么我将开始删除一些限制条件以确保查询按预期运行,然后重新加入限制以查看导致没有记录的内容被发现。

答案 1 :(得分:0)

我终于让这份报告按预期工作了。需要几次迭代才能使查询正常工作,但它现在正在做它应该做的事情。非常感谢你的帮助。如果没有它,我将永远不会到那里!

SELECT x.IncidentNumber
 , x.Subject
 , x.CreatedDateTime
 , x.SubCategory
 , x.ProfileFullName
 , x.PropertyNumber
 , x.Status
 , x.EffectiveDueDate

FROM (SELECT b.IncidentNumber
       , a.Subject
       , a.CreatedDateTime
       , rn = row_number() OVER (PARTITION by b.IncidentNumber 
                                 ORDER BY a.CreatedDateTime DESC)
       , b.SubCategory
       , b.ProfileFullName
       , b.PropertyNumber
   , b.Status
       , b.EffectiveDueDate

   FROM Incident b 
   RIGHT JOIN Journal a  
      ON a.ParentRecordNumber = b.IncidentNumber

      AND a.Category LIKE '%KANBAN%'
      AND a.Subject LIKE 'UP |%'     
      WHERE (b.Status LIKE' %Waiting%' OR b.status LIKE '%Active%')
      ) x

WHERE rn = 1