2012-04-20 40 views
3

我的問題一組中添加序列號爲每個元素是非常相似的一個張貼在這個環節 - How to add sequence number for groups in a SQL query without temp tables如何使用SQL查詢,而無需臨時表

但是,我需要枚舉組的發生。最終的輸出是這樣的:

Record Group  GroupSequence 
-------|---------|-------------- 
1  Chickens 1 
2  Chickens 2 
3  Cows  1 
4  Horses 1 
5  Horses 2 
6  Horses 3 

此外還必須在Oracle SQL中完成。有任何想法嗎?

回答

11

也許是這樣的:

SELECT 
    ROW_NUMBER() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence1, 
    RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence2, 
    DENSE_RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence3, 
    Table1.Group, 
    Table1.Record 
FROM 
    Table1 

GroupSequence1GroupSequence2GroupSequence3會得到你想要的輸出。

+0

非常感謝!問題解決了! – user1346265 2012-04-20 13:09:00

+0

沒問題。很高興提供幫助:P – Arion 2012-04-20 13:23:33

+0

「PARTITION BY」是缺失的鏈接!謝謝! – Chop 2017-12-04 14:04:31

相關問題