View previous topic :: View next topic |
Author |
Message |
yadav2005 Intermediate

Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Thu May 10, 2007 4:33 am Post subject: internal table logic needed |
|
|
I am using a CURSOR in my program and i am fetching records one by one and the way they are retreived is :
Code: |
2007-01-01 ABC 100
2007-01-01 ABC -50
2007-01-01 ABC 200
2007-01-01 DEF -10
2007-01-01 DEF -20
2007-01-02 ABC 3
2007-01-02 ABC 6
2007-01-02 ABC 9
2007-01-02 ABC 12
2007-01-02 ABC 1
2007-01-02 ABC -1
2007-01-02 ABC -12
2007-01-02 ABC 20
2007-01-02 ABC 30
2007-01-02 ABC -9
2007-01-02 PQR 100
2007-05-01 XYZ 1
2007-05-01 XYZ 2
2007-05-01 XYZ 3
2007-05-01 XYZ -6
|
I want to generate a report in this fashion:
Code: |
DATE DEPT COUNT PLUS MINUS DIFFERENCE
2007-01-01 ABC 3 300 -50 250
2007-01-01 DEF 2 0 -30 -30
2007-01-02 ABC 10 81 -21 60
2007-01-02 PQR 1 100 0 100
2007-05-01 XYZ 4 6 -6 0
------- ---- ------ ------------
TOTAL 20 486 -105 381
|
The date is sorted and then the dept while fetching as per the cursor ,Count is the number of records which has the same category ,plus indicates positive amount, minus indicates negative amount and difference is plus + minus.I want to make use of internal table (array) COBOL,please let me know how can i achieve it ? |
|
Back to top |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Thu May 10, 2007 5:09 am Post subject: |
|
|
sort your data in the required sequence, load it to the table and walk through the table accumulating your values as required and writing oout as required. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu May 10, 2007 5:38 am Post subject: |
|
|
I have been sort of a butt head last couple of days, so I will be nice.
yadav2005,
this unfortunately is a poor example of such a requirement. The data is already sorted:
Quote: | The date is sorted and then the dept while fetching as per the cursor |
I assume you have an ORDER BY clause.
you only need 2 structures; DATE-DEPT and TOTALS.
you have an unknown number of DATE-DEPTs, which means you might fill your table. That means complex logic to handle dumping the table
outputing - and trying to keep track of what you printed last, where, are we at top of page etc....... -
initializing and starting again. really unnecessary since the input is sorted in report sequence.
and if you tell me ok, I just want the logic in case it was not sorted and I would tell you - Sort it!
In fact, DFSORT has a 'Report Writer' capability, you could code the ICETool Parms quicker than you could a program. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12382 Topics: 75 Location: San Jose
|
Posted: Thu May 10, 2007 5:43 am Post subject: |
|
|
yadav2005,
1. You don't need an Internal table.
Code: |
01 ws-prev-key.
05 ws-prev-date pic x(10).
05 ws-prev-dept pic x(03).
01 ws-plus-qty pic s9(09) comp-3.
01 ws-neg-qty pic s9(09) comp-3.
01 ws-difference pic s9(09) comp-3.
|
check for the key change and sum up of the values.
2. you can do all this in the sql itself
Code: |
SELECT DATE_COL
,DEPT
,COUNT(*)
,SUM(CASE WHEN QTY < 0 THEN INT(0) ELSE QTY END)
,SUM(CASE WHEN QTY > 0 THEN INT(0) ELSE QTY END)
,SUM(QTY)
FROM TABLE
GROUP BY DATE_COL
,DEPT
|
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu May 10, 2007 5:52 am Post subject: |
|
|
Kolusu,
my, but you are quick. excellent solution. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
yadav2005 Intermediate

Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Thu May 10, 2007 6:08 am Post subject: |
|
|
Kolusu,
Great Answer and it is so simple .But i want to implement the logic by using internal table , can u please help me out as the requirement is to implement by using Internal table.Thanks for your help. |
|
Back to top |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Thu May 10, 2007 6:11 am Post subject: |
|
|
change the requirement if it is less efficient. Why MUST you do it that way - because whoever wrote your spec didn't know any better? Just tell him there is a better way. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
 |
|
|