Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mattlightbourn
Starting Member
37 Posts |
Posted - 2009-11-11 : 05:36:49
|
Dear all, I’m stuck on a problem which I’m hoping someone might be able to help with in an SQL script.I have a table with RAW Timesheet data which includes entries for Absences. When someone enters a timesheet on a particular date for a two week holiday, if I report on the amount of hours recorded in the first week, it brings into account the next week’s absences as well.My solution is that, when the data is imported from the Timesheet system into the SQL database, I want it to identify Absence records greater than one day and then create multiple records for each day taken. This way, I will always get accurate reports regardless of the date range of a report.I am not great at scripting and am a little out of my depth but thought I would include where I have got up to (stuck though it be) to see if someone can see how to do this better or correct my mistakes.The Timesheet record comes from a Timesheet table which is imported from a TXT file. The TimeDate field is the date the timesheet is entered OR the date for the first day’s holiday. The AbsenceDays field is how many days.What I want to do is create a new record for every single day BUT, as it is creating each record, check the date against a Calendar table which checks to see if the proposed new record date is a WorkDay or not. The calendar has a [Date] field and a WorkDay Y/N field which is set to No if it is a weekend or is a Public Holiday.Each Record it creates INTO a new table called Absences (which I haven’t included – but know needs to go after the print DATEADD but don’t know what to add) and the TimeDate is modified to the date created from the @counter increment and each AbsenceDays is set to 1 with exception to any remainder on the last record (like in the example of 3.5 days – records for 1, 1, 1, 0.5 would be created).Sorry if this all looks convoluted but I wanted to ensure I explained myself fully before posting. Thank you all in advance.Kind RegardsMatt-- ****** Modified Version ******declare @StartDate datetimedeclare @Days intdeclare @counter intset @StartDate = dbo.Timesheets.TimeDateif dbo.timesheets.AbsenceDays = <1 THENendelseset @counter = 0-- The reason why counter is set to 0 and not 1 is because I want to create the initial record with the Timesheet date+0. This is because I do not want to keep the original record that is being used to duplicate and create each day timesheet.while(@counter <= dbo.timesheets.AbsenceDays)begin-- Somehow need to specify a relationship between dbo.Calendar.[Date] and dbo.timesheets.DateTime+@counterCASE WHEN(dbo.Calendar.WorkDay = “Y” THEN continue ELSE @counter = @counter+1 redo case END)-- Above case done to ensure that the date it is about to add is not a Sunday or Saturday or public holiday. Don’t know how to format it correctly.print DATEADD(day, @counter, @StartDate)-- I know I can insert record here but don’t know how. I want the same details as the original record being checked by the script but the TimeDate field to be changed to the new date and the AbsenceDays field to be set to 1 per records. In addition, if the absences are 3.5 days, I would need FOUR records created – 1, 1, 1, 0.5. When adding these records, how would I get it to create records which are not simply divided by 4? (namely 0.87 days per record)[/green]set @counter = @counter + 1end |
|
mattlightbourn
Starting Member
37 Posts |
Posted - 2009-11-11 : 12:05:57
|
A little more information for you. I've messed around with the code and included the query which gives me the set that I am using as source. If anyone has any ideas, I would be greatful. ThanksMatt-- This is the source Record Set. This is saved as a view: AbsenceFinderSELECT TimeDate, YearNum, MonthNum, ResourceName, Studio_Company, ChargeDesc, AbHrs, CASE WHEN ROUND((abhrs / (Hours_pw/Days_pw)),0) < 0 THEN 0 - ROUND((abhrs / (Hours_pw/Days_pw)),0) WHEN ROUND((abhrs / (Hours_pw/Days_pw)),0) > 1 THEN ROUND((abhrs / (Hours_pw/Days_pw)),0) ELSE 1 ENDAS NumRecordsFROM dbo.FM_TimesheetsWHERE ChargeCategory = 'Holidays/Absences'-- ************* This is the script so far ****************DECLARE @StartDate datetimeDECLARE @Days intDECLARE @counter intSET @StartDate = dbo.FM_Timesheets.TimeDate -- be careful of US dates (pass as parameters to SP) SET @counter = 0SET @Days = dbo.FM_Timesheets.NumRecordsWHILE (@counter <= @Days)BEGINPRINT DATEADD(day, @counter, @StartDate) – don’t know what this does?-- I’m guessing here, really don’t know how to create rows from scriptIF SELECT dbo.Calendar.workdays = ‘Y’ FROM dbo.Calendar LEFT OUTER JOIN DATEADD(day, @counter, @StartDate) = dbo.Calendar.caldate THENUPDATE AbsenceFinder.TimeDate = DATEADD(day, @counter, @StartDate)UPDATE AbsenceFinder.YearNum = YEAR(DATEADD(day, @counter, @StartDate))UPDATE AbsenceFinder.MonthNum = MONTH(DATEADD(day, @counter, @StartDate))UPDATE AbsenceFinder.ResourceName = ResourceNameUPDATE AbsenceFinder.StudioCompany = StudioCompanyUPDATE AbsenceFinder.ChargeDesc = ChargeDescUPDATE AbsenceFinder.AbHrs = (Hours_pw / Days_pw) I need to find a way of getting it to look at remaining amount and, if less than a day, enter the remaining amount. Example, someone puts 0.5 days, that is 3.75 hours. Full day 7.5. If the sum of this persons timesheets (have to put an ID on there as well to ensure they are groupable together) < original 4.5 days (33.75 hours) AND remaining amount is < (Hours_pw / Days_pw) then, value = remaining amount ELSE (Hours_pw / Days_pw).INTO AbsencesFROM AbsenceFinderSET @counter = @counter + 1ELSESET @counter = @counter + 1ENDENDExamplesOriginalTimeDate YearNum MonthNum ResourceName StudioCompany ChargeDesc AbHrs30/01/09 2009 01 Joe Bloggs Bristol Absence 33.75 (4.5 days)After work doneTimeDate YearNum MonthNum ResourceName StudioCompany ChargeDesc AbHrs30/01/09 2009 01 Joe Bloggs Bristol Absence 7.531/01/09 2009 01 Joe Bloggs Bristol Absence 7.502/02/09 2009 02 Joe Bloggs Bristol Absence 7.503/02/09 2009 02 Joe Bloggs Bristol Absence 7.504/02/09 2009 02 Joe Bloggs Bristol Absence 3.75 |
|
|
mattlightbourn
Starting Member
37 Posts |
Posted - 2009-11-12 : 04:48:19
|
I saw someone has written a post where they wanted to spilt a column into multiple records where a column value was delimited by a ' '.Is there a way that I could modify this for my use? Instead of delimited, I want to do it WHERE NumRecords > 1. I still need it to verify the date that it increments to from a join to the dbo.calendar.caldate looking to ensure dbo.calendar.workdays = 'Y' before proceeding.Thanks for your help with thisMatt(http://www.projectdmx.com/tsql/tblnumbers.aspx).SELECT customerid,firstname,lastname,SUBSTRING(phone_numbers, n, CHARINDEX(' ', phone_numbers + ' ',n) - n) AS phone,n + 1 - LEN(REPLACE(LEFT(phone_numbers, n), ' ', '' )) ASphone_idxFROM Phones AS PCROSS JOIN (SELECT numberFROM master..spt_valuesWHERE type = 'P'AND number BETWEEN 1 AND 100) AS Numbers(n)WHERE SUBSTRING(' ' + phone_numbers, n, 1) = ' 'AND n < LEN(phone_numbers) + 1ORDER BY customerid, phone_idx |
|
|
mattlightbourn
Starting Member
37 Posts |
Posted - 2009-11-12 : 06:47:17
|
Is this getting any closer? Really need some help on this. ThanksIF dbo.Absenceview.NumRecords > 1 THEN SET @AbsenceHours = dbo.Absenceview.Hours_pw / dbo.Absenseview.Days_pw) SET @RecCounter = dbo.Absenceview.NumRecords SET @AdjustDay = 0 SET @DateCheck = 'N' IF dbo.Absencerview.AbHrs / @ AbsenceHours = RecCounter SET @LastRec = @AbsenceHours ELSE SET @LastRec = (@RecCounter-(dbo.Absenceview.AbHrs / @AbsenceHours)) * @AbsenceHours END IF SET @Looper = 1 SET @AbDate = dbo.Absenceview.TimeDate WHILE @Looper <= (@RecCounter-1) INSERT INTO Absences ( TimeDate, MonthNum, RecourceName, Studio_Company, ChargeDesc, AbHrs ) VALUES ( @AbDate, MONTH(@AbDate), dbo.Absenceview.ResourceName, dbo.Absenceview.Studio_Company, dbo.Absenceview.ChargeDesc, @AbsenceHours );-- Check the next date to see if it is a valid WorkDay by looking up proposed date in dbo.Calendar WHILE @DateCheck = 'N' SET @adDate = DATEADD(day, (@Looper+@AdjustDay), @adDate) SET @DateCheck = (SELECT dbo.Calendar.WorkDay FROM dbo.Calendar WHERE dbo.Calendar.CalDate = @adDate) SET @AdjustDay = @AdjustDay + 1 END WHILE-- ******************************************************************************** END WHILE INSERT INTO Absences ( TimeDate, MonthNum, RecourceName, Studio_Company, ChargeDesc, AbHrs ) VALUES ( @AbDate, MONTH(@AbDate), dbo.Absenceview.ResourceName, dbo.Absenceview.Studio_Company, dbo.Absenceview.ChargeDesc, @LastRec ); ELSE INSERT INTO Absences ( TimeDate, MonthNum, RecourceName, Studio_Company, ChargeDesc, AbHrs ) VALUES ( dbo.Absenceview.TimeDate, dbo.Absenceview.MonthNum, dbo.Absenceview.ResourceName, dbo.Absenceview.Studio_Company, dbo.Absenceview.ChargeDesc, dbo.Absenceview.AbHrs );END IF; |
|
|
|
|
|
|
|