請幫我解決我在工作期間遇到的問題。我正在使用SQL Server,並且我意識到使用遊標可以實現這一點,但我確信有一種方法可以在SQL中使用簡單的查詢來完成它,但是我的大腦燈泡不想打開。讓我用一個例子來解釋我的問題。選擇整個表格,但在特定列中有唯一的值
我有一個表是這樣的:
postedby | date | comment |
1 | 01.01.2012 | sth sth |
2 | 01.01.2012 | sth sth |
3 | 01.01.2012 | sth sth |
2 | 01.01.2012 | sth sth |
3 | 02.01.2012 | sth sth |
2 | 03.01.2012 | sth sth |
2 | 05.01.2012 | sth sth |
我想做到的是讓所有的職位,但一個爲每個用戶(postedby列),日期必須是最新的,當然顯示評論。
我曾嘗試做:
Select distinct postedby, date, comment
,但沒有工作,據我所知,每列不同的作品,所以,如果在2行postedby是相同的,但意見不同,它會將其視爲distincts
我曾嘗試做:
Select postedby,date,comment group by postedby
(不要理會FROM子句) 給我的錯誤或聚集,所以我試圖 select postedby,min(date) group by postedby
- 當然工作s,但我無法得到評論。
我應該以某種方式使用聚合查詢嗎?或者我錯過了什麼?
一個問題,我知道這是適用於oracle,但與sql服務器? – ssedano 2012-01-09 18:16:20
@UdoFholl它肯定沒有 – 2012-01-09 18:17:02
@UdoFholl - 這將適用於SQL Server 2005+,所以我在舉辦我的+1時澄清了什麼SQL Server版本正在使用 – Lamak 2012-01-09 18:17:41