使用rownum的SQL Server同行连接不返回值

时间:2018-05-04 00:11:28

标签: sql join rownum

我试图在同一行中返回多个记录值,所以我想对同一个表使用多个连接。我需要前两个记录并排显示。这些连接成为'LatestNote'和'SecondLatestNote',使用RowNumber将它们分开。

非常简单的例子如下,但我没有得到任何结果。我在这里搞砸了什么?

带有不返回值的连接的SQL:

DECLARE @DateFrom AS DATETIME = CONVERT(DateTime, '2017-01-01 00:00:00.000', 120)
DECLARE @DateTo AS DATETIME = CONVERT(DateTime, '2018-01-01 00:00:00.000', 120)

SELECT
    LatestNote.NoteCode,
    LatestNote.NoteDate,           
    SecondLatestNote.NoteCode AS [NoteCode2nd],
    SecondLatestNote.NoteDate AS [NotDate2nd]

FROM Locations LOC


LEFT JOIN (
            SELECT TOP 2 LocationID, NoteID, Row_Number() OVER (ORDER BY Notedate DESC) AS RowNum
            FROM Notes(nolock) 
            WHERE NoteCode = 'NOTIFY' 
            AND NoteDate BETWEEN @DateFrom AND @DateTo
            ) 
                AS TopTwoNotes ON TopTwoNotes.LocationID = LOC.LocationID 

                LEFT JOIN Notes AS LatestNote ON LatestNote.NoteID = TopTwoNotes.NoteID AND TopTwoNotes.RowNum = 1

                LEFT JOIN Notes AS SecondLatestNote ON SecondLatestNote.NoteID = TopTwoNotes.NoteID AND TopTwoNotes.RowNum = 2 

WHERE LOC.LocationID = 308644

结果全为NULL,只返回一行,因为我正在使用LEFT JOIN。为什么这不起作用?

此SQL显示数据存在,结果如下:

SELECT TOP 2 LocationID, NoteID, Row_Number() OVER (ORDER BY NoteDate DESC) AS RowNum
            FROM Notes(nolock) 
            WHERE NoteCode = 'NOTIFY' 
            AND NoteDate BETWEEN @DateFrom AND @DateTo
            AND LocationID = 308644 

LocationID  NoteID  RowNum
308644  10291348    1
308644  10130566    2


SELECT
    NoteID,
    NoteCode,
    NoteDate,
    LEN(CAST(Note AS VARCHAR(8000))) AS [NoteCharCount]
FROM
    Notes
WHERE
    LocationID = 308644
    AND NoteDate BETWEEN @DateFrom AND @DateTo


NoteID  NoteCode    NoteDate    NoteCharCount
10130566    NOTIFY  2017-11-08  50
10291348    NOTIFY  2017-12-13  66

我必须遗漏一些东西 - 我不能像这样使用rownum加入吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您需要在子查询的where子句中添加条件LocationID = 308644,否则第一行和第二行可能不包含LocationID = 308644

然后您使用与LEFT JOIN不匹配的LocationID = 308644,因此会NULL行。

SELECT LatestNote.notecode, 
       LatestNote.notedate, 
       SecondLatestNote.notecode AS [NoteCode2nd], 
       SecondLatestNote.notedate AS [NotDate2nd] 
FROM   locations LOC 
       LEFT JOIN (SELECT TOP 2 locationid, 
                               noteid, 
                               Row_number() 
                                 OVER ( 
                                   ORDER BY notedate DESC) AS RowNum 
                  FROM   notes(nolock) 
                  WHERE  notecode = 'NOTIFY' 
                         AND notedate BETWEEN @DateFrom AND @DateTo AND LocationID = 308644) AS TopTwoNotes 
              ON TopTwoNotes.locationid = LOC.locationid 
       LEFT JOIN notes AS LatestNote 
              ON LatestNote.noteid = TopTwoNotes.noteid 
                 AND TopTwoNotes.rownum = 1 
       LEFT JOIN notes AS SecondLatestNote 
              ON SecondLatestNote.noteid = TopTwoNotes.noteid 
                 AND TopTwoNotes.rownum = 2 
WHERE  LOC.locationid = 308644 

修改

您可以在子查询上放locationsLEFT JOIN

SELECT LatestNote.notecode, 
       LatestNote.notedate, 
       SecondLatestNote.notecode AS [NoteCode2nd], 
       SecondLatestNote.notedate AS [NotDate2nd] 
FROM   (
        SELECT TOP 2 TopTwoNotes.locationid, 
                     TopTwoNotes.noteid, 
                     Row_number() 
                       OVER (ORDER BY TopTwoNotes.notedate DESC) AS RowNum 
        FROM   locations LOC 
               LEFT JOIN notes(nolock) TopTwoNotes
                       ON TopTwoNotes.locationid = LOC.locationid 
        WHERE  TopTwoNotes.notecode = 'NOTIFY' 
               AND TopTwoNotes.notedate BETWEEN @DateFrom AND @DateTo 
               AND LOC.locationid = 308644
        ORDER BY TopTwoNotes.notedate DESC
        ) AS TopTwoNotes 
       INNER JOIN notes AS LatestNote 
               ON LatestNote.noteid = TopTwoNotes.noteid 
                  AND TopTwoNotes.rownum = 1 
       INNER JOIN notes AS SecondLatestNote 
               ON SecondLatestNote.noteid = TopTwoNotes.noteid 
                  AND TopTwoNotes.rownum = 2 

答案 1 :(得分:0)

您可以使用System.out.println("The Longest String is: " + longString + " With The Index Of" + java.util.Arrays.asList(names).indexOf(longString)); 解决此问题:

apply