views:

36

answers:

1

I have the following tables with their columns (only the relevant columns are listed):

Entry
EntryID (int/auto-inc)
EmployeeNumber (int)
JustifyDate (datetime)
EntryDate (datetime)

Employee
EmployeeNumber (int)
CoreHourID (int)
WorkingFromHome (bit/bool)

Hour
EntryID (int)
InHour (datetime)
OutHour (datetime)

CoreHour
CoreHourID (int) - not unique
InHour (int)
OutHour (int)

EDIT: I had forgotten to mention that CoreHourID isn't a unique field, table can look like this:

+-----------+-------+-------+----------+
|CoreHourId |InHour |OutHour|Identifier|
+-----------+-------+-------+----------+
|    2      |  9    |  12   |    1     |
|    2      |  14   |  17   |    2     |
|    3      |  7    |  11   |    3     |
|    3      |  15   |  18   |    4     |
+-----------+-------+-------+----------+

Sorry for the big layout, I really don't know how to properly post table information. Now here's an attempt at explaining what I'm trying to do:

Every day, a row should be inserted in Entry and Hour for all employees who are WorkingFromHome. In the Entry table it should place the corresponding EmployeeNumber, and for JustifyDate it should add whatever day it is when the job is running. For the EntryDate field it should add that day's date, but the time part should be the InHour from the first corresponding CoreHour row.

For the Hour table it should add the EntryID that just got inserted in the Entry table, and the InHour should be the same as the EntryDate and for the OutHour field it should add a DateTime based on the last OutHour corresponding for the employee's CoreHourID.

I'm struggling a lot, so any help is appreciated.

PS: Any comments/questions regarding my explanation are welcome and I will gladly respond.

+2  A: 

The following could be encapsulated into a stored procedure that can be executed via a scheduled job. I'm not exactly sure what you meant by for JustifyDate it should add whatever day it is when the job is running.

Declare @NewEntries Table (
                            EntryId int not null
                            , EmployeeNumber int not null
                            , JustifyDate datetime
                            , EntryDate datetime
                            )

Insert [Entry]( EmployeeNumber, JustifyDate, EntryDate )
    Output Inserted.EntryId, Inserted.EmployeeNumber, Inserted.JustifyDate, Inserted.EntryDate 
        Into @NewEntries
Select EmployeeNumber
    , CURRENT_TIMESTAMP
    , DateAdd(hh, CoreHour.InHour, DateAdd(d, 0, CURRENT_TIMESTAMP))
From Employee
    Join CoreHour
        On CoreHour.CoreHourId = Employee.CoreHourId
Where WorkingFromHome = 1

Insert [Hour]( EntryId, InHour, OutHour )
Select NE.EntryId, DatePart(hh, NE.EntryDate), CoreHour.OutHour
From @NewEntries As NE
    Join Employee
        On Employee.EmployeeNumber = NE.EmployeeNumber
    Join CoreHour
        On CoreHour.CoreHourId = Employee.CoreHourId

(Revised to use Output clause).

Thomas
I meant in JustifyDate it should add the date in which the script/job is being executed, which I think is what you did with CURRENT_TIMESTAMP. I have a question, what did you do for EntryDate? Also for the OutHour, the field from the CoreHour table is an int (say, 17) so it should add like "09/02/2010 17:00:00" (taking the value as an hour and turning it into a complete datetime)
Eton B.
Also I think there's a mistake where only 3 values are getting inserted into Entry, since EntityId wasn't made auto increment?
Eton B.
Also getting error in Hour(NE.EntryDate) since Hour is not a function
Eton B.
@Eton B - RE: EntryDate. I am stripping the current date and time to just a date and then adding the hours. I guess Hour doesn't exist until 2008. I'll replace with DatePart.
Thomas
@Eton B - RE: Insert into Entry. In your spec, you stated that EntryId was an Identity (auto-incr) column. I want to let SQL Server create the identity values for me and thus I do not explicitly enter a value for EntityId. In the Output clause, I'm posting the result of the insert which will include the newly created identity values.
Thomas
@Eton B - Btw, "EntityId" was a typo. It should have been EntryId.
Thomas
Hey Thomas, thanks for the follow up! I figured it was a typo and corrected that, thanks. I'm getting a "Incorrect syntax near 'Inserted'." error now, though. Also, a small FYI: 'h' isn't recognized as a part in DateAdd, gotta use 'hh' (at least in 2005)
Eton B.
@Eton B - Take a look now. Typo on where I placed the Output clause. It should be right after the Insert clause and before the Select clause. In addition, I wrapped the table names Entry and Hour in square brackets.
Thomas
@Thomas - Really appreciating your help! Already modified your code to fully match my needs (added extra columns, etc) but I had forgotten one thing regarding CoreHour, which I added on the edit. For the InHour it should take the first CoreHourId row and for the OutHour it should take the last CoreHourId row. Also another thing, the time part on EntryDate is being added to the current time stamp instead of replacing it (e.g Justify Date = 7:00AM, EntryDate = 4:00PM which means that 9 hours from InHour got added to the current timestamp instead of being 9:00AM)
Eton B.
@Thomas - And thanks for your apparently infinite patience.
Eton B.
@Thomas - Nevermind, I was able to get the OutHour/InHour part right myself, I just need help with the Dates now. On the 2nd insert (Hour table) the InHour and OutHour fields are being filled with '1/18/1900 12:00:00 AM'
Eton B.
@Thomas - I finally solved it. Used " DateAdd(hh, CoreHours.OutHour, DATEADD(d, 0, DATEDIFF(d, 0, GETDATE())) ) " to remove the time and just add the CoreHour value. I know I couldn't have done any of this without your input. Thanks a lot! Accepted.
Eton B.