Problem source:http://bbs.csdn.net/topics/390500337 .
A selection from the original table (TB1):
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:
A | B | |
1 | =db.cursor(“SELECT * FROM TB1 ORDER BY PROGROUP ASC”) | |
2 | for A1;PROGROUP | >file(“E:\\”+A2.PROGROUP+”.txt”).export@t(A2) |
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.