June 28, 2015

esProc Exports Data by Groups


A selection from the original table (TB1)

MONTH  ID   COMPANY    DEPARTMENT     JOINTIME     PROGROUP
201305    1009       1     A     A1   201108    DG
201305    1009       1     B     B1   201207    PG
201305    1009       1     C     C1   201301    SG
201305    1009       1     D     D1   201109    SEG
201305    1013       2     C     C2   201302    SG
201305    1027       2     A     A3   201007    DG

Note: The table has a great amount of data volume with 4 million rows of data.
You need to export data into several TXTfiles according to different values in PROGROUP column. Based on the above data, the expected result is as follows:

DG.txt
MONTH  ID   COMPANY    DEPARTMENT     JOINTIME     PROGROUP
201305    1009       1     A     A1   201108    DG
201305    1027       2     A     A3   201007    DG

PG.txt
MONTH  ID   COMPANY    DEPARTMENT     JOINTIME     PROGROUP
201305    1009       1     B     B1   201207    PG

SEG.txt
MONTH  ID   COMPANY    DEPARTMENT     JOINTIME     PROGROUP
201305    1009       1     D     D1   201109    SEG

SG.txt
MONTH  ID   COMPANY    DEPARTMENT     JOINTIME     PROGROUP
201305    1009       1     C     C1   201301    SG
201305    1013       2     C     C2   201302    SG

You can not store the grouped set in SQL (aggregation is required after grouping), which makes the coding really complicated. Besides, it is annoying to export the big data involved in the problem to TXT files in batches using cursors. esProc can handle the operation elegantly with its group cursor through a three-line script:

A1: Use the SQL statement to create and return a database cursor sorted by PROGROUP.

A2-B2: Group data by PROGROUP. Run a loop to retrieve a group of records each time and then export them in B2 to the corresponding TXT file named after a type of value in PROGROUP field.