Script Files

9
Script Files Script Files contain SQL statements to manipulate data and also include parameter statements to control the scope of the data returned by an SQL statement. Normally an SQL statement includes the physical value of that sets the range of data the statement will operate on but when a script includes many statements operating on the same range then the value of the range is set outside the of a statement and the statement includes a reference to the parameter which is set outside of the parameter and substituted in the statement at run time. Using as an example lines from the file “All_Projects.sqv” parameters are set immediately after the statements which drop the temporary tables the script file will work with. // Temporary tables Drop Table Projects; Drop Table Bateman; Drop table task_codes; Drop table Week_End; Drop Table Bateman_Sum; Drop Table Tasks_1065; Drop Table Project_Tasks; Drop Table Departments; // Set Month start date; PARAMETER_SET prmStr_1 = #01/26/2013#; // Set Month end date;

Transcript of Script Files

Script Files

Script Files contain SQL statements to manipulate data and also include parameter statements to control the scope of the data returned by an SQL statement. Normally an SQL statement includes the physical value of that sets the range of data the statement will operate on but when a script includes many statements operating on the same range then the value of the range is set outside the of a statement and the statement includes a reference to the parameter which is set outside of the parameter and substituted in the statement at run time.

Using as an example lines from the file “All_Projects.sqv” parameters are set immediately after the statements which drop the temporary tables the script file will work with.

// Temporary tables

Drop Table Projects;Drop Table Bateman;Drop table task_codes;Drop table Week_End;Drop Table Bateman_Sum;Drop Table Tasks_1065;Drop Table Project_Tasks;Drop Table Departments;

// Set Month start date; PARAMETER_SET prmStr_1 = #01/26/2013#; // Set Month end date; PARAMETER_SET prmStr_2 = #02/22/2013#;

// Set start of current year parameter;

PARAMETER_SET prmStr_3 = #12/29/2012#;

// Set start of previous year parameter;

PARAMETER_SET prmStr_4 = #06/18/2011#;

// Set end of previous year parameter;

PARAMETER_SET prmStr_5 = #12/28/2012#;

One of the advantages of using parameters like this is that they can be used multiple times in the script file as data is accessed or manipulated to create a report.

*** This sets the range of the timesheet data the script file will work with.

Select * INTO Projects from lnkBEPL_Projects where lnkBEPL_Projects.UOM = "HOURS" AND lnkBEPL_Projects.ITEM_DATE BETWEEN prmStr_1 AND prmStr_2;

*** This calculates the distance of a date from month start date and then determines which of the 5 weeks in the report range the data will fall into

Alter Table Projects ADD COLUMN Week_NUM TEXT (1);Alter Table Projects ADD COLUMN Date_Diff Integer;Alter Table Projects ADD COLUMN Week_End Date;

Update Projects Set Projects.Date_Diff = ((Projects.ITEM_DATE - prmStr_1) mod 35);

Update Projects Set Projects.Week_End = Projects.ITEM_DATE + (6 - (Projects.Date_Diff Mod 7));

Update Projects SET Projects.Week_NUM = "5" where Projects.Date_Diff BETWEEN 28 AND 34;Update Projects SET Projects.Week_NUM = "4" where Projects.Date_Diff BETWEEN 21 AND 27;Update Projects SET Projects.Week_NUM = "3" where Projects.Date_Diff BETWEEN 14 AND 20;Update Projects SET Projects.Week_NUM = "2" where Projects.Date_Diff BETWEEN 7 AND 13;Update Projects SET Projects.Week_NUM = "1" where Projects.Date_Diff BETWEEN 0 AND 6;

For this query the accounting period runs from the first Saturday in a month to the last Friday in a week which may be in the next physical month and results in either 4 or 5 week months.

As data is aggregated further information such as employee details and task description are added but this isn’t shown in this introduction but if required is seen in the file “All_Projects.sqv”.

Select * into Bateman from Projects;

**** Example of SQL statements that manipulate the data into the form required to output into a worksheet where the day of the week is matched with a date***

Alter Table Bateman ADD Column WEEK_END_DATE Date;Alter Table Bateman ADD Column SATHRS Float;Alter Table Bateman ADD Column SUNHRS Float;Alter Table Bateman ADD Column MONHRS Float;Alter Table Bateman ADD Column TUEHRS Float;Alter Table Bateman ADD Column WEDHRS Float;Alter Table Bateman ADD Column THUHRS Float;Alter Table Bateman ADD Column FRIHRS Float;

Alter Table Bateman ADD Column STTOT Float;

***** initialise values

Update Bateman SET Bateman.SATHRS = 0.00;Update Bateman SET Bateman.SUNHRS = 0.00;Update Bateman SET Bateman.MONHRS = 0.00;Update Bateman SET Bateman.TUEHRS = 0.00;Update Bateman SET Bateman.WEDHRS = 0.00;Update Bateman SET Bateman.THUHRS = 0.00;Update Bateman SET Bateman.FRIHRS = 0.00;

***** assign timesheet hours based on the day of the week identified by the value of the modulo

Update Bateman SET Bateman.FRIHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 0;

Update Bateman SET Bateman.THUHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 1;

Update Bateman SET Bateman.WEDHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 2;

Update Bateman SET Bateman.TUEHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 3;

Update Bateman SET Bateman.MONHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 4;

Update Bateman SET Bateman.SUNHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 5;

Update Bateman SET Bateman.SATHRS = Bateman.QUANTITY WHERE (Bateman.Week_End - Bateman.ITEM_DATE) mod 7 = 6;

*****Calculate total hours worked in the week by an employee which is a line in a single record

Update Bateman SET Bateman.STTOT = Bateman.SATHRS + Bateman.SUNHRS + Bateman.MONHRS + Bateman.TUEHRS + Bateman.WEDHRS + Bateman.THUHRS + Bateman.FRIHRS;

*****The values are then transferred to the table Bateman_Sum which is used as the source for data written to Excel

Select Bateman.PROJECT, Bateman.TASK, Bateman.Task_Desc, Bateman.EXPEND_TYPE, Sum( Bateman.SATHRS )AS SATHRS , SUM( Bateman.SUNHRS) AS SUNHRS, SUM ( Bateman.MONHRS ) AS MONHRS , SUM ( Bateman.TUEHRS ) AS TUEHRS , SUM ( Bateman.WEDHRS ) AS WEDHRS , SUM ( Bateman.THUHRS ) AS THUHRS , SUM ( Bateman.FRIHRS ) AS FRIHRS , SUM ( Bateman.STTOT ) AS STTOT ,Bateman.EMPLOYEE, Bateman.CLASSDESCRIP , Bateman.Location ,Bateman.Week_NUM , Bateman.Week_End INTO Bateman_Sum from Bateman GROUP BY Bateman.PROJECT , Bateman.TASK ,Bateman.Task_Desc, Bateman.Dept , Bateman.EMPLOYEE, Bateman.EXPEND_TYPE, Bateman.CLASSDESCRIP, Bateman.Location, Bateman.Week_NUM,Bateman.Week_End ;

*****Transferring the data to Excel and identifying the spreadsheet to use

DDE Excel C:\SQL_Test\Spreadsheets\Projects_All.xls;

*****Identify worksheet to use

DDE Excel_Sheet Week1;

*****start writing to the worksheet

DDE Add_Header 1, Project , Employee Name , Type , Classification , Task , Task Description , Saturday , Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Total Hours , ;

// Get Current Week Ending for Report;

Select TOP 1 “Project Man-hours Week Ending -” & format ( Bateman_Sum.Week_End, “dd-mmm-yyyy") & “- for review” from Bateman_Sum Where Bateman_Sum.Week_NUM = "1”;

DDE Insert_Row 1; //add a blank line to worksheet

DDE Sum_On_Change 1, 7, 14, Total for Project,;

// output employee hours per project to worksheet

Select Bateman_Sum.PROJECT, Bateman_Sum.EMPLOYEE, Bateman_Sum.EXPEND_TYPE, Bateman_Sum.CLASSDESCRIP, Bateman_Sum.TASK, Bateman_Sum.Task_Desc, Bateman_Sum.SATHRS, Bateman_Sum.SUNHRS, Bateman_Sum.MONHRS, Bateman_Sum.TUEHRS, Bateman_Sum.WEDHRS, Bateman_Sum.THUHRS, Bateman_Sum.FRIHRS, Bateman_Sum.STTOT from Bateman_Sum Where Bateman_Sum.Week_NUM = "1" Order by Bateman_Sum.PROJECT, Bateman_Sum.EMPLOYEE, Bateman_Sum.TASK;

Using this workbook from C:\SQl_Test\Spreadsheets as an example an extract for Week 1 is shown below

Project Employee Name Classification Task Task Description Saturday Sunday Monday Tuesday Wednesday Thursday Friday Total Hours

Project Manhours Week Ending - 01-Jun-2012 - for review

C0001 McDonald, Doctor Douglas Colin DC A Process Engineer Principal A0010 8.00 8.00 8.00 8.00 8.00 40.00

Total for Project 8.00 8.00 8.00 8.00 8.00 40.00

M6031 Frearson, Mr. Russell RF AUS81082 R Cost Engineer A1015 1.00 1.00

Total for Project 1.00 1.00

M6037 Frearson, Mr. Russell RF AUS81082 R Cost Engineer P1000 1.00 1.00M6037 Loveday, Mr. Grant Kelsey GK AUS238 Process Engineer Senior P1000 5.00 6.50 4.50 5.00 21.00

Total for Project 5.00 7.50 4.50 5.00 22.00

What the above shows that by running multiple SQL statements it is possible to take raw data from a database add values and provide a meaningful report and by using parameters it is possible to provide a generic report that will output what the parameters determine.