November 2, 2007 at 4:39 am
Hi All,
I feel bad asking this cause I have seen it one this site before and have infact done this myself before...
Perhaps the pressure is getting to me he he he
I have one table as follows:
CREATE TABLE TableA
(Value CHAR(1) NOT NULL)
The values in side for this example are say
Value
a
b
b
c
c
d
I am trying to write a query that will return the following results
ID VALUE
1 a
1 b
2 b
1 c
2 c
1 d
OK I can do this using a loop very easily but I would like to avoid using a loop and def not use a cursor.
Any help or tips and ideas to put me in the right direction would be great!
Thanks
Chris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
November 2, 2007 at 5:11 am
select row_number() over(partition by Value order by Value) as ID,
Value
from TableA
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537November 2, 2007 at 5:26 am
Do you want 1 & 2 to be repeated for each character in the table?
November 2, 2007 at 5:34 am
Sorry, I misread the question....
What did we do before the days of Ranking functions???
November 2, 2007 at 5:44 am
HI MarkC ,
that is perfect 10000% correct and so simple.
Sorry I missed that. Didn't realise you could use partition and a order by in a over clause 🙂
thanks again.
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply