The Power and Possibilities of      SQL Expressions      Kurt Reinhardt, Crystal Advantage
Topics      What is a SQL Expression?      SQL Expressions Vs. Formulas      SQL Expression Samples      When to use a SQL...
What is a SQL Expression?SQL Expressions allow one to:      Eliminate      unnecessary      subeports!      Increase      ...
What is a SQL Expression?      Everybody wants to know!           Knowledge base article c2016183 – What is the intended ...
What is a SQL Expression?                                                         1/2Correlated Subquery      Fields from ...
What is a SQL Expression?                                                         2/2Uncorrelated Subquery      The Subque...
What is a SQL Expression?      General Guidelines to Consider             Intended to be used against SQL-based databases...
Topics      What is a SQL Expression?      SQL Expressions Vs. Formulas      SQL Expression Samples      When to use a SQL...
SQL Expressions Vs. Formulas     Multi-pass Reporting        SQL Expressions - processed on the database during Pass #1  ...
Topics     What is a SQL Expression?     SQL Expressions Vs. Formulas     SQL Expression Samples     When to use a SQL Exp...
SQL Expression Samples     Using SQL Expressions to prequery subreport data:          Customer Report – grouped by Custom...
SQL Expression Samples                                                     1/7Prequery - Two customers, one with a blank s...
SQL Expression Samples                                                     2/7Prequery - 322 subreport instances!Slide 14 ...
SQL Expression Samples                                                    3/7Prequery - Create a SQL ExpressionSlide 15   ...
SQL Expression Samples                                                    4/7Prequery - Check SQL Expression for NULLs wit...
SQL Expression Samples                                                     5/7Prequery - Conditionally suppress subreportS...
SQL Expression Samples                                                     6/7Prequery - Conditionally suppress report sec...
SQL Expression Samples                                                     7/7Prequery - Only 196 instances in 1 minute 15...
SQL Expression Samples     Using SQL Expressions to eliminate subreports:          Report has a subreport that returns a ...
SQL Expression Samples                                                    1/2Aggregate Value – Create SQL ExpressionSlide ...
SQL Expression Samples                                                     2/2Aggregate Value – Use SQL Expression in calc...
SQL Expression Samples     Using SQL Expressions to optimize selection criteria:          Listing of Practitioners with M...
SQL Expression Samples                                                     1/5Optimization – Formulas in the Record Select...
SQL Expression Samples                                                            2/5Optimization – The Formulas aren’t pa...
SQL Expression Samples                                                    3/5Optimization – Create SQL ExpressionsSlide 26...
SQL Expression Samples                                                     4/5Optimization – Substitute SQL Expressions fo...
SQL Expression Samples                                                           5/5Optimization –SQL Expressions ARE pass...
SQL Expression Samples     Using SQL Expressions to filter against aggregates:          Most Recent Orders by Customer   ...
SQL Expression Samples                                                    1/3Most Recent – Create a SQL ExpressionSlide 30...
SQL Expression Samples                                                    2/3Most Recent – Apply SQL Expression to Record ...
SQL Expression Samples                                                           3/3Most Recent – The filter is applied on...
Topics     What is a SQL Expression?     SQL Expressions Vs. Formulas     SQL Expression Samples     When to use a SQL Exp...
When to use SQL Expressions     Generally in table-based reports          Creating Views or Stored Procedures may not be ...
Topics     What is a SQL Expression?     SQL Expressions Vs. Formulas     SQL Expression Samples     When to use a SQL Exp...
Troubleshooting SQL Expression Errors     Only attempt to return a single, distinct value     Use the most recent Service ...
Q&A     Questions        Kurt Reinhardt, Crystal Advantage        I will repeat questions to ensure everyone can hear   ...
Upcoming SlideShare
Loading in...5
×

Crystal Reports - The Power and Possibilities of SQL Expressions

7,778

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,778
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
70
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Business Objects Insight Americas 2006
  • Transcript of "Crystal Reports - The Power and Possibilities of SQL Expressions"

    1. 1. The Power and Possibilities of SQL Expressions Kurt Reinhardt, Crystal Advantage
    2. 2. Topics What is a SQL Expression? SQL Expressions Vs. Formulas SQL Expression Samples When to use a SQL Expression Troubleshooting SQL Expression errors Q&ASlide 3 Copyright © 2006 Business Objects S.A. All rights reserved.
    3. 3. What is a SQL Expression?SQL Expressions allow one to: Eliminate unnecessary subeports! Increase performance! Optimize efficiency! Reduce subreport instances!Slide 4 Copyright © 2006 Business Objects S.A. All rights reserved.
    4. 4. What is a SQL Expression? 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. UncorrelatedSlide 5 Copyright © 2006 Business Objects S.A. All rights reserved.
    5. 5. What is a SQL Expression? 1/2Correlated Subquery 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_SCOPE FROM FINDING_HEADER ASlide 6 Copyright © 2006 Business Objects S.A. All rights reserved.
    6. 6. What is a SQL Expression? 2/2Uncorrelated Subquery 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 C ORDER BY C."Customer ID"Slide 7 Copyright © 2006 Business Objects S.A. All rights reserved.
    7. 7. What is a SQL Expression? 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 valueSlide 8 Copyright © 2006 Business Objects S.A. All rights reserved.
    8. 8. Topics What is a SQL Expression? SQL Expressions Vs. Formulas SQL Expression Samples When to use a SQL Expression Troubleshooting SQL Expression errors Q&ASlide 9 Copyright © 2006 Business Objects S.A. All rights reserved.
    9. 9. 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 elementsSlide 10 Copyright © 2006 Business Objects S.A. All rights reserved.
    10. 10. Topics What is a SQL Expression? SQL Expressions Vs. Formulas SQL Expression Samples When to use a SQL Expression Troubleshooting SQL Expression errors Q&ASlide 11 Copyright © 2006 Business Objects S.A. All rights reserved.
    11. 11. 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 impactedSlide 12 Copyright © 2006 Business Objects S.A. All rights reserved.
    12. 12. SQL Expression Samples 1/7Prequery - Two customers, one with a blank subreportSlide 13 Copyright © 2006 Business Objects S.A. All rights reserved.
    13. 13. SQL Expression Samples 2/7Prequery - 322 subreport instances!Slide 14 Copyright © 2006 Business Objects S.A. All rights reserved.
    14. 14. SQL Expression Samples 3/7Prequery - Create a SQL ExpressionSlide 15 Copyright © 2006 Business Objects S.A. All rights reserved.
    15. 15. SQL Expression Samples 4/7Prequery - Check SQL Expression for NULLs with FormulaSlide 16 Copyright © 2006 Business Objects S.A. All rights reserved.
    16. 16. SQL Expression Samples 5/7Prequery - Conditionally suppress subreportSlide 17 Copyright © 2006 Business Objects S.A. All rights reserved.
    17. 17. SQL Expression Samples 6/7Prequery - Conditionally suppress report sectionSlide 18 Copyright © 2006 Business Objects S.A. All rights reserved.
    18. 18. SQL Expression Samples 7/7Prequery - Only 196 instances in 1 minute 15 seconds!That’s a 44% performance increase!Slide 19 Copyright © 2006 Business Objects S.A. All rights reserved.
    19. 19. 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 complexSlide 20 Copyright © 2006 Business Objects S.A. All rights reserved.
    20. 20. SQL Expression Samples 1/2Aggregate Value – Create SQL ExpressionSlide 21 Copyright © 2006 Business Objects S.A. All rights reserved.
    21. 21. SQL Expression Samples 2/2Aggregate Value – Use SQL Expression in calculationsSlide 22 Copyright © 2006 Business Objects S.A. All rights reserved.
    22. 22. 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 suffersSlide 23 Copyright © 2006 Business Objects S.A. All rights reserved.
    23. 23. SQL Expression Samples 1/5Optimization – Formulas in the Record Selection CriteriaSlide 24 Copyright © 2006 Business Objects S.A. All rights reserved.
    24. 24. SQL Expression Samples 2/5Optimization – 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"Slide 25 Copyright © 2006 Business Objects S.A. All rights reserved.
    25. 25. SQL Expression Samples 3/5Optimization – Create SQL ExpressionsSlide 26 Copyright © 2006 Business Objects S.A. All rights reserved.
    26. 26. SQL Expression Samples 4/5Optimization – Substitute SQL Expressions for FormulasSlide 27 Copyright © 2006 Business Objects S.A. All rights reserved.
    27. 27. SQL Expression Samples 5/5Optimization –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"Slide 28 Copyright © 2006 Business Objects S.A. All rights reserved.
    28. 28. 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 suffersSlide 29 Copyright © 2006 Business Objects S.A. All rights reserved.
    29. 29. SQL Expression Samples 1/3Most Recent – Create a SQL ExpressionSlide 30 Copyright © 2006 Business Objects S.A. All rights reserved.
    30. 30. SQL Expression Samples 2/3Most Recent – Apply SQL Expression to Record SelectionSlide 31 Copyright © 2006 Business Objects S.A. All rights reserved.
    31. 31. SQL Expression Samples 3/3Most 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" )) …Slide 32 Copyright © 2006 Business Objects S.A. All rights reserved.
    32. 32. Topics What is a SQL Expression? SQL Expressions Vs. Formulas SQL Expression Samples When to use a SQL Expression Troubleshooting SQL Expression errors Q&ASlide 33 Copyright © 2006 Business Objects S.A. All rights reserved.
    33. 33. 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 functionalitySlide 34 Copyright © 2006 Business Objects S.A. All rights reserved.
    34. 34. Topics What is a SQL Expression? SQL Expressions Vs. Formulas SQL Expression Samples When to use a SQL Expression Troubleshooting SQL Expression errors Q&ASlide 35 Copyright © 2006 Business Objects S.A. All rights reserved.
    35. 35. 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 analyzerSlide 36 Copyright © 2006 Business Objects S.A. All rights reserved.
    36. 36. Q&A Questions  Kurt Reinhardt, Crystal Advantage  I will repeat questions to ensure everyone can hear Contact information  Email: kreinhardt@crystaladvantage.comSlide 37 Copyright © 2006 Business Objects S.A. All rights reserved.
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×