INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help with populating a column with a sequence number (I think)
2

Need help with populating a column with a sequence number (I think)

Need help with populating a column with a sequence number (I think)

(OP)
Hello there and thank you in advance. I am trying to populate a column (week_number) in my employee table. I need to insert a 1 at the first entry_date and then increment by 1 for every entry_date after, in sequence for EACH employee. The issue is, each employee can have a different entry_date from another employee. How do I accomplish this in SQL to include in a view? thanks for looking!

RE: Need help with populating a column with a sequence number (I think)

CODE

SELECT Employee_Id,
       Entry_Date,
       ROW_NUMBER() OVER (PARTITION BY Employee_Id ORDER BY Employee_Id, Entry_Date) AS Week_Number
FROM... 
NOT TESTED!

Borislav Borissov
VFP9 SP2, SQL Server

RE: Need help with populating a column with a sequence number (I think)

You need to use OVER with ROW_NUMBER(). This is untested, but should give you the idea.

CODE

UPDATE YourTable
   SET Week_Number = RecNos.Week_Number
   FROM (SELECT Employee_ID, Entry_Date, 
                ROW_NUMBER() OVER (ORDER BY Entry_Date) AS Week_Number
           FROM YourTable) RecNos
   WHERE YourTable.Employee_ID = RecNos.Employee_ID 
     AND YourTable.Entry_Date = RecNos.Entry_Date 

I've written about this use of OVER: http://www.tomorrowssolutionsllc.com/Articles/Gett... Just skip over the VFP portion of the article.

Tamar

RE: Need help with populating a column with a sequence number (I think)

(OP)
Thanks to you both for great solutions and speedy fast, thanks again!!!

D

RE: Need help with populating a column with a sequence number (I think)

Just a side note, this should be obvious from the code: ROW_NUMBER just numbers rows. That this row number corresponds to week numbers relies on the rows having entry_dates in week distances. If you ever have a gap of a missing entry date, the row numbers will not reflect that by also having that gap.

It's often sufficient to rely on requirements fulfilled from the outset, just watch out the data is not inseparably connected.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

Resources

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