0
我的查詢返回的數據,如:如何返回具有最新日期的行?
ID | ProcessID | Type| Value | Date
--------------------------------------------------
1 | 2 | A | 10 | 2/23/2016 10:10:42 AM
1 | 2 | B | 20 | 2/23/2016 10:10:42 AM
1 | 2 | C | 30 | 2/23/2016 10:10:42 AM
1 | 1 | A | 11 | 1/6/2016 12:48:04 PM
1 | 1 | B | 21 | 1/6/2016 12:48:04 PM
1 | 1 | C | 31 | 1/6/2016 12:48:04 PM
2 | 4 | A | 10 | 7/21/2016 11:28:22 AM
2 | 4 | B | 20 | 7/21/2016 11:28:22 AM
2 | 4 | C | 30 | 7/21/2016 11:28:22 AM
2 | 3 | A | 11 | 6/21/2016 09:41:07 AM
2 | 3 | B | 21 | 6/21/2016 09:41:07 AM
2 | 3 | C | 31 | 6/21/2016 09:41:07 AM
可是,我真的只在乎獲取最新的類型的A,B和C這樣的數據,我的回報應該是這樣的:
ID | ProcessID | Type| Value | Date
--------------------------------------------------
1 | 2 | A | 10 | 2/23/2016 10:10:42 AM
1 | 2 | B | 20 | 2/23/2016 10:10:42 AM
1 | 2 | C | 30 | 2/23/2016 10:10:42 AM
2 | 4 | A | 10 | 7/21/2016 11:28:22 AM
2 | 4 | B | 20 | 7/21/2016 11:28:22 AM
2 | 4 | C | 30 | 7/21/2016 11:28:22 AM
我試過在select子句中使用max,但那不起作用。
這裏是我的SQL,限制例如一個 「ID」 的緣故,實際回報樣品一起:
select f.folderrsn,fpi.processrsn,fpi.infocode, fpi.infovalue, fp.enddate
from folderprocessinfo fpi
join folderprocess fp on fp.processrsn = fpi.processrsn
join folder f on f.folderrsn = fp.folderrsn
where fpi.infocode in (51437,51438,51439)
and trunc(f.issuedate) > '01-JAN-16'
and not fpi.infovalue is null
and f.folderrsn = 11179547 --limited to one "ID" for example
order by fp.enddate asc
返回:
"FOLDERRSN","PROCESSRSN","INFOCODE","INFOVALUE","ENDDATE"
"11179547","37159700","51437","91","6/2/2015 10:27:46 AM"
"11179547","37159700","51438","0","6/2/2015 10:27:46 AM"
"11179547","37159700","51439","145.5","6/2/2015 10:27:46 AM"
"11179547","37540455","51437","91","7/24/2015 6:44:08 AM"
"11179547","37540455","51438","0","7/24/2015 6:44:08 AM"
"11179547","37540455","51439","145.5","7/24/2015 6:44:08 AM"
謝謝你,戈登。我是SQL新手,我正在尋找改進方法。你似乎有經驗。你會推薦格式化我的查詢,就像你在第一組括號中做的那樣,以提高可讀性嗎? –
這不是完全的解決方案。我應該爲每個processrsn(processid)準確地獲取三行數據,每個infocode(type)都有一行數據。我可以通過一次查詢一個信息代碼來獲取數據,但爲了鍛鍊,您是否還有其他想法? –
@Chris:考慮編輯你的問題,使你的需求更清晰。你剛纔評論的關於'processid'的問題在你的問題的任何地方都沒有明確說明或說明。 – sstan