# Grouping and Count Records

**URL:** <https://techcommunity.softwareag.com/t/grouping-and-count-records/101617>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [October 18, 2012, 9:24pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617 "2012-10-18T21:24:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JB\_Newbie](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jb_newbie/32/537_2.png) [@JB\_Newbie](https://techcommunity.softwareag.com/u/JB_Newbie)\
**Post date:** [October 18, 2012, 9:24pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/1 "2012-10-18T21:24:12Z")

</div>

I know there has to be an easier way to do this… I have a file that has say 200 records. Part of each record contains a form #. All I I want to group and summarize how many times each form occurs in those 200 records.

So for 200 records you would get something like this:

Form # # of Occurrences  
12344 100  
26351 10  
85963 10  
63693 2  
98745 78  
74582 100  
Total number of records = 200

Thank You!!!

---

<div class="post-metadata">

**Author:** ![Nikolay\_Kuhta](https://avatars.discourse-cdn.com/v4/letter/n/35a633/32.png) [@Nikolay\_Kuhta](https://techcommunity.softwareag.com/u/Nikolay_Kuhta)\
**Post date:** [October 18, 2012, 10:40pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/2 "2012-10-18T22:40:17Z")

</div>

Well, may be there is one of course, but may I ask you: why don’t you like doing this task in your OWN way? If it works properly, why do you need another solution (especially for such a small number of records)?  
Any way, I would NOT look for that “easier way”🙂

Best regards.  
NK

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [October 18, 2012, 10:53pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/3 "2012-10-18T22:53:25Z")

</div>

If your FORM number is a descriptor on the file, you can get what you want with a simple histogram:

```auto

DEFINE DATA LOCAL
1 FILE-HIST VIEW OF FILE
  2 FORM-NBR
1 #TOTAL (I4)
END-DEFINE
*
HIST.
HISTOGRAM FILE-HIST FORM-NBR
  DISPLAY FORM-NBR *NUMBER(HIST.)
  ADD *NUMBER(HIST.) TO #TOTAL
END-HISTOGRAM
WRITE '=' #TOTAL
END
```

If FORM-NBR is not a descriptor, or you need to get more info from each file record, you could read the file, sort by form number and us at break logic.

```auto

DEFINE DATA LOCAL
1 FILE VIEW OF FILE
  2 FORM-NBR
END-DEFINE
*
RD.
READ FILE FORM-NBR
END-ALL
SORT BY FORM-NBR
   USING KEYS
  AT BREAK OF FORM-NBR
    DISPLAY OLD(FORM-NBR) *COUNT(FORM-NBR)
  END-BREAK
END-HISTOGRAM
WRITE '=' *COUNTER(RD.)
END
```

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [October 18, 2012, 11:32pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/4 "2012-10-18T23:32:38Z")

</div>

First, if FORM-NUMBER is a descriptor, use Jerome’s HISTOGRAM code.

If not, rather than using SORT, it might (I have not done a timing comparison yet) be faster to do something like:

DEFINE DATA LOCAL  
1 FILE-HIST VIEW OF FILE  
2 FORM-NBR  
1 #COUNTS (I2/0:99999)  
1 #LOOP (I2)  
END-DEFINE  
\*  
READ FILE-HIST  
ADD 1 TO #COUNTS (FORM-NBR)  
END-READ  
\*  
FOR #LOOP = 0 to 99999  
IF #COUNTS (#LOOP) NE 0  
DISPLAY #LOOP #COUNTS (#LOOP)  
END-IF  
END-FOR  
END

---

<div class="post-metadata">

**Author:** ![JB\_Newbie](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jb_newbie/32/537_2.png) [@JB\_Newbie](https://techcommunity.softwareag.com/u/JB_Newbie)\
**Post date:** [October 19, 2012, 3:03am UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/5 "2012-10-19T03:03:56Z")

</div>

How do I change the rating I gave you?? uggg… Thank you, this is exactly … great example. 2nd is what I will need. Am I able to add a where statement in with this too? I tried and getting errors.

> [@Jerome LeBlanc](#):
>
> If your FORM number is a descriptor on the file, you can get what you want with a simple histogram:
> 
> ```auto
> 
> DEFINE DATA LOCAL
> 1 FILE-HIST VIEW OF FILE
> 2 FORM-NBR
> 1 #TOTAL (I4)
> END-DEFINE
> *
> HIST.
> HISTOGRAM FILE-HIST FORM-NBR
> DISPLAY FORM-NBR *NUMBER(HIST.)
> ADD *NUMBER(HIST.) TO #TOTAL
> END-HISTOGRAM
> WRITE '=' #TOTAL
> END
> ```
> 
> If FORM-NBR is not a descriptor, or you need to get more info from each file record, you could read the file, sort by form number and us at break logic.
> 
> ```auto
> 
> DEFINE DATA LOCAL
> 1 FILE VIEW OF FILE
> 2 FORM-NBR
> END-DEFINE
> *
> RD.
> READ FILE FORM-NBR
> END-ALL
> SORT BY FORM-NBR
> USING KEYS
> AT BREAK OF FORM-NBR
> DISPLAY OLD(FORM-NBR) *COUNT(FORM-NBR)
> END-BREAK
> END-HISTOGRAM
> WRITE '=' *COUNTER(RD.)
> END
> ```

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [October 19, 2012, 12:30pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/6 "2012-10-19T12:30:44Z")

</div>

Could you show us the WHERE clause?

With a HISTOGRAM statement, you must reference a view that has only one field, namely the field to be HISTOGRAM’ed. Remember, HISTOGRAM does not read records, it only accesses the inverted tables. Thus you do not have access to other fields.

You can have a WHERE clause, but it would be something like:

HISTOGRAM MYVIEW FIELD1  
WHERE FIELD1 NE ‘C’ THRU ‘G’

The WHERE clause as mentioned above could not reference a FIELD2.

If you are using the READ loop, there should be no problem with the WHERE clause. Very straightforward; for example:

READ MYVIEW WHERE FIELD2 NE ‘C’ THRU ‘G’

steve

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [October 22, 2012, 10:02pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/7 "2012-10-22T22:02:44Z")

</div>

Are you reading an Adabas file or a flat file?

In either case, I prefer Jerome’s SORT/BREAK method, but here’s another “array” solution.

```auto
DEFINE DATA LOCAL
1 #M (I4) CONST <20>
1 #TBL (0:#M)
  2 #FORM (A5)
  2 #COUNT (I4)
1 #WRK
  2 #FORM (A5)
1 #E (I4)
1 #I (I4)
1 #J (I4)
END-DEFINE
DEFINE WORK FILE 1 'C:\Users\ZbrogR\Documents\NatBatch\grpcount.txt' TYPE 'ASCII'
FORMAT SG=F
READ WORK 1 #WRK
  EXAMINE #TBL.#FORM (0:#E) FOR #WRK.#FORM GIVING INDEX #I
  IF #I = 0
    THEN
      ADD 1 TO #E
      ASSIGN #I = #E
      ASSIGN #TBL.#FORM (#I) = #WRK.#FORM
  END-IF
  ADD 1 TO #TBL.#COUNT (#I)
  ADD 1 TO #J
END-WORK
DISPLAY #TBL.#FORM (1:#E)
        #TBL.#COUNT (1:#E)
WRITE / 'Total records:' #J (AD=L)
END
```

For performance, all indices should be defined as I4.

---

<div class="post-metadata">

**Author:** ![JB\_Newbie](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jb_newbie/32/537_2.png) [@JB\_Newbie](https://techcommunity.softwareag.com/u/JB_Newbie)\
**Post date:** [October 23, 2012, 4:05pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/8 "2012-10-23T16:05:06Z")

</div>

Thanks!!

Adabas, I got pulled away from this project and getting back on it today. I will let you know how I make out. Thank you for our help, this site is great!

===========================================

> [@Ralph Zbrog](#):
>
> Are you reading an Adabas file or a flat file?
> 
> In either case, I prefer Jerome’s SORT/BREAK method, but here’s another “array” solution.
> 
> ```auto
> DEFINE DATA LOCAL
> 1 #M (I4) CONST <20>
> 1 #TBL (0:#M)
> 2 #FORM (A5)
> 2 #COUNT (I4)
> 1 #WRK
> 2 #FORM (A5)
> 1 #E (I4)
> 1 #I (I4)
> 1 #J (I4)
> END-DEFINE
> DEFINE WORK FILE 1 'C:\Users\ZbrogR\Documents\NatBatch\grpcount.txt' TYPE 'ASCII'
> FORMAT SG=F
> READ WORK 1 #WRK
> EXAMINE #TBL.#FORM (0:#E) FOR #WRK.#FORM GIVING INDEX #I
> IF #I = 0
> THEN
> ADD 1 TO #E
> ASSIGN #I = #E
> ASSIGN #TBL.#FORM (#I) = #WRK.#FORM
> END-IF
> ADD 1 TO #TBL.#COUNT (#I)
> ADD 1 TO #J
> END-WORK
> DISPLAY #TBL.#FORM (1:#E)
> #TBL.#COUNT (1:#E)
> WRITE / 'Total records:' #J (AD=L)
> END
> ```
> 
> For performance, all indices should be defined as I4.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:02pm UTC](https://techcommunity.softwareag.com/t/grouping-and-count-records/101617/9 "2021-04-02T15:02:31Z")

</div>


