views:

164

answers:

2

Is it possible to add a identity column to a GROUP BY so that each duplicate has a identity number?

My original data looks like this:

1    AAA  [timestamp]
2    AAA  [timestamp]
3    BBB  [timestamp]
4    CCC  [timestamp]
5    CCC  [timestamp]
6    CCC  [timestamp]
7    DDD  [timestamp]
8    DDD  [timestamp]
9    EEE  [timestamp]
....

And I want to convert it to:

1    AAA   1
2    AAA   2
4    CCC   1
5    CCC   2
6    CCC   3
7    DDD   1
8    DDD   2
...

The solution was:

CREATE PROCEDURE [dbo].[RankIt]
AS
BEGIN
SET NOCOUNT ON;

SELECT  *, RANK() OVER(PARTITION BY col2 ORDER BY timestamp DESC) AS ranking 
FROM MYTABLE;

END
+4  A: 

You could try using ROW_NUMBER if you are using Sql Server 2005

DECLARE @Table TABLE(
        ID INT,
        Val VARCHAR(10)
)

INSERT INTO @Table SELECT 1,'AAA'
INSERT INTO @Table SELECT 2,'AAA'
INSERT INTO @Table SELECT 3,'BBB' 
INSERT INTO @Table SELECT 4,'CCC' 
INSERT INTO @Table SELECT 5,'CCC' 
INSERT INTO @Table SELECT 6,'CCC' 
INSERT INTO @Table SELECT 7,'DDD' 
INSERT INTO @Table SELECT 8,'DDD' 
INSERT INTO @Table SELECT 9,'EEE' 

SELECT  *,
        ROW_NUMBER() OVER(PARTITION BY VAL ORDER BY Val)
FROM    @Table
astander
i tried SELECT *, RANK() OVER (ORDER BY col2) AS ranking FROM table and it crashes my SQL2005 management studio...
djangofan
Remember to use **PARTITION BY** as in the example.
astander
SELECT *,ROW_NUMBER() OVER (PARTITION BY col2) also crashes my sql managment studio. this rank and partition stuff is wierd.... i cant figure it out.
djangofan
What do you mean by **CRASH** ? Like fatal error, restart the app?
astander
got it working with difficulty. thanks very much astander... you got me looking in the right place. i'll update my question with the answer.
djangofan
+3  A: 
create table #testalot
(
  [id] int identity,
  data varchar(50)
)

insert #testalot (data) values('AAA')
insert #testalot (data) values('AAA')
insert #testalot (data) values('BBB')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('DDD')
insert #testalot (data) values('DDD')

select *,ROW_NUMBER() OVER(PARTITION BY data ORDER BY data DESC) AS 'Number'
 from #testalot

 drop table #testalot

returns

id  data Number
1   AAA  1
2   AAA  2
3   BBB  1
4   CCC  1
5   CCC  2
6   CCC  3
7   DDD  1
8   DDD  2
Hogan
sigh... click the save and the page comes up with someone else entering the same answer 3 mins earlier.
Hogan
i never could get it to work with ROW_NUMBER(). i ended up using RANK() .
djangofan
-shrug- I tested the code above. They actually give different results when you have ties.
Hogan