Working with database tables and SQL

The procedures and images in these topics reflect Crystal Reports XI Release 2. For more information on later versions, see the corresponding User Guide available on the Crystal Reports documentation site.

Table linking

In most cases, reports contain data from multiple database tables. They need to be linked together based on common fields.

For example, suppose you want to include the Bill To customer’s phone number on a report that only references the TLORDER table. The phone number resides in the CLIENT table.

  1. Go to Database > Database Expert.

  2. In the Available Data Sources list, go to Current Connections > TMWIN > Tables.

  3. Locate and double-click CLIENT. It is added to the Selected Tables list along with TLORDER.

  4. On the Links tab, both tables are present but unlinked. The common link between them is TLORDER.BILL_TO_CODE and CLIENT.CLIENT_ID.

    In the TLORDER table, locate the BILL_TO_CODE field and drag it to the CLIENT_ID field in the CLIENT table.

    linktbl

  5. Click OK. When prompted, refresh your data.

You now have access to fields in the CLIENT table.

clienttbl

Table joins used in TruckMate reporting

The two types of links, or joins, used in TruckMate reporting are:

  • INNER JOIN
    Returns records that are common in both tables

  • LEFT OUTER JOIN
    Returns all records in your "main" table and the common records in your "lookup" table

In the example above, the two tables represent an INNER JOIN. To change it to a LEFT OUTER JOIN:

  1. Go to Database > Database Expert.

  2. In the Database Expert window, click the Links tab.

  3. Click the line that links the tables together.

  4. Click Link Options.

    linkoption

  5. On the Link Options window, change the join type to Left Outer Join. Leave the other options as is, then click OK.

  6. Click OK on the Database Expert window.

SQL expression fields

If you have a good grasp of SQL, expressions are helpful when you want to use data that is not easily accessed through table linking.

In this example, we will create an expression field that shows the total fuel surcharge for each freight bill.

  1. In the Field Explorer window, right-click SQL Expression Fields and select New.
    The SQL Expression Name window opens.

  2. Enter a name for the expression.

    sqlname

  3. Click OK.
    The SQL Expression Editor opens.

  4. Use the features on this window to build the following expression:

    (SELECT SUM(CHARGE_AMOUNT) FROM ACHARGE_TLORDER WHERE ACODE_ID LIKE 'FUEL%' AND DETAIL_LINE_ID = )

    Note that the expression must be encased in parentheses.

  5. Click Save and Close.

  6. Drag the field from the Field Explorer to the report.

    sqladd

Preview the report and make adjustments as needed.

Command objects

Command objects are useful if you prefer using SQL to table linking, or if you are working with complex reports.

  1. Open the Database Expert.

  2. On the Data page of the Standard Report Creation Wizard, select Add Command.

    addcmd

    The Add Command To Report window opens.

  3. Type your SQL query into the field as indicated.

    addcmdrpt

    Note: Any SELECT statement supported by DB2 works within a command object. Keep this in mind if you convert a Command Center KPI to a Crystal Report.

  4. Click OK.

  5. Click OK in Database Expert.

The command object appears in the Field Explorer under a Command table.

If you are having trouble locating tables for your report:

  1. In TruckMate, go to the application and grid that contains the field(s) you want to include in your report.

  2. Right-click on the field.

  3. From the shortcut menu, select Show Grid’s SQL.

Remember to use the _ALL views in place of a table if you are creating a report showing data from multiple companies.

These illustrations show some common table links. Click an illustration to enlarge it.

Accounts Payable

Checks (bill payments)

sqltblcksbill

Checks (instant checks)

sqltblinst

Invoices

sqltblapinv

Accounts Receivable

sqltblar

Dispatch

sqltbldisp

General Ledger

sqltblgl

Interline Payables

sqltblip

Invoicing

Accessorial charges

sqltblinvac

Details

sqltblinvdet

Payroll checks

sqltblpay