2013-10-28 45 views
1

我有這樣顯示記錄,並且還刪除複製數據

+------+------+------+------+ 
| col1 | col2 | col3 | rank | 
+------+------+------+------+ 
| 1 | A | X | 4 | 
| 2 | C | Y | 3 | 
| 2 | C | Y | 3 | 
|  | A | X | 3 | 
| 1 | B | Z | 2 | 
+------+------+------+------+ 

(5行)的表

我需要這樣

+------+------+------+------+ 
| col1 | col2 | col3 | rank | 
+------+------+------+------+ 
| 1 | A | X | 4 | 
| 2 | C | Y | 3 | 
| 1 | B | Z | 2 | 
+------+------+------+------+ 

使得O/P我寫下如下的查詢

select col1,col2,col3,rank,dense_rank() over(order by rank desc) from table1; 

b UT斯達康公司沒有給予適當的O/P

回答

0

看起來像您需要使用MAX()聚集:

select 
    col1,col2,col3, 
    max(rnk) 
from table1 
group by col1,col2,col3 

如果你能有col1不同值的col2, col3一個組合,那麼distinct on是你所需要的:

select distinct on (col2, col3) 
    col1,col2,col3, 
    rnk 
from table1 
order by col2, col3, rnk desc 

sql fiddle demo

+0

感謝越來越o/p ... – user2793872

1

試試這個!

select a.col1,a.col2,a.col3,max(a.rank) as rank 
      from [dbo].[5] a join [dbo].[5] b 
       on a.col1=b.col1 group by a.col1,a.col2,a.col3 
0

下應與你在找什麼:

select col1,col2,col3,rank,dense_rank() over(order by rank desc) from table1 
WHERE col1 IS NOT NULL 
GROUP BY 1, 2, 3, 4; 

您可以通過子句,如果你想要一個也使用數字別名,在您的訂單。