- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Figure 10-9. The partial results of comparing UNION to GROUPING SETS in Font
Figure 10-9. The partial results of comparing UNION to GROUPING SETS Draw PDF-417 2d Barcode In None Using Barcode creation for Font Control to generate, create PDF-417 2d barcode image in Font applications. www.OnBarcode.comCode-39 Generator In None Using Barcode creator for Font Control to generate, create Code 39 Full ASCII image in Font applications. www.OnBarcode.comPivoted Queries
Encode PDF-417 2d Barcode In None Using Barcode creator for Font Control to generate, create PDF-417 2d barcode image in Font applications. www.OnBarcode.comPainting Data Matrix In None Using Barcode generator for Font Control to generate, create Data Matrix ECC200 image in Font applications. www.OnBarcode.comNormally, a query displays the data in a way that is similar to how it looks in a table, often with the column headers being the actual names of the columns within the table. Generating EAN-13 Supplement 5 In None Using Barcode drawer for Font Control to generate, create EAN13 image in Font applications. www.OnBarcode.comPainting EAN / UCC - 13 In None Using Barcode generator for Font Control to generate, create EAN / UCC - 14 image in Font applications. www.OnBarcode.comCHAPTER 10 WRITING ADVANCED QUERIES
Paint Quick Response Code In None Using Barcode creator for Font Control to generate, create QR image in Font applications. www.OnBarcode.comEAN-8 Supplement 5 Add-On Creation In None Using Barcode printer for Font Control to generate, create European Article Number 8 image in Font applications. www.OnBarcode.comA pivoted query displays the values of one column as column headers instead. For example, you could display the sum of the sales by month so that the month names are column headers. Each row would then contain the data by year with the sum for each month displayed from left to right. This section shows how to write pivoted queries with two techniques: CASE and PIVOT. PDF-417 2d Barcode Scanner In .NET Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comPrinting PDF417 In Java Using Barcode creation for Android Control to generate, create PDF417 image in Android applications. www.OnBarcode.comPivoting Data with CASE
UCC - 12 Encoder In None Using Barcode creation for Word Control to generate, create UPC-A image in Office Word applications. www.OnBarcode.comGenerate Code39 In None Using Barcode creation for Online Control to generate, create Code 3 of 9 image in Online applications. www.OnBarcode.comBefore SQL Server 2005, many developers used the CASE function to create pivoted results. (See The Case Function section in 3 to learn more about CASE.) In fact, many still use this technique. Essentially, you use several CASE expressions in the query, one for each pivoted column header. For example, the query will have a CASE expression checking to see whether the month of the order date is January. If the order does occur in January, supply the total sales value. If not, supply a zero. For each row, the data ends up in the correct column where it can be aggregated. Here is the syntax for using CASE to pivot data: CASE <col1>,SUM(CASE <col3> WHEN <value1> THEN <col2> ELSE 0 END) AS <alias1>, SUM(CASE <col3> WHEN <value2> THEN <col2> ELSE 0 END) AS <alias2>, SUM(CASE <col3> WHEN <value3> THEN <col2> ELSE 0 END) AS <alias3> FROM <table1> GROUP BY <col1> Type in and execute Listing 10-10 to learn how to pivot data using CASE. Listing 10-10. Using CASE to Pivot Data USE AdventureWorks2008; GO SELECT YEAR(OrderDate) AS OrderYear, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS Jan, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS Feb, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS Mar, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS Apr, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS May, ROUND(SUM(CASE MONTH(OrderDate) WHEN AS Jun FROM Sales.SalesOrderHeader GROUP BY YEAR(OrderDate) ORDER BY OrderYear; Scanning Quick Response Code In None Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comReading QR In VS .NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.com1 THEN TotalDue ELSE 0 END),0) 2 THEN TotalDue ELSE 0 END),0) 3 THEN TotalDue ELSE 0 END),0) 4 THEN TotalDue ELSE 0 END),0) 5 THEN TotalDue ELSE 0 END),0) 6 THEN TotalDue ELSE 0 END),0) Code128 Decoder In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comData Matrix ECC200 Creator In Visual Basic .NET Using Barcode creation for .NET framework Control to generate, create Data Matrix image in .NET applications. www.OnBarcode.comCHAPTER 10 WRITING ADVANCED QUERIES
Code 128 Code Set B Encoder In Objective-C Using Barcode creation for iPhone Control to generate, create USS Code 128 image in iPhone applications. www.OnBarcode.comPDF417 Creator In VB.NET Using Barcode maker for Visual Studio .NET Control to generate, create PDF 417 image in .NET applications. www.OnBarcode.comFigure 10-10 shows the results. To save space in the results, the statement calculates the totals only for the months January through June and uses the ROUND function. The GROUP BY clause contains just the YEAR(OrderDate) expression. You might think that you need to group by month as well, but this query doesn t group by month. It just includes each TotalDue value in a different column depending on the month. Generate Data Matrix 2d Barcode In Java Using Barcode printer for Android Control to generate, create Data Matrix 2d barcode image in Android applications. www.OnBarcode.comBarcode Generator In .NET Using Barcode generation for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comFigure 10-10. The results of using CASE to create a pivot query
Using the PIVOT Function
Microsoft introduced the PIVOT function with SQL Server 2005. In my opinion, the PIVOT function is more difficult to understand than using CASE to produce the same results. Just like CASE, you have to hard-code the column names. This works fine when the pivoted column names will never change, such as the months of the year. When the query bases the pivoted column on data that changes over time, such as employee or department names, the query must be modified each time that data changes. Here is the syntax for PIVOT: SELECT <groupingCol>, <pivotedValue1> [AS <alias1>], <pivotedValue2> [AS <alias2>] FROM (SELECT <groupingCol>, <value column>, <pivoted column>) AS <queryAlias> PIVOT ( <aggregate function>(<value column>) FOR <pivoted column> IN (<pivotedValue1>,<pivotedValue2>) ) AS <pivotAlias> [ORDER BY <groupingCol>] The SELECT part of the query lists any nonpivoted columns along with the values from the pivoted column. These values from the pivoted column will become the column names in your query. You can use aliases if you want to use a different column name than the actual value. For example, if the column names will be the month numbers, you can alias with the month names. This syntax uses a derived table, listed after the word FROM, as the basis of the query. See the Derived Tables section in 4 to review derived tables. Make sure that you only list columns that you want as grouping levels, the pivoted column, and the column that will be summarized in this derived table. Adding other columns to this query will cause extra grouping levels and unexpected results. The derived table must be aliased, so don t forget that small detail. Tip It is possible to use a CTE to write this query instead of a derived table. See the article Create Pivoted
|
|