19
Advanced Reporting Rev 5.01 Page 1 of 19 Advanced Report Writing Manual Contents Target Audience ................................................................................................................................. 2 Overview ............................................................................................................................................. 2 Top x Rows......................................................................................................................................... 2 Standard Computed Fields ............................................................................................................... 2 Overview ........................................................................................................................................ 2 Report Designer Computed Field............................................................................................... 3 Overview .................................................................................................................................... 3 Calculated Values ...................................................................................................................... 3 Combining Text and Fields ..................................................................................................... 4 SQL Select Computed Column................................................................................................... 6 Overview .................................................................................................................................... 6 How to Access the Functions List ......................................................................................... 7 Examples of Use ....................................................................................................................... 7 Sub-Queries – Nested Select ............................................................................................................ 9 Overview ........................................................................................................................................ 9 Method............................................................................................................................................ 9 Common Mistakes ...................................................................................................................... 10 Worked Example......................................................................................................................... 10 Nested Reports................................................................................................................................. 11 Overview ...................................................................................................................................... 11 Creating Nested Reports ............................................................................................................ 11 In brief ...................................................................................................................................... 12 The master report ................................................................................................................... 12 The sub-reports ....................................................................................................................... 12 Tying the reports together ..................................................................................................... 12 Queries in Computed Fields .......................................................................................................... 15 Overview ...................................................................................................................................... 15 Summary Method ........................................................................................................................ 15 Overview .................................................................................................................................. 15 Worked Example .................................................................................................................... 15 Result Dependant Method......................................................................................................... 17

Advanced Report Writing

Embed Size (px)

Citation preview

Page 1: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 1 of 19

Advanced Report Writing Manual

Contents Target Audience.................................................................................................................................2 Overview .............................................................................................................................................2 Top x Rows.........................................................................................................................................2 Standard Computed Fields ...............................................................................................................2

Overview ........................................................................................................................................2 Report Designer Computed Field...............................................................................................3

Overview....................................................................................................................................3 Calculated Values ......................................................................................................................3 Combining Text and Fields.....................................................................................................4

SQL Select Computed Column...................................................................................................6 Overview....................................................................................................................................6 How to Access the Functions List .........................................................................................7 Examples of Use.......................................................................................................................7

Sub-Queries – Nested Select............................................................................................................9 Overview ........................................................................................................................................9 Method............................................................................................................................................9 Common Mistakes ......................................................................................................................10 Worked Example.........................................................................................................................10

Nested Reports.................................................................................................................................11 Overview ......................................................................................................................................11 Creating Nested Reports ............................................................................................................11

In brief......................................................................................................................................12 The master report ...................................................................................................................12 The sub-reports.......................................................................................................................12 Tying the reports together.....................................................................................................12

Queries in Computed Fields ..........................................................................................................15 Overview ......................................................................................................................................15 Summary Method........................................................................................................................15

Overview..................................................................................................................................15 Worked Example ....................................................................................................................15

Result Dependant Method.........................................................................................................17

Page 2: Advanced Report Writing

Page 2 of 19 Advanced Report Writing Rev 5.01

Overview..................................................................................................................................17 Worked Example ....................................................................................................................17

Other considerations: ........................................................................................................18 Revision History...............................................................................................................................19

Target Audience This document is for use by ProspectSoft CRM Resellers’ staff who have completed the Report Writer Basic & Intermediate course and want to move on to Advanced report writing.

Overview This document is an Advanced guide to report writing and should be used for writing complex reports in different styles. At times throughout this document it is recommended that a copy of the Introduction to InfoMaker document is at hand to help locate the required buttons and formatting options.

Top x Rows This statement will allow the report to return only the top x number of records from a query, a practical use of this could be for graph reports, where the report user only wants to know about their top 20 customers. To use this function, convert the SQL Select screen to syntax, then type after select, top x. Example:

Select top 20 “dba”.”division”.”divno”,

“dba”.”division”.”divname”

From “dba”.”division”

Where ………..

This will only select the top 20 returns and the outputted results will depend highly on the orderby statement used. (This is the sort defined in the SQL Select screen.)

Warning: once an SQL select statement has been modified in this way, it may not be possible to convert back to graphics, ensure that the query is working correctly before attempting this.

Standard Computed Fields

Overview Computed fields in InfoMaker allow the creation of new fields specifically for the use of the report, the new field could contain a hard-coded value, the result of a function on a field from the database, or a totally independent SQL select (see the section on ‘Queries in Computed Fields later in the manual for more details).

There are 2 types of Computed Field, they are:

o Report Designer Computed Field

o SQL Select Computed Column

Page 3: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 3 of 19

Report Designer Computed Field

Overview

The Report Designer Computed fields are created in the Report Designer Screen and can be used in most of the different report styles.

This type of Computed Field is used to display calculated values based on the results of the report or to combine text and fields together to make a report look better.

Calculated Values

In order to calculate values in the report output, all variables to be calculated on must be selected in the SQL Select Screen. For example if the report being written is about problems, the end-user may want to know how old each problem being returned is.

To do this, ensure that the “created” field from the problem table is selected in the SQL Select Screen. Then, in the Report Designer Screen click ‘insert’, ‘control’ then ‘computed field’. Next, click where the computed field is to appear.

Note: As this example will return a different result on each row returned, it is important that the computed field is placed in the detail section.

After inserting the computed field, the expression builder appears:

Using the functions list on the left it is possible to do all sorts of calculations. The function needed for the above example is ‘daysafter(x,x)’ this function simply calculates the number of days difference between two dates. In order for the above computed field to work the function will calculate the number of days between the created date and when the report was run. In order to return today’s date into a report the ‘today()’ function can be used.

Page 4: Advanced Report Writing

Page 4 of 19 Advanced Report Writing Rev 5.01

As displayed by the output of the report, the user can now see how old each problem logged in the ProspectSoft CRM database is.

For the sake of consistency a title should be added for the computed field (column).

Combining Text and Fields

It is possible to create computed fields that combine fixed text values with variable field or argument values. For example, if a report was returning values for a specific division

Page 5: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 5 of 19

and between 2 dates (entered as retrieval arguments), a computed field in the ‘header’ section could display a combination of fixed text, the division name that the report is running for and the dates that the user has entered in for the retrieval arguments.

Ensure that all variables are included in the SQL and any retrieval arguments to be used are defined as usual, in the Report Designer Screen select ‘insert’, ‘control’, then select ‘computed field’, click where the field is to be placed. For the above example the computed field would be placed in the ‘header’ section.

As before, the expression builder will appear. To combine text with values each different variable must be separated with a + symbol and all fixed text must be enclosed in “ ”.

The computed field will then display as:

Another example of this is to group fields together to display them in an easier way. For example, in the above report the contact details are split into title, forename and surname, by using a computed field all these values can be combined into one field.

The existing title, forename and surname fields can be deleted from the report design (do not delete them from the SQL Select as they will be needed for the computed field). Insert the computed field as before, except that instead of being placed in the ‘header’ section it would be placed in the ‘detail’ section.

Page 6: Advanced Report Writing

Page 6 of 19 Advanced Report Writing Rev 5.01

This will now display the contact details in the desired way:

Note: In order to sort in a logical way for the above example, the sort would need to work by surname. Rename the header field to contact_surname_t and insert the surname field back into the report. This field can be made invisible and will only be used to sort the data.

SQL Select Computed Column

Overview

The SQL Select Computed field is used to create a computed field at the point or retrieval. The advantage of using this method of creating computed fields is that the computed field can then be used in the where clause, or if used in graph reports or crosstab report they can be used as values or categories.

The computed column is created in the ‘compute’ tab in the SQL Select Screen.

The format for a computed column is:

<value> as <title>

The <value> can be a hard-coded value, a stand alone function, a function referencing another field (column) or a nested select (covered separately in this document).

Page 7: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 7 of 19

The <title> is whatever best describes the output for this column and will define the labelling of the column in the report designer.

Note: It is advised when labelling computed columns not to use reserved words, InfoMaker may cause errors if attempting to label a column with a reserved word such as “date” or “user”, if in any doubt prefix any titles with initials and an underscore. e.g. peb_date.

How to Access the Functions List

InfoMaker allows the use of all of the SQL functions used with the Sybase database, a list of these functions is made available by right-clicking in the computed column and selecting functions, for more information on the available functions and how they work consult the SQL Anywhere help, this can be found by loading Interactive SQL, cancel the login screen, click the help menu and select SQL Syntax, from here there is an option for ‘Alphabetical List of Functions’.

Examples of Use

In a crosstab report about sales history it might be good to display values (sales income or quantity sold) split by the date that they sales were invoiced, if the crosstab report was to use the invoice date field from within ProspectSoft CRM as the column each different date that an invoice was raised on would have a column to itself, the output of a report such as this would be un-manageable (even for a small sales history). With the use of the SQL functions list the invoiced could be grouped by year, then quarter or month or even week.

In the computed columns section the syntax would appear like so:

If these computed columns were returned into a grid report the results would appear like so:

Page 8: Advanced Report Writing

Page 8 of 19 Advanced Report Writing Rev 5.01

As displayed for the lines returned the computed columns are not returning the actual invoice date, just the year and quarter that the date falls into.

In the crosstab version of this report all the product sold are listed as the rows, with the year then quarter selected as the columns, by having two values in the columns section InfoMaker will subtotal the results for each year.

Page 9: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 9 of 19

Sub-Queries – Nested Select

Overview This section highlights a reporting technique called sub-queries (sometimes called nested selects). Sub-queries are used to return results that don’t meet specific criteria. A lot of reports will return the correct results without using a sub-query. For example a report written to return all companies that are not of a specific company type can be written easily using the where clause to just return the required results, however, writing a report that returned all divisions without a contact that has a specific role code (i.e. ‘DECIDE’, which is the demo code for Decision Maker) is not quite so easy. Writing this report with the techniques used so far would be near impossible. It would be possible to link the division table to the contact table and return all contacts that have a role code other than ‘DECIDE’, however if a division has multiple contacts (1 ‘DECIDE’ and 1 other) it will still appear in the list of divisions without decision makers. Therefore this report would be returning incorrect results.

The best way of writing the above report is to use a sub-query.

The way the sub-query would work is that the report will return a list of all divisions. The report will then take away from this list all of the divisions that do have a decision maker. Logically whatever remains will be the divisions that don’t have a decision maker.

Method The first thing to do is to build the report that will return the master list, the master list should contain all possible results before attempting to remove that which is not required.

Page 10: Advanced Report Writing

Page 10 of 19 Advanced Report Writing Rev 5.01

Now build a ‘where’ clause using the primary key from the master list and the ‘not in’ operator, in the values section of the where clause right-click and choose ‘Select’. InfoMaker will now open another SQL Select screen, in this new select screen build a query that will return a list of the same primary key as selected in the master list that meets the criteria not required. In the above decision maker example, the sub-query would return a list of division numbers that do have a decision maker.

Common Mistakes Mismatch of primary keys, make sure that the field used in the where clause is the same as the field returned from the sub-query.

‘Master List’ includes unnecessary tables, it is habit to include tables that are not required, in the above example it is natural to include the contact table right from the beginning, this will make the report in-accurate as the division will need a contact in order for it to be returned in the master list, a division with no contacts however should appear in a list of divisions with no decision maker.

= operator used, when using sub-queries for this purpose, the ‘not in’ operator must be used.

Worked Example To re-create the above example, the first step is to create the master list, as the requirements for this report are quite simple the only table required in the SQL Select is the division table.

If this report was to be run it would return a list of all the division names in the database. Now the report needs to take away all the division that have a decision maker, to do this select divno as the column in the ‘where’ clause, then change the operator to ‘not in’ right-click in the ‘value’ section and choose ‘Select’.

Page 11: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 11 of 19

This will open the sub-query SQL Select screen. In this screen select the contact and the division tables, select the division.divno to be retuned into the main select. Add a ‘where’ clause to return only decision makers:

Click the ‘return’ button to go back to the main select, and then click return to proceed to the Report Designer.

Nested Reports

Overview The Nested Reports technique is a way of running multiple reports at once, and tying them together in a simple way, this style of report writing is mainly used for summary reports.

Creating Nested Reports In most active databases the following example would not be much use, however, given the limited amount of data in the demo database it should give a good idea of how to use Nested Reports.

Page 12: Advanced Report Writing

Page 12 of 19 Advanced Report Writing Rev 5.01

In brief

The end result will be a list of all the divisions in the database and a summary of the leads and problems that they have.

To achieve this there are 3 reports that will need creating, the full list of divisions, a list of leads by division and a list of problems by division.

The master report

Create a simple tabular report (Note: Do not use grid style for the master list, as each row retrieved will be of varying size, a report style that will enable ‘autosize height’ is required), selecting just from the division table and returning only divno and divname. Save this report.

The sub-reports

Create two grid reports (in a working example any report style for the sub-reports can be used, but for simplicity in this example grid style reports will be used as it is quicker and easier to do), the first report should select appropriate lead information from the lead and contact table, the second report should select appropriate problem information from the problem and contact table, both reports will need a retrieval argument against the contact.divno, the retrieval argument must be of type ‘number’ as this report is comparing numeric values.

(Note: when doing retrieval arguments against divno, contno, etc. the report would normally use the argument type of ‘string’, this is because ProspectSoft CRM would normally return a string for xtratab forms and reports, in this case the comparison is made with the report, not the software, so the ‘number’ argument type must be used)

Tying the reports together

Back in the master report, expand the detail section, click ‘insert’ > ‘control’ > ‘report’, then select where the report is required, a list of saved reports from the current library will appear (if an empty list appears, please ensure that master report has been saved, then try again), select the lead report, repeat the above process and select the problem report.

All three reports are now in the same place, now the results of the report needs to apply to each division. To do this, highlight the lead report, in the ‘general’ tab of the properties section, there is an area for retrieval arguments:

Page 13: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 13 of 19

Click the expression builder

In the expression builder there will be available all the fields and retrieval arguments from the master report. Select divno from the columns, click ‘verify’ then ‘ok’. The sub report will use this value to populate the retrieval argument, repeat this for the problem report. Now for each row returned in our master report, it will run each of the sub-reports and populate the retrieval argument with the appropriate division number.

Note: before the master report will work correctly all the sub-reports will need to be saved again. Open each sub-report, click save and close the report. The master report should now work fine.

Page 14: Advanced Report Writing

Page 14 of 19 Advanced Report Writing Rev 5.01

In order to reduce the empty spaces in the report, move the detail section height so that none of the sub-reports can be seen, as we have AutoSize height automatically selected the detail section will expand accordingly.

End result:

Page 15: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 15 of 19

Queries in Computed Fields

Overview The use of queries in computed fields is possibly one of the most complex sections in this document. A sound knowledge of using SQL select statements will be a real benefit, although InfoMaker can be used to generate the required SQL if required.

Using queries in computed fields can achieve creation of the most complex of reports. The technique has incredible benefits when creating summary style reports and can be used most efficiently for more complex graph reports.

The theory is a combination of the SQL computed fields and the nested reports, however unlike the nested reports we are returning a potentially independent select in to the main report.

It is advised that the ‘About SQL Statements’ section in the ‘Basic Report Writing’ document is used to assist with the SQL in this section.

Note: The entire SQL select statement that is used in a computed field must be enclosed in parenthesis.

Summary Method

Overview

By using the queries in computed fields technique it would be totally possible to display in one report information from entirely un-related areas of the database, for example a report that showed company performance for a period.

The detail of the above report would depend largely on what measures were used to monitor company performance, but at a crude level the report could return the number of problems created, the number of leads created and the number of quotes created. These values could be vastly refined to show more appropriate statistics.

For this example a ‘freeform’ report style would be the most appropriate to use.

In the SQL Select screen, when prompted with the table list, select ‘Show System Tables’, then choose the “sys”.“dummy” table. No fields will be selected from this table, but InfoMaker requires a table to be selected in every select screen.

In the above example the report will return 3 values, the count of leads, problems and quotes, these 3 values need to be created as computed fields.

Worked Example

It is recommended that when creating this type of computed field the user builds it up gradually, for the above example, although the aim is to count the number of leads created during a period, this computed field will just count all the leads in the database for the time being. The syntax for this would appear as so:

Page 16: Advanced Report Writing

Page 16 of 19 Advanced Report Writing Rev 5.01

Once the simple SQL selects are working, it is possible to add the criteria to return just the desired results. For example it is important that we add a where clause to ignore deleted records. The requirement for this report also states that the results should only be returned for a specific time period, for this the report needs to include 2 retrieval arguments, ‘date from’ and ‘date to’.

The retrieval arguments will need to be defined as usual, but will need to be typed manually in the computed fields.

Page 17: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 17 of 19

The resulting freeform report will only prompt the user for the retrieval arguments once, but will carry the value through into all of the computed fields

Result Dependant Method

Overview

The result dependant method of queries in computed fields is used to return one of a number of values into each line of a report, based on an existing value returned.

A good example of this is a list of divisions with a total of their sales history. This could be done with a group report, by hiding the detail section and summing all of the transaction lines returns, however if the report is made in this fashion the user of the report will be unable to drilldown as there is no detail section available to select.

The result dependant method of this report will return a list of the required divisions and include a computed field that will work out the sales history for that division.

Worked Example

First create the report that will return the required list of divisions (a grid report will do), for this example the report will return all of the divisions that are of a ‘Customer’ company type. It is important that in the main report the sales history table is not included.

Page 18: Advanced Report Writing

Page 18 of 19 Advanced Report Writing Rev 5.01

As displayed the only field currently being returned to the report is the division name.

Now add a computed field that will sum the sales history.

At the moment this computed field will return the sum of the whole sales history table and display for each division.

To tie the sum of the sales history the computed field needs to reference results that are returned in the main report, to this add a where clause to the computed field to effectively ‘join’ the division table in the main report to the sales history table in the computed select.

This ‘join’ now means that for each division returned in the main report the computed field will be run and will return just the sales history for that division.

Other considerations:

In most ProspectSoft CRM systems the sales history table would also contain values for orders, so the where clause should be written to ignore these.

The value return from the above select would be return as an integer (the original format for that field) this means that even if the field was divided to the correct number of decimal places, it would still remain an integer, the cast function can be used to convert the integer to a floating decimal or fixed numeric value.

Note: in the result dependant method, the computed fields returned can be used in the where clause, for example to only return divisions with a high enough sales history total.

Page 19: Advanced Report Writing

Advanced Reporting Rev 5.01 Page 19 of 19

Revision History Date Software Doc

Revision Author Review Remarks

30 Jan 05 Infomaker 9 1.0 PEB - Original Document 2 Jun 05 Infomaker 9 5.00 - SNC Small formatting changes

19 Aug 05 Infomaker 9 5.01 - SNC Target Audience para. included