Skip to main content

Adding custom rows on summarization

Hi,
I am summarizing a file based on a column 'Category'.
Categories will be A, B, C, D, etc
So summary file will have records like given below.
Category    No of records
A                     10
B                     20
C                     15
My requirement is this:
If there are no records corresponding to Category ='B', it should show record count as zero for category B, as given below. 
Category    No of records
A                     10
B                     0
C                     15
Could you please suggest any workarounds to achieve this requirement?
Thanks in advance,
Shafeer
 
 
 

Brian Element Mon, 10/24/2016 - 08:44

In reply to by mohamed

Sorry, I hadn't realized it was for a SmartAnalzyer app, now I understand.

So there is only 8 items. Then I would suggest that your script had a section that creates a new database and adds the categories.  Below is some sample code to do that.  You can copy it and modify it.  Then you can do the join to have a file with a 0 for records if there is no items.

Let me know how it goes.

Brian

Option Explicit

Sub Main

	Call createDatabase
	Call addData
	Client.OpenDatabase "SampleData.IMD"

End Sub 

Function createDatabase
	Dim NewTable As Table
	Dim db As database
	Dim AddedField As field
	Set NewTable = Client.NewTableDef
	
	' Define a field for the table.
	Set AddedField = NewTable.NewField
		AddedField.Name = "Category"
		AddedField.Type = WI_CHAR_FIELD
		AddedField.Length = 20
		' Add the field to the table.
		NewTable.AppendField AddedField
	Set AddedField = Nothing
	
	' Create the database.
	Set db = Client.NewDatabase("SampleData.IMD", "", NewTable)
	Set NewTable = Nothing
	Set db = Nothing

End Function

Function addData
	Dim db As Database
	Dim Table As Table
	Dim rs As RecordSet
	Dim rec As Record
	Dim i As Integer
	' Create a table.
	
		
	' Obtain the recordset.
	Set db = client.OpenDatabase("SampleData.IMD")
		Set table = db.TableDef
			table.Protect = False
			Set rs = db.RecordSet
			' Obtain a new record.
			For i = 1 To 8
				Set rec = rs.NewRecord
				' Use the field name method to add data.
				rec.SetCharValue "Category", "Category" & i
				rs.AppendRecord rec
			Next i
			
			' Protect the table before you commit it.
			
			table.Protect = True
			
			' Commit the database.
			
			db.CommitDatabase
			
			' Open the database.
			
			Client.OpenDatabase "SampleData.IMD"
			
			' Clear memory
		Set table = Nothing
		db.close
	Set db = Nothing
	Set rs = Nothing
	Set rec =  Nothing
End Function

 

mohamed Tue, 10/25/2016 - 03:36

Hi Brian,
This is what I was looking for.
Thanks a lot
Shafeer