2017-05-08 129 views
1

我有一個查詢,其中我想從我的ROW_NUMBER()選擇中獲取最近的日期。我在ORDER BY條款中嘗試了MAX()DESC。它沒有顯示最近的日期爲RowNum 1SQL Row_Number()不排序日期

這是我的查詢:

;WITH cte3 AS 
(  
    SELECT 
     o.PartNo, 
     o.JobNo, 
     MAX(tt.TicketDate) as rawr, 
     ROW_NUMBER() OVER (PARTITION BY o.JobNo, o.PartNo 
          ORDER BY tt.TicketDate DESC) as RowNum  
    FROM 
     OrderDet AS o  
    INNER JOIN 
     TimeTicketDet AS tt ON o.JobNo = tt.JobNo  
    WHERE 
     o.Status = 'Open' 
    GROUP BY 
     tt.TicketDate, o.JobNo, o.PartNo 
)  
SELECT * 
FROM cte3 

當我把它給了我正確的結果,我會在CTE查詢添加一個WHERE RowNum = 1

從我目前的查詢,這是結果:

+--------+-------+-----------+--------+ 
| PartNo | JobNo | rawr | RowNum | 
+--------+-------+-----------+--------+ 
| 1234 | 20 | 5/30/2012 |  1 | 
| 1234 | 20 | 5/29/2012 |  2 | 
| 1234 | 20 | 5/25/2012 |  3 | 
| 1234 | 20 | 5/24/2012 |  4 | 
| 1234 | 20 | 5/23/2012 |  5 | 
| 1234 | 20 | 5/22/2012 |  6 | 
| 1234 | 20 | 5/16/2012 |  7 | 
| 1234 | 20 | 5/15/2012 |  8 | 
| 1234 | 20 | 5/14/2012 |  9 | 
| 1234 | 20 | 5/11/2012 |  10 | 
| 1234 | 20 | 5/10/2012 |  11 | 
| 1234 | 20 | 5/9/2012 |  12 | 
| 1234 | 20 | 3/27/2015 |  13 | 
| 1234 | 20 | 1/3/2013 |  14 | 
| 1234 | 20 | 1/2/2013 |  15 | 
+--------+-------+-----------+--------+ 

RowNum = 13是最近的日期。我是否會錯誤地或不正確地將我的日期轉換成類型?

編輯:

TimeTicketDet表的示例數據:

+------------+-------+ 
| TicketDate | JobNo | 
+------------+-------+ 
| 5/9/2012 | 20 | 
| 5/10/2012 | 20 | 
| 5/24/2012 | 20 | 
| 3/27/2015 | 20 | 
| 5/22/2012 | 20 | 
| 5/10/2012 | 20 | 
| 5/11/2012 | 20 | 
| 5/9/2012 | 100 | 
| 5/10/2012 | 100 | 
| 5/24/2012 | 100 | 
| 3/27/2015 | 100 | 
| 5/22/2012 | 100 | 
| 5/10/2012 | 100 | 
| 5/11/2012 | 100 | 
+------------+-------+ 

OrderDet表的示例數據:

+--------+--------+-------+ 
| PartNo | Status | JobNo | 
+--------+--------+-------+ 
| 1234 | Open | 20 | 
| 1234 | Open | 100 | 
+--------+--------+-------+ 

所需的結果:

+--------+------------+-------+--------+ 
| PartNo | TicketDate | JobNo | RowNum | 
+--------+------------+-------+--------+ 
| 1234 | 3/27/2015 | 20 |  1 | 
| 1234 | 3/27/2015 | 100 |  1 | 
+--------+------------+-------+--------+ 
+7

關於什麼是'TicketDate'列的數據類型?它可能是一個字符串/ varchar?它看起來不像基於排序的日期時間。 – ollie

+2

看起來就像你的「Group By」不應該有'tt.TicketDate'。沒有這個嘗試。 – RBarryYoung

+0

當我使用row_number這樣的事情時,我不使用group by或aggregates。 我只是在哪裏RowNum = 1 –

回答

2

正如我在my comment提到的,由於你的TicketDate列是一個字符,你需要convert itdatetime爲了通過實際日期來排序。現在,你正在通過字符串值來排序它是不正確的。

我建議你改變你的查詢是這樣的:

;WITH cte3 AS 
(  
    SELECT 
     o.PartNo, 
     o.JobNo, 
     MAX(tt.TicketDate) as rawr, 
     ROW_NUMBER() OVER (PARTITION BY o.JobNo, o.PartNo 
          ORDER BY cast(tt.TicketDate as datetime) DESC) as RowNum  
    FROM 
     OrderDet AS o  
    INNER JOIN 
     TimeTicketDet AS tt ON o.JobNo = tt.JobNo  
    WHERE 
     o.Status = 'Open' 
    GROUP BY 
     cast(tt.TicketDate as datetime), o.JobNo, o.PartNo 
)  
SELECT * 
FROM cte3 
where RowNum = 1; 

這裏是一個demo。通過在row_number中將char轉換爲datetime,您將按日期而不是字符串對數據進行排序。

此外,你並不真正需要的max()GROUP BY因爲由鑄造TicketDate爲datetime將返回正確的行:

;WITH cte3 AS 
(  
    SELECT 
     o.PartNo, 
     o.JobNo, 
     tt.TicketDate as rawr, 
     ROW_NUMBER() OVER (PARTITION BY o.JobNo, o.PartNo 
          ORDER BY cast(tt.TicketDate as datetime) DESC) as RowNum  
    FROM 
     #OrderDet AS o  
    INNER JOIN 
     #TimeTicketDet AS tt ON o.JobNo = tt.JobNo  
    WHERE 
     o.Status = 'Open' 
)  
SELECT * 
FROM cte3 
where RowNum =1; 
+0

你不需要爲'MAX(cast(tt.TicketDate as datetime))'添加劇組嗎? –

+0

這就是訣竅!謝謝!我會在這個數據庫中更好地瞭解一些奇怪的東西,比如字符串作爲日期和什麼。我非常感謝你的幫助。 –

+0

@JuanCarlosOropeza你是對的,我其實是在向我的回答添加這個的過程 – ollie

1

由於奧利建議你可以CAST你的字符串DATETIME而你不需要額外的Group By

SQL DEMO

;WITH cte3 AS 
(  
    SELECT 
     o.PartNo, 
     o.JobNo, 
     tt.TicketDate as rawr, 
     ROW_NUMBER() OVER (PARTITION BY o.JobNo, o.PartNo 
          ORDER BY cast(tt.TicketDate as datetime) DESC) as RowNum  
    FROM OrderDet AS o  
    JOIN TimeTicketDet AS tt 
     ON o.JobNo = tt.JobNo  
    WHERE 
     o.Status = 'Open' 
)  
SELECT * 
FROM cte3 
WHERE RowNum = 1 

輸出

enter image description here

+0

感謝您的幫助,並跟進每一條評論。 –