Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I love the structure of the site. You start at the top, and drill down to what you want. Maybe I've been using Unix too long... :-) "

Geography

Where in the world do Tek-Tips members come from?
Khanson2 (TechnicalUser)
1 May 12 10:46
I have basic data like this.
and I want to add the column you see out the far right.
Identity        ID        Contactid            Desired Column
1               A            123                   1
2               A            124                   1   
3               A            125                   1
4               B            200                   4
5               B            201                   4
6               C            354                   6
7               C            368                   6
8               C            370                   6
9               C            395                   6
10              D            400                   10

All the desired column is doing is getting the min identity valuewhere the ID columns starts.
Any ideas?

Thanks,
wbodger (Programmer)
1 May 12 11:59
Is this something that you want to happen automatically, something you just need to run once or you will run regularly?

wb
Khanson2 (TechnicalUser)
1 May 12 12:03
I have all the data to this point in a temp table and I need to add this column to the data set, so I can then get the ordinal # for these events to better analyze it.
The data will change daily, so I just need the method of creating this information in sql when I run the scripts.

Thanks,
Helpful Member!  wbodger (Programmer)
1 May 12 12:12
well, something like this will give you the value you want to insert into the column

CODE

select a.id, MIN(a.autoid)
from [dbTesting].[dbo].[TestTable] a
    join [dbTesting].[dbo].[TestTable] b on a.id=b.id
group by a.id

wb
markros (Programmer)
1 May 12 12:21

CODE

select *, rank() over (order by ID) as [Desired Column]
from yourtable -- SQL Server 2005 and up

PluralSight Learning Library

Khanson2 (TechnicalUser)
1 May 12 12:37
Thanks wbodger. I used this logic and then joined back to the main dataset and all worked.

 
markros (Programmer)
1 May 12 12:42
If you're using SQL Server 2005 and up then use built-in RANK() window function.

PluralSight Learning Library

Khanson2 (TechnicalUser)
1 May 12 12:47
Yes that's right Markros, sorry, when I first used yours it didn't look right, but after trying again I get desired results..

Thanks!
wbodger (Programmer)
1 May 12 12:49
I'm new to the post SQL2K versions, how exactly would you use the rank() function? I cannot get it to act nicely.

wb
wbodger (Programmer)
1 May 12 12:54
Oh. Nevermind. I was trying to do too much. That's pretty cool.

wb
wbodger (Programmer)
1 May 12 13:02
Now, why does it work? In my test table I names the identity column AutoID, which is what shows up in the [Desired Col] field, but there is no command for Rank to use this, is there? What does Rank() assume/default to?

wb
markros (Programmer)
1 May 12 14:59
In the OP sample the ID column has the same values first 3 rows have A in ID, next 2 B, etc.

So

CODE

     Rank Row_number Dense_Rank
 
A    1     1           1
A    1     2           1
A    1     3           1
B    4     4            2

PluralSight Learning Library

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close