36
The Power and Possibilities of SQL Expressions Kurt Reinhardt, Crystal Advantage

Crystal Reports - The Power and Possibilities of SQL Expressions

Embed Size (px)

DESCRIPTION

Presentation from the Business Objects Insight 2006 conference that is still relevant to Crystal Reports today!

Citation preview

Page 1: Crystal Reports - The Power and Possibilities of SQL Expressions

The Power and Possibilities of SQL Expressions

The Power and Possibilities of SQL Expressions

Kurt Reinhardt, Crystal Advantage

Page 2: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 3 Copyright © 2006 Business Objects S.A. All rights reserved.

What is a SQL Expression?

SQL Expressions Vs. Formulas

SQL Expression Samples

When to use a SQL Expression

Troubleshooting SQL Expression errors

Q&A

Topics

Page 3: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 4 Copyright © 2006 Business Objects S.A. All rights reserved.

Eliminate unnecessary subeports!

Increase performance!

Optimize efficiency!

Reduce subreport instances!

What is a SQL Expression?

SQL Expressions allow one to:

Page 4: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 5 Copyright © 2006 Business Objects S.A. All rights reserved.

Everybody wants to know! Knowledge base article c2016183 – What is the intended use of ‘SQL

Expression Fields’? was one of the most visited articles for July 2006!

Similar to Formulas but written in SQL Generally used against SQL-based databases Will be disabled if your database isn’t compatible

SQL Concepts Subqueries / Subselects Correlated Vs. Uncorrelated

What is a SQL Expression?

Page 5: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 6 Copyright © 2006 Business Objects S.A. All rights reserved.

Fields from the Outer Query are passed to the Subquery:

SELECT A.REPAIR_ORDER_ID, A.REPAIR_FACILITY_CD, NVL((SELECT DECODE(X.FINDING_CONDITION_TXT,’TRUE’,’Y’,’N’) FROM RECORDED_FINDING_CONDITION X WHERE X.REPAIR_ORDER_ID = A.REPAIR_ORDER_ID AND X.REPAIR_FACILITY_CD = A.REPAIR_FACILITY_CD AND X.FINDING_QUESTION_ID = ‘smart_scope’ AND X. FINDING_CONDITION_ID = ‘TRUE’ ),’N’) SMART_SCOPEFROM FINDING_HEADER A

What is a SQL Expression? 1/2

Correlated Subquery

Page 6: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 7 Copyright © 2006 Business Objects S.A. All rights reserved.

The Subquery doesn’t relate to any fields:

SELECT C."Customer ID", C."Customer Name", ( SELECT SUM("Order Amount") FROM ORDERS ) 'Total Orders'FROM CUSTOMER CORDER BY C."Customer ID"

What is a SQL Expression? 2/2

Uncorrelated Subquery

Page 7: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 8 Copyright © 2006 Business Objects S.A. All rights reserved.

General Guidelines to Consider Intended to be used against SQL-based databases Can be used against certain PC-based databases Can’t access Formula or Parameter fields Available functions are based on your db and method of connectivity SQL Expressions are always added as fields in the SELECT clause SQL Expressions must return a single, distinct value

What is a SQL Expression?

Page 8: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 9 Copyright © 2006 Business Objects S.A. All rights reserved.

What is a SQL Expression?

SQL Expressions Vs. Formulas

SQL Expression Samples

When to use a SQL Expression

Troubleshooting SQL Expression errors

Q&A

Topics

Page 9: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 10 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expressions Vs. Formulas

Multi-pass Reporting SQL Expressions - processed on the database during Pass #1 Formulas - many processed on the client during Pass #2

Database Considerations SQL Expressions – used against SQL-based databases SQL Expressions – must be used against tables / views Formulas - can be used against any data source

Report Design SQL Expressions – only reference database fields Formulas - can reference all other Crystal Reports field elements

Page 10: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 11 Copyright © 2006 Business Objects S.A. All rights reserved.

What is a SQL Expression?

SQL Expressions Vs. Formulas

SQL Expression Samples

When to use a SQL Expression

Troubleshooting SQL Expression errors

Q&A

Topics

Page 11: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 12 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples

Using SQL Expressions to prequery subreport data: Customer Report – grouped by Customer Has a subreport in the Group Footer The subreports don’t always have data, but still run every time Report performance suffers The database and network are impacted

Page 12: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 13 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 1/7

Prequery - Two customers, one with a blank subreport

Page 13: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 14 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 2/7

Prequery - 322 subreport instances!

Page 14: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 15 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 3/7

Prequery - Create a SQL Expression

Page 15: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 16 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 4/7

Prequery - Check SQL Expression for NULLs with Formula

Page 16: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 17 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 5/7

Prequery - Conditionally suppress subreport

Page 17: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 18 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 6/7

Prequery - Conditionally suppress report section

Page 18: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 19 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 7/7

Prequery - Only 196 instances in 1 minute 15 seconds!

That’s a 44% performance increase!

Page 19: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 20 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples

Using SQL Expressions to eliminate subreports: Report has a subreport that returns a single aggregate value A Shared Variable is used to pass the value back to the main report Main report uses the value in calculations Performance is not optimal due to subreport Report is overly complex

Page 20: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 21 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 1/2

Aggregate Value – Create SQL Expression

Page 21: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 22 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 2/2

Aggregate Value – Use SQL Expression in calculations

Page 22: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 23 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples

Using SQL Expressions to optimize selection criteria: Listing of Practitioners with Missing or Incomplete SSNs Report uses Formulas in the Record Selection Criteria The Formula isn’t passed to the database for processing Performance suffers

Page 23: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 24 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 1/5

Optimization – Formulas in the Record Selection Criteria

Page 24: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 25 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 2/5

Optimization – The Formulas aren’t passed to the database

Database|Show SQL Query Notice, there is no WHERE clause All possible records will be returned and filtered out on the Client

Example of code below

SELECT "ENTITY"."ENTITY", "PRACENT"."PRACID", "PRACENT"."NAME", "PRAC"."SSNUM", "ENTITY"."DESCRIPTION"

FROM ("ENTITY" "ENTITY" INNER JOIN "PRACENT" "PRACENT" ON "ENTITY"."ENTITY"="PRACENT"."ENTITY") INNER JOIN "PRAC" "PRAC" ON "PRACENT"."PRACNO"="PRAC"."PRACNO"

ORDER BY "ENTITY"."ENTITY", "PRACENT"."NAME"

Page 25: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 26 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 3/5

Optimization – Create SQL Expressions

Page 26: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 27 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 4/5

Optimization – Substitute SQL Expressions for Formulas

Page 27: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 28 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 5/5

Optimization –SQL Expressions ARE passed to the database!

Database|Show SQL Query WHERE clause is present Only records that meet the criteria will be returned

Example of code below …

WHERE ("PRAC"."SSNUM" IS NULL OR ({fn RTRIM(PRAC."SSNUM")}

)='' OR ({fn LENGTH(PRAC."SSNUM")}

)<>11)

ORDER BY "ENTITY"."ENTITY", "PRACENT"."NAME"

Page 28: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 29 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples

Using SQL Expressions to filter against aggregates: Most Recent Orders by Customer The report lists all customers and their most recent order only Traditional Method 1: using a subreport to find the Max Order Date Traditional Method 2: suppressing all records via Group Selection Report is inefficient Performance suffers

Page 29: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 30 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 1/3

Most Recent – Create a SQL Expression

Page 30: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 31 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 2/3

Most Recent – Apply SQL Expression to Record Selection

Page 31: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 32 Copyright © 2006 Business Objects S.A. All rights reserved.

SQL Expression Samples 3/3

Most Recent – The filter is applied on the database!

Database|Show SQL Query WHERE clause is present Only records that meet the criteria will be returned

Example of code below …

WHERE "Orders"."Order Date"=((

SELECT

MAX("ORDER DATE")

FROM

ORDERS

WHERE

"CUSTOMER ID" = "Customer"."Customer ID"

))

Page 32: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 33 Copyright © 2006 Business Objects S.A. All rights reserved.

What is a SQL Expression?

SQL Expressions Vs. Formulas

SQL Expression Samples

When to use a SQL Expression

Troubleshooting SQL Expression errors

Q&A

Topics

Page 33: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 34 Copyright © 2006 Business Objects S.A. All rights reserved.

When to use SQL Expressions

Generally in table-based reports Creating Views or Stored Procedures may not be an option Many companies limit database access for report writers The database may be proprietary It may be simpler to create a SQL Expression than to modify a View Stored Procedures and SQL Commands may limit functionality

Page 34: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 35 Copyright © 2006 Business Objects S.A. All rights reserved.

What is a SQL Expression?

SQL Expressions Vs. Formulas

SQL Expression Samples

When to use a SQL Expression

Troubleshooting SQL Expression errors

Q&A

Topics

Page 35: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 36 Copyright © 2006 Business Objects S.A. All rights reserved.

Troubleshooting SQL Expression Errors

Only attempt to return a single, distinct value

Use the most recent Service Pack Especially important for Crystal Reports 7 – Crystal Reports 8.5

Some keywords are prohibited

Try encapsulating your code in parentheses Required by some databases such as MS SQL Server

Test the code in an analyzer first

Copy the code from the analyzer

Page 36: Crystal Reports - The Power and Possibilities of SQL Expressions

Slide 37 Copyright © 2006 Business Objects S.A. All rights reserved.

Questions Kurt Reinhardt, Crystal Advantage I will repeat questions to ensure everyone can hear

Contact information Email: [email protected]

Q&A