Doorgaan naar hoofdcontent

Crystal Reports: On SQL Expression Fields

This is based on an article from the SAP site:

http://search.sap.com/notes?id=0001217871&boj=/sap/bc/bsp/spn/scn_bosap/notes.do?access=69765F6D6F64653D3939382669765F7361706E6F7465735F6E756D6265723D30303031323137383731

I tried to add some more examples to it.

SQL Expression Field definition

SQL Expression fields are similar to formula fields, but they are written in Structured Query Language (SQL). They are useful in optimizing report performance because the tasks they execute are performed on the database server.

You can use SQL Expression fields to perform pre-defined functions on database fields. The list of available functions depends on the type of database in use. This list is available in the Function Tree of the SQL Expression Editor dialog box.

NOTE: If Crystal Reports recognizes the function name it will turn blue in the SQL Expression Editor dialog box. Additional database functions may be available to you, depending on your database, but they will not turn blue.

SQL Expression field versus a Crystal Reports formula field

Often an SQL Expression field and a Crystal Reports formula field can accomplish the same goal. For example, a report design requirement is to display the first 3 letters of the first name of an employee.

A formula field or an SQL Expression field can fulfill this requirement. The advantage of the SQL Expression field is that the processing of the request will be done on the database server instead of in the Crystal Reports Designer. This results in a faster processing time.

SQL Expression for SQL Server:

{fn LEFT("Employee"."First Name",3)}

Crystal Reports formula:

left({Employee.First Name},3)

SQL Expression Fields and SELECT Statements

Using a SELECT statement in a SQL Expression field is not supported. Generally, an SQL Expression field cannot contain a SELECT statement because Crystal Reports can only process one SELECT statement per main report. If a SELECT statement is included in an SQL Expression field, an error message, similar to the following, may appear:


 The exact wording of the message depends upon the database in use. In this case, I used SQL Server.
 
Although SQL Expressions with SELECT statements are not supported, they can work if they only return a single value.  For example, Maximum, Minimum and Count are functions that return a single value.

It appears to be neceassary to enclose the SELECT statement with parentheses.  For example, a Microsoft SQL Server requires the SELECT statement to be enclosed in parentheses and so does Microsoft Access.

Example for the SQL Server error message when you leave the parenthesis out:

Example with the parenthesis:
 
(
SELECT DISTINCT 'Yes'
FROM orders
WHERE CustomerId = "Customers"."CustomerID"
)

Same example for Microsoft Access:

(
SELECT DISTINCT 'Yes'
FROM orders
WHERE CustomerId = `Customers`.`CustomerID`
)

In both cases, the Show SQL Query box will look something like this:


Database Functions Not Pre-Defined in Crystal Reports

Database functions that are not pre-defined in Crystal Reports can be used in SQL Expression fields.
For example, in case of the SQL Server:

LOWER("table"."field")

LOWER is not a pre-defined function in Crystal Reports and, therefore, will not turn blue in the SQL Expression Editor. However, the syntax will be accepted and you can now insert this field into your report.
Another example for the SQL Server:
 
DATEPART("WW", "Orders"."OrderDate")
 
Another example for Microsoft Access:
 
DATEPART("WW", `Orders`.`OrderDate`,2,2)

In both cases the Show SQL Query box will look something like this:

Parameters

You can not use parameters in an SQL Expression field. In case you want to use them, you have to switch to Commands.

Reacties

Populaire posts van deze blog

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To 66: For m = 32 To 126                         ActiveSheet.Unprotect Chr(a) & Chr(b) & _   Chr(c) & Chr(d) & Chr(e) & Chr(f) & _   Chr(g) & Chr(h) &  Chr(I) & Chr(j) & C

Excel 2013: uniek aantal in draaitabel

Tot en met versie 2010 was het in Excel lastig om in een draaitabel een uniek aantal (DISTINCT COUNT) te tellen. We geven een voorbeeld op basis van een verkoperslijst. In deze lijst kunnen we zien welke verkopers welke artikelen hebben verkocht. Willen we nu in een draaitabel laten zien hoeveel artikelen een verkoper heeft verkocht, dan krijgen we wel de aantallen maar niet de unieke aantallen te zien. Om toch de unieke aantallen te laten zien, hebben we een aantal stappen nodig. Op het moment dat we de draaitabel invoegen, krijgen we in Excel 2013 dit dialoogvenster: Onderaan zien we daar een nieuwe optie: Deze gegevens toevoegen aan het gegevensmodel . Deze optie moeten we aanvinken, voor we op OK klikken. We krijgen dan een iets ander beeld dan normaal: Normaliter krijgen we alleen de veldnamen. Nu zien we er het woord Bereik boven staan. Voor het voorbeeld heb ik nu Verkoper toegevoegd aan Rijen en Artikelomschrijving aan Waarden . Het resultaat is identiek