esProc Exports Data by Groups

Problem source:http://bbs.csdn.net/topics/390500337.

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:

2015-06-29_115505

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.

Advertisements

About datathinker

a technical consultant on Database performance optimization, Database storage expansion, Off-database computation. personal blog at: datakeywrod, website: raqsoft
This entry was posted in SQL-related Puzzle and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s