This page intentionally left blank chapter 20 Strategies for Displaying Data In this final chapter, we’re going to return to the main theme of this book, which is how to retrieve data from relational databases. In the past few chapters, we’ve taken a slight detour from the related topics of updating data, maintaining tables, and designing databases. But now, we want to focus again on the role of SQL in retrieving data. More specifically, we want to address situations where data is presented to users via reporting software. Beyond SQL Specialized reporting tools are often used to present data to users. Examples of this type of software include Microsoft Reporting Services and Crystal Reports. These software packages allow developers to connect to database s via SQL, and they provide a well-designed user interface, allowing users to easily access data via predefined reports. With minimal effort, these reporting tools also enable developers to present data in a variety of formats. In addition, most reporting tools enable users to export retrieved data to a spread- sheet, such as Excel. This provides opportunities for users to manipulate their own data, allowing them to transform data into formats unique to spreadsheets. With this in mind, the purpose of this chapter is to raise an awareness on the part of the database developer of what can be accomplished either through reporting software or by users manipulating data in spreadsheets. Either way, there are 207 opportunities for offloading some of the complexity that would ordinarily be involved with using SQL to other tools or the end user. Often, it is easier and better for the user to play a role in the final arrangement of data than it is for the database developer to attempt to do it all in SQL. Basically, we’ll consider the possibility of avoiding overly complex SQL state- ments when similar functionality can be handled either by a reporting tool or by the user viewing the data. Reporting Tools and Crosstab Reports We’re going to use Micr osoft Reporting Services to illustrate what can be done with reporting tools. This is just one of many available tools, including Crystal Reports, Cognos, and MicroStrategy. Specifically, we’re going to look at a report type that Microsoft calls a matrix report. Outside of Microsoft, matrix reports are commonly referred to as crosstab reports. The simplest way to create a new report in Microsoft Reporting Services is through their Report Wizard. After being invoked, the wizard runs you through the following steps: 1. Obtain a data source and connection. 2. Create the query, either through a provided SELECT statement or by using a built-in Query Builder. 3. Specify a report type. Options include tabular and matrix. 4. Arrange specific columns from the SELECT or Query Builder into various report areas. 5. Select a visual style for the report. Step 3 allows you to specify the report type. In Microsoft terminology, a tabular report presents data in the normal fashion. Data elements are shown as columns, and each occurrence appears as a new row. The data shown in the report is exactly as the SELECT statement or Query Builder specifies. The matrix report is the new feature we want to consider. When the matrix report type is utilized, data items are not placed into columns as normal. Instead, data items are placed into one of four different areas of the report: rows, columns, details, and pages. Chapter 20 ■ Strategies for Displaying Data208 The best way to illustrate the difference between tabular and matrix reports is with a simple example. Let’s say that you have a SELECT statement such as the following: SELECT CustomerName AS 'Customer', ProductCategory AS 'Product Category', OrderAmount AS 'Amount' FROM SalesTable When you create a tabular report with this statement, a report might appear as in Figure 20.1. Figure 20.1 Tabular report. Data in this tabular report is presented in the normal arrangement of columns and rows. Now, let’s see what the report looks like if the same SELECT statement is used in conjunction with a matrix report. In specifying the matrix report, the CustomerName column can be put in the rows area, ProductCategory in the columns area, and OrderAmount in the details area. Figure 20.2 display s the resulting matrix report. Figure 20.2 Matrix report. Reporting Tools and Crosstab Reports 209 The matrix report presents data in an entirely different way. Rather than showing individual rows, it summa rizes the data by customer and product groups. The report has dynamically determined which unique values exist in the customers and product groups, and it presents the necessary rows and columns to display all the values. The details area of a matrix report requires data items with quantitative values, because those values will be summed up automatically in the matrix report. Notice that in the tabular report, Pollen, had two orders of chairs, one for 40 and one for 200. In the matrix report, these two values were summed automatically, to display a total of 240. There are, of course, numerous other features and capabilities of this and other reporting tools. The main point is to remain mindful of what can accomplished with reporting tools to lessen the burden on the developer writing SQL statements. Spreadsheets and Pivot Tables In addition to reporting tools, spreadsheets also provide a great deal of func- tionality for you to manipulate data. Most reporting tools allow you to export data into an Excel spreads heet. At that point, you have the ability to do whatever you want in Excel to format and analyze the data. Many basic Excel functions and features overlap what can be specified in a SELECT statement. For example, Excel allows you to sort data easily. Excel provides numerous built-in functions that are similar or identical to the built-in functions in SQL. Another important feature of Excel is the capability to group data with subtotals. This means that if you need both detailed data and subtotals by group, you can easily accomplish this in Excel. The SQL developer may need to provide data at only the lowest level of detail. With a few keystrokes in Excel, you can then group data and add subtotals as desired. However, the most significant feature that Excel provides in this regard is the pivot table. With Excel, you have the ability to select any area of data on a worksheet and convert that data into a pivot table. At a basic level, a pivot table is the equivalent of the matrix (crosstab) report type seen in the previous example. Chapter 20 ■ Strategies for Displaying Data210 . developer of what can be accomplished either through reporting software or by users manipulating data in spreadsheets. Either way, there are 207 opportunities for offloading some of the complexity. automatically, to display a total of 240. There are, of course, numerous other features and capabilities of this and other reporting tools. The main point is to remain mindful of what can accomplished with. users. Examples of this type of software include Microsoft Reporting Services and Crystal Reports. These software packages allow developers to connect to database s via SQL, and they provide a