1 / 19

Sort and Merge

Sort and Merge. SORTING. During the processing of sequential files, it is required that the records in a file appear in some predetermined sequence. The process of sequencing the records in some desired manner is known as sorting . Sorting is done upon some key data item in the record.

denzel
Download Presentation

Sort and Merge

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SortandMerge

  2. SORTING • During the processing of sequential files, it is required that the records in a file appear in some predetermined sequence. • The process of sequencing the records in some desired manner is known as sorting. • Sorting is done upon some key data item in the record. • For eg, payroll file where each record contains all the necessary information of an employee, such as name, id , deptno, basicpay etc. • In this we can say name as the key for sorting and can get a new file in the sorted order from the original file.

  3. The sorting is done either in the ascending order or descending order of the key. • ASCENDING: From lowest to highest • DESCENDING: From highest to lowest • It is possible to sort a file on more than one key. THE SIMPLE SORT VERB The SORT verb has got different forms.let us discuss the simple SORT verb. • This form is to be used when it is required to sort a given input file. • The input file is not disturbed, instead, a new output file containing the records in a sorted order is created.

  4. Besides the input and output files, the SORT verb also requires a work file. • The work file is used during the process of sorting. • Thus the simple SORT verb requires the naming of three files- the unsorted input file, the sorted output file and the work file. • The format of the simple SORT verb is,

  5. The WorkFileName identifies a temporary work file that the SORT process uses for the sort. It is defined in the FILE SECTION using an SD entry. • InFileName and OutFileName, are the names of the input and output files. These files are automatically opened by the SORT. When the SORT executes they must not be already open. • These two files should be described in the usual manner by means of an FD entry. • The format of the SD entry is, SD file-name [; RECORD CONTAINS [integer-1 TO] integer-2 CHARACTERS] [; DATA { RECORD IS } data-name-1 [ , data-name-2]……] The clauses have the same meanings as in the case of FD entry.

  6. RULES • The input, output as well as the work file are opened by the SORT statement before the sorting begins and are closed by the SORT statement itself after the sorting is over. • There can be any number of SORT statements in a program. • The sorting can be done on any number of keys. • Sortkeyidentifier is the major key. When more than one SortKeyIdentifier is specified, the keys decrease in significance from left to right (leftmost key is most significant, rightmost is least significant). • For a particular key, either the word ASCENDING or the word DESCENDING is used depending on the requirement.

  7. All the keys on which the sorting is done, must appear with their descriptions in the record descriptions of workfile. • Keys in the SORT statement do not require any qualification even when they are identical to data names defined elsewhere. • When two or more records in the input file have identical keys, their relative order within the input file may not be retained in the output file. • The infilename and outfilename must be sequential files. • The select clause for the workfile is SELECT file-name ASSIGN TO hardware-name.

  8. Sort Example. FD SalesFile.01 SalesRec. 02 FILLER PIC X(10). SD WorkFile.01 WorkRec. 02 WSalesmanNum PIC 9(5). 02 FILLER PIC X(5). FD SortedSalesFile.01 SortedSalesRec. 02 SalesmanNum PIC 9(5). 02 ItemType PIC X. 02 QtySold PIC 9(4). PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile GIVING SortedSalesFile. OPEN INPUT SortedSalesFile.

  9. ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT WorkFile ASSIGN TO "WORK.TMP". SD WorkFile.01 WorkRecord. 02 ProvinceCode PIC 9. 02 SalesmanCode PIC 9(5). 02 FILLER PIC X(19). PROCEDURE DIVISION.Begin. SORT WorkFile ON ASCENDING KEY ProvinceCode DESCENDING KEY SalesmanCode USING UnsortedSales GIVING SortedSales. OPEN INPUT SortedSales.

  10. How the SORT works. SORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile GIVING SortedSalesFile. SalesFile SortedSalesFile Unsorted Records Sorted Records SORT Process WorkFile

  11. MERGE Statement • To combine two or more files into one • Files to be merged must each be in sequence by key field • Format similar to SORT, rules for clauses are same • The Merge takes two or more identically sequenced files and combines them, according to the key values specified, to produce a combined file which is then output to an output file or OUTPUT PROCEDURE.

  12. MERGE Syntax. MERGE file-name-1 ON ASCENDING KEY data-name-1 … … DESCENDING USING file-name-2 , file-name-3 … GIVING file-name-4 .

  13. SYNTAX • File-name-1 is work file designated as an SD • Keys specified are defined within SD • Data-name-1 is major key, may be followed by intermediate and minor keys • USING clause names file to be merged • At least two must be included • Automatically handles opening, closing, and input/output associated with files

  14. MERGE Statement Example • Suppose two separate files of employees are to be combined into one • Both input files and the resulting output file contain 80 characters with an Emp-No in the first nine positions • File definitions and MERGE instruction follow

  15. MERGE Statement Example Data Division. File Section. FD Emp-File-1. 01 Emp-Rec-1 Pic X(80). FD Emp-File-2. 01 Emp-Rec-2 Pic X(80).

  16. MERGE Statement Example SD Merge-File. • 01 Merge-Rec. • 05 Mrg-Emp-No Pic X(9). • 05 Rest-of-Rec Pic X(71). FD Out-Emp-File. 01 Out-Emp-Rec Pic X(80).

  17. MERGE Statement Example Procedure Division. Main-Module. Merge Merge-File On Ascending Key Mrg-Emp-No Using Emp-File-1, Emp-File-2 Giving Out-Emp-File Stop Run.

  18. MERGE Example • e.g.MERGE WorkFile ON ASCENDING KEY StudentId USING InsertionsFile, StudentFile GIVING NewStudentFile.

  19. ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO "STUDENTS.DAT" ORGANIZATION IS LINE SEQUENTIAL. SELECT InsertionsFile ASSIGN TO "TRANSINS.DAT" ORGANIZATION IS LINE SEQUENTIAL. SELECT NewStudentFile ASSIGN TO "STUDENTS.NEW" ORGANIZATION IS LINE SEQUENTIAL. SELECT WorkFile ASSIGN TO "WORK.TMP". DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentRec PIC X(32). FD InsertionsFile.01 InsertionRec PIC X(32). FD NewStudentFile.01 NewStudentRec PIC X(32). SD WorkFile.01 WorkRec. 02 WStudentId PIC 9(7). 02 FILLER PIC X(25). PROCEDURE DIVISION.Begin. MERGE WorkFile ON ASCENDING KEY WStudentId USING InsertionsFile, StudentFile GIVING NewStudentFile. STOP RUN.

More Related