站內搜尋
Friday, March 29, 2013
[SQL SERVER] 排除重複資料僅留下最新一筆資料的語法
假設某資料表為如下,會依照寫入順序自動產生no:
no | name
------------
01 | aa
02 | aa
03 | bb
(略)
71 | cc
72 | aa
73 | bb
74 | bb
如何篩掉name欄位中重複的資料,並依照no留下最新的一筆,產生類似下面的結果
no | name
------------
71 | aa
72 | cc
74 | bb
SELECT b.name, b.no FROM (
SELECT name, max(no) AS max_no
FROM mytable
GROUP BY name
) AS a INNER JOIN mytable AS b ON a.max_no=b.no
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment