2017-04-07 101 views
1

我有兩個表格(雜誌和事件)。每個事件可能有多個日記條目。我想選擇記錄和最新的日誌數據。選擇最新記錄

底部的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 
+0

在SSMS中運行查詢,看看出現了什麼錯誤。 –

+1

您的頂級選擇不會使用b或僅限x。 – xQbert

回答

1

幾件事情:。

  • 最外層選擇的值應該是從別名內嵌視圖「 X「沒有a。o r b。因爲這些別名僅在內部查詢的範圍內。 (除了使用聯合,但只有1級我相信)
  • 您或者需要right join而不是左邊或更改表的順序。我相信你想要所有的事件和最近的最新期刊;不是所有的期刊和相關的事件(如果存在的話)。因此我改變了順序。
  • 最後,在使用外連接時,只能對外連接的所有記錄表進行限制。 Where子句條件中,OUTER連接的表將導致排除外連接生成的空記錄。爲了解決這個問題,你必須將限制條件移動到連接處,或者使用'或'語句來檢查是否爲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 

如果你沒有得到從這裏的記錄,然後我會開始刪除一些限制標準,以確保根據需要查詢是否正常工作,然後添加回去極限,看看是什麼導致沒有記錄可尋。

+0

非常好,我會試一試,讓你知道它是怎麼回事。 –

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