Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

dinzana

Programmer
Nov 21, 2002
118
US
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!

empweeknum_k8rp0g.png
 
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
 
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: Just skip over the VFP portion of the article.

Tamar
 
Thanks to you both for great solutions and speedy fast, thanks again!!!

D
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top