244 40 8MB
English Pages 683 Year 2015
The Tera-Tom Video Series
Lessons with Tera-Tom Teradata Architecture and SQL Video Series These exciting videos make learning and certification much easier
Three ways to view them: 1. Safari (look up Coffing Studios) 2. CoffingDW.com (sign-up on our website) 3. Your company can buy them all for everyone to see (contact [email protected])
The Tera-Tom Genius Series
The Tera-Tom Genius Series consists of ten books. Each book is designed for a specific audience, and Teradata is explained to the level best suited for that audience. The books take a building block approach; always starting out simple, then each page builds upon the previous point. Order them all at www.CoffingDW.com.
Tera-Tom- Author of over 50 Books
Tera-Tom books have been the primary source of Teradata learning for over 20 years. They have helped to teach millions of people all aspects of Teradata. What people love the most about the Tera-Tom books is how easy they are to understand. They are so easy that a seven year old boy (raised by wolves) can understand them!
The Best Query Tool Works on all Systems
When you possess a tool like Nexus, you have access to every system in your enterprise! The Nexus Query Chameleon is the only tool that works on all systems. Its Super Join Builder allows for the ERwin Logical Model to be loaded, and then Nexus shows tables and views visually. It then guides users to show what joins to what. As users choose the tables and columns they want in their report, Nexus builds the SQL for them with each click of the mouse. Nexus was designed for Teradata and Hadoop, but works on all platforms. Nexus even converts table structures between vendors, so querying and managing multi-vendor platforms is transparent. Even if you only work with one system, you will find that the Nexus is the best query tool you have ever used. If you work with multiple systems, you will be even more amazed. Download a free trial at www.CoffingDW.com.
Trademarks and Copyrights Microsoft Windows, Windows 2003 Server, SQL Server 2012, SQL Server Compact Edition, .NET, PDW, SQL Server, T-SQL, Azure SQL Data Warehouse and Azure Cloud are trademarks of Microsoft. Teradata, NCR, BYNET and SQL Assistant are registered trademarks of Teradata Corporation, Dayton, Ohio, U.S.A., IBM, DB2 and Netezza are registered trademarks of IBM Corporation, ANSI is a registered trademark of the American National Standards Institute. Ethernet is a trademark of Xerox. UNIX is a trademark of The Open Group. Linux is a trademark of Linus Torvalds. Java and Oracle is a trademark of Oracle. ParAccel is a trademark of ParAccel. Kognitio is a trademark of Kognitio. Greenplum is a trademark of EMC Corporation. Nexus Query Chameleon is a trademark of Coffing Data Warehousing. Coffing Data Warehousing shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of programs or program segments that are included. The manual is not a publication of Microsoft Corporation, nor was it produced in conjunction with Microsoft Corporation. Copyright © May 2015 by Coffing Publishing ISBN 978-1-940540-30-6 All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, neither is any liability assumed for damages resulting from the use of information contained herein.
About Tom Coffing
Tom Coffing, better known as Tera-Tom, is the founder of Coffing Data Warehousing where he has been CEO for the past 20 years. Tom has written over 50 books on all aspects of Teradata, Netezza, Kognitio, Redshift, ParAccel, Vertica, SQL Server, and Greenplum. Tom has taught over 1,000 Teradata classes in places such as India, Africa, Europe, China, Malaysia, and throughout North America. Tom is also the owner and designer of the Nexus Query Chameleon, the most sophisticated enterprise query tool in the industry. The Nexus works on all platforms, including Hadoop, converts table structures between all systems, and allows companies to load their ERwin logical model inside Nexus. The Nexus guides users like a GPS system. Users point and click on any table or view from any system, and they are guided to what joins to what. As users choose the columns they want on their report, the SQL is built automatically. In High School, Tom was the first athlete from his school to ever place at state. He was selected by his school to represent them at Buckeye Boys State, and Tom was inducted into the first class of the Lakota High School Hall of Fame. At the University of Arizona and University of Nevada Las Vegas, Tom was a two-time All-American wrestler, Sophomore Athlete of the year, and a two-time winner of the 1980 Olympic wrestling trials. Tom graduated with a Bachelor’s degree in Speech Communications. After college, Tom became a state and national champion speech winner for Toastmasters and won two orchid awards as an actor. Tom is the proud father of three wonderful children and has been married for the past 32 years. You can contact Tom at 513 300-0341 or at [email protected].
About Debra Aaron
Debra Aaron’s professional life has centered around data. She started as a Certified Public Accountant and transitioned to working in Information Technology shortly before Y2K. Debra is passionate about technical training and the impact it can have on individual’s careers. She believes that most technical documentation is to too hard to understand. Her academic credentials include: BS in Business Administration from the University of North Carolina at Chapel Hill; MS in Accounting from the University of Virginia; Certificate in Information Systems from Virginia Commonwealth University Throughout her career she has pursued industry certifications that have grown to include: Teradata Basics Teradata Application Designer Teradata SQL Teradata Physical Implementation Teradata Professional Project Management Professional Microsoft SQL Server Design/Implementation Microsoft SQL Server Administration
Data Warehousing Using SQL Server Informatica Mapping Designer Windows NT Workstation Windows NT Server Windows NT Enterprise Networking Essentials Uniform Certified Public Accountants
Debra is an avid biker, skier, hiker, and kayaker. She splits her time between Colonial Heights, VA and Beech Mountain, NC where she resides with her wife of 25 years and their two dogs.
Table of Contents
Contents Chapter 1 – Introduction ............................................................................................................................................... 1 Introduction to the Family of SQL Server Products .................................................................................................. 2 Introduction to the Family Continued ........................................................................................................................ 3 Microsoft Azure SQL Data Warehouse ..................................................................................................................... 4 Nexus is Now Available on the Microsoft Azure Cloud ........................................................................................... 5 Symmetric Multi-Processing (SMP) .......................................................................................................................... 6 Naming of Objects ..................................................................................................................................................... 7 Chapter 2 – The Basics of SQL .................................................................................................................................... 9 Introduction .............................................................................................................................................................. 10 Setting Your Default Database ................................................................................................................................. 11 SELECT * (All Columns) in a Table ....................................................................................................................... 12 Fully Qualifying a Database, Schema and Table ..................................................................................................... 13 SELECT Specific Columns in a Table .................................................................................................................... 14 Commas in the Front or Back? ................................................................................................................................ 15 Place your Commas in front for better Debugging Capabilities .............................................................................. 16 Sort the Data with the ORDER BY Keyword ......................................................................................................... 17 ORDER BY Defaults to Ascending ......................................................................................................................... 18 Use the Name or the Number in your ORDER BY Statement ................................................................................ 19 Two Examples of ORDER BY using Different Techniques ................................................................................... 20 Changing the ORDER BY to Descending Order ..................................................................................................... 21 NULL Values sort First in Ascending Mode (Default) ........................................................................................... 22 NULL Values sort Last in Descending Mode (DESC ............................................................................................. 23 Major Sort vs. Minor Sorts ...................................................................................................................................... 24 Multiple Sort Keys using Names vs. Numbers ........................................................................................................ 25
Table of Contents Sorts are Alphabetical, NOT Logical ....................................................................................................................... 26 Using A CASE Statement to Sort Logically ............................................................................................................ 27 An Order By That Uses an Expression .................................................................................................................... 28 How to ALIAS a Column Name .............................................................................................................................. 29 Aliasing a Column Name with Spaces or Reserved Words..................................................................................... 30 A Missing Comma can by Mistake become an Alias .............................................................................................. 31 Comments using Double Dashes are Single Line Comments ................................................................................. 32 Comments for Multi-Lines....................................................................................................................................... 33 Comments for Multi-Lines as Double Dashes Per Line .......................................................................................... 34 A Great Technique for Comments to Look for SQL Errors .................................................................................... 35 sp_help at the Database Level .................................................................................................................................. 36 sp_help at the Object Level ...................................................................................................................................... 37 Getting System Information ..................................................................................................................................... 38 Getting Additional System Information ................................................................................................................... 39 Chapter 3 – The WHERE Clause................................................................................................................................ 41 The WHERE Clause limits Returning Rows ........................................................................................................... 42 Double Quoted Aliases are for Reserved Words and Spaces .................................................................................. 43 Using A Column ALIAS In A WHERE Clause ...................................................................................................... 44 Using A Column ALIAS In An ORDER BY Clause .............................................................................................. 45 In What Order Does SQL Server Process A Query? ............................................................................................... 46 Character Data needs Single Quotes in the WHERE Clause................................................................................... 47 Character Data needs Single Quotes, but Numbers Don’t....................................................................................... 48 Declaring a Variable ................................................................................................................................................ 49 Comparisons against a Null Value ........................................................................................................................... 50 NULL means UNKNOWN DATA so Equal (=) won’t Work ................................................................................ 51 Use IS NULL or IS NOT NULL when dealing with NULLs ................................................................................. 52 NULL is UNKNOWN DATA so NOT Equal won’t Work .................................................................................... 53 Use IS NULL or IS NOT NULL when dealing with NULLs ................................................................................. 54
Table of Contents Using Greater Than or Equal To (>=)...................................................................................................................... 55 AND in the WHERE Clause .................................................................................................................................... 56 Troubleshooting AND .............................................................................................................................................. 57 OR in the WHERE Clause ....................................................................................................................................... 58 Troubleshooting Or .................................................................................................................................................. 59 Troubleshooting Character Data .............................................................................................................................. 60 Using Different Columns in an AND Statement ..................................................................................................... 61 Quiz – How many rows will return? ........................................................................................................................ 62 Answer to Quiz – How many rows will return? ...................................................................................................... 63 What is the Order of Precedence? ............................................................................................................................ 64 Using Parentheses to change the Order of Precedence ............................................................................................ 65 Using an IN List in place of OR .............................................................................................................................. 66 The IN List is an Excellent Technique..................................................................................................................... 67 IN List vs. OR brings the same Results ................................................................................................................... 68 The IN List Can Use Character Data ....................................................................................................................... 69 Using a NOT IN List ................................................................................................................................................ 70 Null Values in a NOT IN List Bring Back No Rows .............................................................................................. 71 A Technique for Handling Nulls with a NOT IN List ............................................................................................. 72 BETWEEN is Inclusive ........................................................................................................................................... 73 NOT BETWEEN is Also Inclusive ......................................................................................................................... 74 LIKE command Underscore is Wildcard for one Character.................................................................................... 75 LIKE command using a Range of Values................................................................................................................ 76 LIKE command Using a NOT Range of Values ..................................................................................................... 77 LIKE Command Works Differently on Char Vs Varchar ....................................................................................... 78 Troubleshooting LIKE Command on Character Data ............................................................................................. 79 Introducing the RTRIM Command .......................................................................................................................... 80 Quiz – What Data is Left Justified and What is Right? ........................................................................................... 81 Numbers are Right Justified and Character Data is Left ......................................................................................... 82 Answer – What Data is Left Justified and What is Right? ...................................................................................... 83
Table of Contents An Example of Data with Left and Right Justification ........................................................................................... 84 A Visual of CHARACTER Data vs. VARCHAR Data .......................................................................................... 85 RTRIM command Removes Trailing spaces on CHAR Data ................................................................................. 86 Using Like with an AND Clause to Find Multiple Letters ...................................................................................... 87 Using Like with an OR Clause to Find Either Letters ............................................................................................. 88 Declaring a Variable and Using it with the LIKE Command .................................................................................. 89 Escape Character in the LIKE Command changes Wildcards ................................................................................ 90 Escape Characters Turn off Wildcards in the LIKE Command .............................................................................. 91 Quiz – Turn off that Wildcard.................................................................................................................................. 92 ANSWER – To Find that Wildcard ......................................................................................................................... 93 Chapter 4 – Distinct, Group By and TOP ................................................................................................................... 95 The Distinct Command ............................................................................................................................................ 96 Distinct vs. GROUP BY .......................................................................................................................................... 97 Quiz – How many rows come back from the Distinct? ........................................................................................... 98 Answer – How many rows come back from the Distinct? ...................................................................................... 99 TOP Command....................................................................................................................................................... 100 TOP Command is brilliant when ORDER BY is used! ......................................................................................... 101 TOP Command with PERCENT............................................................................................................................ 102 TOP Command with PERCENT............................................................................................................................ 103 The TOP Command WITH TIES .......................................................................................................................... 104 The TOP Command Using a Variable ................................................................................................................... 105 The TOP 1 Command for a Random Sample ........................................................................................................ 106 Chapter 5 – Aggregation ........................................................................................................................................... 108 Quiz – You calculate the Answer Set in your own Mind ...................................................................................... 109 Answer – You calculate the Answer Set in your own Mind ................................................................................. 110 The 3 Rules of Aggregation ................................................................................................................................... 111 There are Five Aggregates ..................................................................................................................................... 112
Table of Contents Quiz – How many rows come back? ..................................................................................................................... 113 Answer – How many rows come back? ................................................................................................................. 114 Troubleshooting Aggregates .................................................................................................................................. 115 GROUP BY when Aggregates and Normal Columns Mix ................................................................................... 116 GROUP BY delivers one row per Group .............................................................................................................. 117 Count_Big .............................................................................................................................................................. 118 Limiting Rows and Improving Performance with WHERE .................................................................................. 119 WHERE Clause in Aggregation limits unneeded Calculations ............................................................................. 120 Keyword HAVING tests Aggregates after they are Totaled ................................................................................. 121 CHECKSUM_AGG to Check If a Table Has Changed ........................................................................................ 122 Using Distinct Within the Aggregate ..................................................................................................................... 123 Group By Grouping Sets ........................................................................................................................................ 124 Group By Rollup .................................................................................................................................................... 125 Answer Set for Group By Rollup Query................................................................................................................ 126 Creating a Cube ...................................................................................................................................................... 127 Answer Set for Cube Query ................................................................................................................................... 128 An Easy Example of Creating a Cube ................................................................................................................... 129 Quiz - GROUP BY GROUPING SETS Challenge ............................................................................................... 130 Answer To Quiz - GROUP BY GROUPING SETS Challenge ............................................................................ 131 Getting the Average Values Per Column ............................................................................................................... 132 Average Values per Column for all Columns in a Table ....................................................................................... 133 Chapter 6 - Join Functions ........................................................................................................................................ 135 A Two-Table Join Using Traditional Syntax ......................................................................................................... 136 A two-table join using Non-ANSI Syntax with Table Alias ................................................................................. 137 You Can Fully Qualify All Columns ..................................................................................................................... 138 A two-table join using ANSI Syntax ..................................................................................................................... 139 Both Queries have the same Results and Performance.......................................................................................... 140 Quiz – Can You Finish the Join Syntax? ............................................................................................................... 141
Table of Contents Answer to Quiz – Can You Finish the Join Syntax? ............................................................................................. 142 Quiz – Can You Find the Error? ............................................................................................................................ 143 Answer to Quiz – Can You Find the Error? .......................................................................................................... 144 Super Quiz – Can You Find the Difficult Error? ................................................................................................... 145 Answer to Super Quiz – Can You Find the Difficult Error? ................................................................................. 146 Quiz – Which rows from both tables won’t Return? ............................................................................................. 147 Answer to Quiz – Which rows from both tables Won’t Return?........................................................................... 148 LEFT OUTER JOIN .............................................................................................................................................. 149 LEFT OUTER JOIN Results ................................................................................................................................. 150 RIGHT OUTER JOIN............................................................................................................................................ 151 RIGHT OUTER JOIN Example and Results......................................................................................................... 152 FULL OUTER JOIN .............................................................................................................................................. 153 FULL OUTER JOIN Results ................................................................................................................................. 154 Which Tables are the Left and which Tables are Right? ....................................................................................... 155 Answer - Which Tables are the Left and Which are the Right? ............................................................................ 156 INNER JOIN with Additional AND Clause .......................................................................................................... 157 ANSI INNER JOIN with Additional AND Clause ............................................................................................... 158 ANSI INNER JOIN with Additional WHERE Clause .......................................................................................... 159 OUTER JOIN with Additional WHERE Clause ................................................................................................... 160 OUTER JOIN with Additional AND Clause ......................................................................................................... 161 OUTER JOIN with Additional AND Clause Results ............................................................................................ 162 Quiz – Why is this considered an INNER JOIN? .................................................................................................. 163 Evaluation Order for Outer Queries ....................................................................................................................... 164 The DREADED Product Join ................................................................................................................................ 165 The DREADED Product Join Results ................................................................................................................... 166 The Horrifying Cartesian Product Join .................................................................................................................. 167 The ANSI Cartesian Join will ERROR .................................................................................................................. 168 Quiz – Do these Joins Return the Same Answer Set? ........................................................................................... 169 Answer – Do these Joins Return the Same Answer Set? ....................................................................................... 170
Table of Contents The CROSS JOIN .................................................................................................................................................. 171 The CROSS JOIN Answer Set............................................................................................................................... 172 The Self Join.......................................................................................................................................................... 173 The Self Join with ANSI Syntax ........................................................................................................................... 174 Quiz – Will both queries bring back the same Answer Set? ................................................................................. 175 Answer – Will both queries bring back the same Answer Set? ............................................................................. 176 Quiz – Will both queries bring back the same Answer Set? ................................................................................. 177 Answer – Will both queries bring back the same Answer Set? ............................................................................. 178 How would you Join these two tables? .................................................................................................................. 179 An Associative Table is a Bridge that Joins Two Tables ...................................................................................... 180 Quiz – Can you write the 3-Table Join? ................................................................................................................ 181 Answer to Quiz – Can you Write the 3-Table Join? .............................................................................................. 182 Quiz – Can you write the 3-Table Join to ANSI Syntax? ...................................................................................... 183 Answer – Can you Write the 3-Table Join to ANSI Syntax? ................................................................................ 184 Quiz – Can you Place the ON Clauses at the End?................................................................................................ 185 Answer – Can you Place the ON Clauses at the End? ........................................................................................... 186 The 5-Table Join – Logical Insurance Model ........................................................................................................ 187 Quiz - Write a Five Table Join Using ANSI Syntax .............................................................................................. 188 Answer - Write a Five Table Join Using ANSI Syntax ......................................................................................... 189 Quiz - Write a Five Table Join Using Non-ANSI Syntax ..................................................................................... 190 Answer - Write a Five Table Join Using Non-ANSI Syntax ................................................................................. 191 Quiz –Re-Write this putting the ON clauses at the END ...................................................................................... 192 Answer –Re-Write this putting the ON clauses at the END .................................................................................. 193 Chapter 7 – Date Function ....................................................................................................................................... 195 Current_Timestamp................................................................................................................................................ 196 Getdate ................................................................................................................................................................... 197 Date and Time Keywords....................................................................................................................................... 198 SYSDATETIMEOFFSET Provides the Timezone Offset .................................................................................... 199
Table of Contents SYSDATETIMEOFFSET Provides the Timezone Offset .................................................................................... 200 Using both CAST and CONVERT in Literal Values ............................................................................................ 201 Using Both CAST and CONVERT in Literal Values ........................................................................................... 202 Using both CAST and CONVERT in Literal Values ............................................................................................ 203 The DATEADD Function ...................................................................................................................................... 204 The DATEDIFF Function ...................................................................................................................................... 205 DATEADD Function ............................................................................................................................................. 206 A Real World Example for DateAdd Using the Order Table ................................................................................ 207 DATEPART Function ............................................................................................................................................ 208 DATEPART Function Examples ........................................................................................................................... 209 YEAR, MONTH, and DAY Functions .................................................................................................................. 210 A Better Technique for YEAR, MONTH, and DAY Functions ........................................................................... 211 DATENAME Function .......................................................................................................................................... 212 Date Formatting ..................................................................................................................................................... 213 Time Formatting..................................................................................................................................................... 214 ISDATE Function .................................................................................................................................................. 215 Chapter 8 - Temporary Tables .................................................................................................................................. 217 There are three types of Temporary Tables in TEMPDB ...................................................................................... 218 Tables in TEMPDB are not your only Temporary Storage ................................................................................... 219 What is TEMPDB? ................................................................................................................................................ 220 Creating a Private Temporary Table ...................................................................................................................... 221 You Populate a Private Temporary Table with an INSERT/SELECT .................................................................. 222 The Three Steps to Use a Private Temporary Table .............................................................................................. 223 Creating a Global Temporary Table ...................................................................................................................... 224 You Populate a Global Temporary Table with an INSERT/SELECT .................................................................. 225 The Three Steps to Use a Global Temporary Table .............................................................................................. 226 Creating a Direct Temporary Table ....................................................................................................................... 227 You Populate a Direct Temporary Table with an INSERT/SELECT ................................................................... 228
Table of Contents The Three Steps to Use a Direct Temporary Table ............................................................................................... 229 CREATING A Derived Table................................................................................................................................ 230 Naming the Derived Table ..................................................................................................................................... 231 Aliasing the Column Names in the Derived Table ................................................................................................ 232 Multiple Ways to Alias the Columns in a Derived Table ...................................................................................... 233 CREATING a Derived Table using the WITH Command .................................................................................... 234 The Same Derived Query shown Three Different Ways ....................................................................................... 235 MULTIPLE Derived Tables using the WITH Command ..................................................................................... 236 Column Alias Can Default For Normal Columns.................................................................................................. 237 Most Derived Tables Are Used To Join To Other Tables ..................................................................................... 238 A Join Example Showing Different Column Alias Styles ..................................................................................... 239 The Three Components of a Derived Table ........................................................................................................... 240 Visualize This Derived Table ................................................................................................................................ 241 Our Join Example With The WITH Syntax ........................................................................................................... 242 Quiz - Answer the Questions ................................................................................................................................. 243 Answer to Quiz - Answer the Questions................................................................................................................ 244 Clever Tricks on Aliasing Columns in a Derived Table ........................................................................................ 245 A Derived Table lives only for the lifetime of a single query ............................................................................... 246 An Example of Two Derived Tables in a Single Query ........................................................................................ 247 RECURSIVE Derived Table Hierarchy ................................................................................................................ 248 RECURSIVE Derived Table Query ...................................................................................................................... 249 RECURSIVE Derived Table Definition ................................................................................................................ 250 WITH RECURSIVE Derived Table Seeding ........................................................................................................ 251 WITH RECURSIVE Derived Table Looping ....................................................................................................... 252 RECURSIVE Derived Table Looping in Slow Motion......................................................................................... 253 RECURSIVE Derived Table Looping Continued ................................................................................................. 254 RECURSIVE Derived Table Looping Continued ................................................................................................. 255 RECURSIVE Derived Table Ends the Looping .................................................................................................... 256 RECURSIVE Derived Table Ends the Looping .................................................................................................... 256
Table of Contents RECURSIVE Derived Table Definition ................................................................................................................ 257 RECURSIVE Derived Table Answer Set .............................................................................................................. 258 Using a Table Variable........................................................................................................................................... 259 Chapter 9 – Sub-query Functions ............................................................................................................................. 261 An IN List is much like a Subquery ....................................................................................................................... 262 An IN List Never has Duplicates – Just like a Subquery....................................................................................... 263 An IN List Ignores Duplicates ............................................................................................................................... 264 The Subquery ......................................................................................................................................................... 265 The Three Steps of How a Basic Subquery Works................................................................................................ 266 These are Equivalent Queries ................................................................................................................................ 267 The Final Answer Set from the Subquery.............................................................................................................. 268 Quiz- Answer the Difficult Question ..................................................................................................................... 269 Answer to Quiz- Answer the Difficult Question ................................................................................................... 270 Should you use a Subquery of a Join? ................................................................................................................... 271 Quiz- Write the Subquery ...................................................................................................................................... 272 Answer to Quiz- Write the Subquery..................................................................................................................... 273 Quiz- Write the More Difficult Subquery .............................................................................................................. 274 Answer to Quiz- Write the More Difficult Subquery ............................................................................................ 275 Quiz – Write the Extreme Subquery ...................................................................................................................... 276 Answer to Quiz – Write the Extreme Subquery .................................................................................................... 277 Quiz- Write the Subquery with an Aggregate........................................................................................................ 278 Answer to Quiz- Write the Subquery with an Aggregate ...................................................................................... 279 Quiz- Write the Correlated Subquery .................................................................................................................... 280 Answer to Quiz- Write the Correlated Subquery ................................................................................................... 281 The Basics of a Correlated Subquery ..................................................................................................................... 282 The Top Query always runs first in a Correlated Subquery .................................................................................. 283 Correlated Subquery Example vs. a Join with a Derived Table ............................................................................ 284 Quiz- A Second Chance to Write a Correlated Subquery ..................................................................................... 285
Table of Contents Answer - A Second Chance to Write a Correlated Subquery ................................................................................ 286 Quiz- A Third Chance to Write a Correlated Subquery ........................................................................................ 287 Answer - A Third Chance to Write a Correlated Subquery ................................................................................... 288 Quiz- Last Chance To Write a Correlated Subquery ............................................................................................. 289 Answer – Last Chance to Write a Correlated Subquery ........................................................................................ 290 Quiz – Write the Extreme Correlated Subquery .................................................................................................... 291 Answer To Quiz – Write the Extreme Correlated Subquery ................................................................................. 292 Quiz- Write the NOT Subquery ............................................................................................................................. 293 Answer to Quiz- Write the NOT Subquery ........................................................................................................... 294 Quiz- Write the Subquery using a WHERE Clause............................................................................................... 295 Answer - Write the Subquery using a WHERE Clause ......................................................................................... 296 Quiz – Write the Triple Subquery .......................................................................................................................... 297 Answer to Quiz – Write the Triple Subquery ........................................................................................................ 298 Quiz – How many rows return on a NOT IN with a NULL? ................................................................................ 299 Answer – How many rows return on a NOT IN with a NULL? ........................................................................... 300 How to handle a NOT IN with Potential NULL Values........................................................................................ 301 Using a Correlated Exists ....................................................................................................................................... 302 How a Correlated Exists matches up ..................................................................................................................... 303 The Correlated NOT Exists.................................................................................................................................... 304 The Correlated NOT Exists Answer Set ................................................................................................................ 305 Quiz – How many rows come back from this NOT Exists? .................................................................................. 306 Answer – How many rows come back from this NOT Exists? ............................................................................. 307 Chapter 10 – Window Functions OLAP ................................................................................................................... 309 The Row_Number Command ................................................................................................................................ 310 Quiz – How did the Row_Number Reset? ............................................................................................................. 311 Quiz – How did the Row_Number Reset? ............................................................................................................. 312 Using a Derived Table and Row_Number ............................................................................................................. 313 Ordered Analytics OVER ...................................................................................................................................... 314
Table of Contents RANK and DENSE RANK ................................................................................................................................... 315 RANK Defaults to Ascending Order ..................................................................................................................... 316 Getting RANK to Sort in DESC Order .................................................................................................................. 317 RANK() OVER and PARTITION BY .................................................................................................................. 318 CSUM – Rows Unbounded Preceding Explained ................................................................................................. 319 CSUM – Making Sense of the Data ....................................................................................................................... 320 CSUM – Making Even More Sense of the Data .................................................................................................... 321 CSUM – The Major and Minor Sort Key(s) .......................................................................................................... 322 The ANSI CSUM – Getting a Sequential Number ................................................................................................ 323 Troubleshooting The ANSI OLAP on a GROUP BY ........................................................................................... 324 Reset with a PARTITION BY Statement .............................................................................................................. 325 PARTITION BY only Resets a Single OLAP not ALL of them........................................................................... 326 CURRENT ROW AND UNBOUNDED FOLLOWING ...................................................................................... 327 Different Windowing Options ............................................................................................................................... 328 Moving Sum has a Moving Window ..................................................................................................................... 329 How ANSI Moving SUM Handles the Sort .......................................................................................................... 330 Quiz – How is that Total Calculated? .................................................................................................................... 331 Answer to Quiz – How is that Total Calculated? .................................................................................................. 332 Moving SUM every 3-rows Vs a Continuous Average ......................................................................................... 333 Partition By Resets an ANSI OLAP ...................................................................................................................... 334 The Moving Window is Current Row and Preceding ............................................................................................ 335 How Moving Average Handles the Sort ................................................................................................................ 336 Moving Average..................................................................................................................................................... 337 Moving Average..................................................................................................................................................... 338 Quiz – How is that Total Calculated? .................................................................................................................... 339 Answer to Quiz – How is that Total Calculated? .................................................................................................. 340 Quiz – How is that 4th Row Calculated? ................................................................................................................ 341 Answer to Quiz – How is that 4th Row Calculated? .............................................................................................. 342 Moving Average every 3-rows Vs a Continuous Average .................................................................................... 343
Table of Contents Partition By Resets an ANSI OLAP ...................................................................................................................... 344 Moving Difference using ANSI Syntax ................................................................................................................. 345 Moving Difference using ANSI Syntax with Partition By .................................................................................... 346 COUNT OVER for a Sequential Number ............................................................................................................. 347 COUNT OVER without Rows Unbounded Preceding .......................................................................................... 348 Quiz – What caused the COUNT OVER to Reset? ............................................................................................... 349 Answer to Quiz – What caused the COUNT OVER to Reset? ............................................................................. 350 The MAX OVER Command.................................................................................................................................. 351 MAX OVER with PARTITION BY Reset ........................................................................................................... 352 MAX OVER Without Rows Unbounded Preceding ............................................................................................. 353 The MIN OVER Command ................................................................................................................................... 354 Troubleshooting MIN OVER................................................................................................................................. 355 Finding a Value of a Column in the Next Row with MIN .................................................................................... 356 The CSUM For Each Product_Id and the Next Start Date .................................................................................... 357 Quiz – Fill in the Blank .......................................................................................................................................... 358 Answer – Fill in the Blank ..................................................................................................................................... 359 How Ntile Works ................................................................................................................................................... 360 Ntile ........................................................................................................................................................................ 361 Ntile Continued ...................................................................................................................................................... 362 Ntile Percentile ....................................................................................................................................................... 363 Another Ntile Example .......................................................................................................................................... 364 Using Tertiles (Partitions of Four) ......................................................................................................................... 365 NTILE .................................................................................................................................................................... 366 NTILE Using a Value of 10 ................................................................................................................................... 367 NTILE With a Partition.......................................................................................................................................... 368 Using FIRST_VALUE ........................................................................................................................................... 369 FIRST_VALUE ..................................................................................................................................................... 370 FIRST_VALUE After Sorting by the Highest Value ............................................................................................ 371 FIRST_VALUE with Partitioning ......................................................................................................................... 372
Table of Contents Using LAST_VALUE ............................................................................................................................................ 373 LAST_VALUE ...................................................................................................................................................... 374 Using LAG and LEAD........................................................................................................................................... 375 Using LEAD........................................................................................................................................................... 376 Using LEAD With and Offset of 2 ........................................................................................................................ 377 LEAD ..................................................................................................................................................................... 378 LEAD With Partitioning ........................................................................................................................................ 379 Using LAG ............................................................................................................................................................. 380 Using LAG With an Offset of 2 ............................................................................................................................. 381 LAG ........................................................................................................................................................................ 382 LAG with Partitioning............................................................................................................................................ 383 CUME_DIST ......................................................................................................................................................... 384 CUME_DIST With a Partition ............................................................................................................................... 385 SUM(SUM(n)) ....................................................................................................................................................... 386 Chapter 11 - Working with Strings ........................................................................................................................... 388 The ASCII Function ............................................................................................................................................... 389 The CHAR Function .............................................................................................................................................. 390 The UNICODE Function ....................................................................................................................................... 391 The NCHAR Function ........................................................................................................................................... 392 The LEN Function.................................................................................................................................................. 393 The DATALENGTH Function ............................................................................................................................... 394 Concatenation ......................................................................................................................................................... 395 The RTRIM and LTRIM Command trims Spaces ................................................................................................ 396 The SUBSTRING Command................................................................................................................................. 397 Using SUBSTRING to move Backwards .............................................................................................................. 398 How SUBSTRING Works with a Starting Position of -1 ..................................................................................... 399 How SUBSTRING Works with an Ending Position of 0 ...................................................................................... 400 Concatenation and SUBSTRING........................................................................................................................... 401
Table of Contents SUBSTRING and Different Aliasing .................................................................................................................... 402 The LEFT and RIGHT Functions .......................................................................................................................... 403 Four Concatenations Together ............................................................................................................................... 404 The DATALENGTH Function and RTRIM.......................................................................................................... 405 A Visual of the TRIM Command Using Concatenation ........................................................................................ 406 CHARINDEX Function Finds a Letter(s) Position in a String ............................................................................. 407 The CHARINDEX Command is brilliant with SUBSTRING .............................................................................. 408 The CHARINDEX Command Using a Literal ...................................................................................................... 409 PATINDEX Function............................................................................................................................................. 410 PATINDEX Function to Find a Character Pattern ................................................................................................ 411 SOUNDEX Function to Find a Sound ................................................................................................................... 412 DIFFERENCE Function to Quantile a Sound ....................................................................................................... 413 The REPLACE Function ....................................................................................................................................... 414 LEN and REPLACE Functions for Number of Occurrences ................................................................................ 415 REPLICATE Function ........................................................................................................................................... 416 STUFF Function..................................................................................................................................................... 417 STUFF without Deleting Function ........................................................................................................................ 418 UPPER and lower Functions.................................................................................................................................. 419 Chapter 12 - Interrogating the Data ......................................................................................................................... 421 Quiz – What would the Answer be? ...................................................................................................................... 422 Answer to Quiz – What would the Answer be? ..................................................................................................... 423 The NULLIF Command ......................................................................................................................................... 424 Quiz – Fill in the Answers for the NULLIF Command ......................................................................................... 425 Answer– Fill in the Answers for the NULLIF Command ..................................................................................... 426 The COALESCE Command – Fill In the Answers ............................................................................................... 427 The COALESCE Answer Set ................................................................................................................................ 428 COALESCE is Equivalent to This CASE Statement ............................................................................................ 429 The Basics of CAST (Convert and Store).............................................................................................................. 430
Table of Contents Some Great CAST (Convert and Store) Examples ................................................................................................ 431 Some Great CAST (Convert and Store) Examples ................................................................................................ 432 A Rounding Example ............................................................................................................................................. 433 Quiz - CAST Examples .......................................................................................................................................... 434 Answer to Quiz - CAST Examples ........................................................................................................................ 435 Quiz - The Basics of the CASE Statements ........................................................................................................... 436 Answer to Quiz - The Basics of the CASE Statements ......................................................................................... 437 Using an ELSE in the Case Statement ................................................................................................................... 438 Using an ELSE as a Safety Net .............................................................................................................................. 439 Rules For a Valued Case Statement ....................................................................................................................... 440 Rules for a Searched Case Statement ..................................................................................................................... 441 Valued Case Vs. A Searched Case.......................................................................................................................... 442 Quiz - Valued Case Statement ............................................................................................................................... 443 Answer - Valued Case Statement........................................................................................................................... 444 Quiz - Searched Case Statement ............................................................................................................................ 445 Answer - Searched Case Statement ....................................................................................................................... 446 Quiz - When NO ELSE is present in CASE Statement ......................................................................................... 447 Answer - When NO ELSE is present in CASE Statement .................................................................................... 448 Quiz -When an Alias is NOT used in a CASE Statement ..................................................................................... 449 Answer -When an Alias is NOT used in a CASE Statement................................................................................. 450 Combining Searched Case and Valued Case ......................................................................................................... 451 A Trick for getting a Horizontal Case .................................................................................................................... 452 Nested Case ............................................................................................................................................................ 453 Put a CASE in the ORDER BY ............................................................................................................................. 454 Chapter 13 – Table Create and Data Types ............................................................................................................. 456 Creating a Table that is a Heap .............................................................................................................................. 457 Heap Page ............................................................................................................................................................... 458 Extents .................................................................................................................................................................... 459
Table of Contents Creating a Table That Has a Clustered Index ........................................................................................................ 460 Clustered Index Page.............................................................................................................................................. 461 When Do I Create a Clustered Index? ................................................................................................................... 462 B-Trees ................................................................................................................................................................... 463 The Building of a B-Tree For a Clustered Index (1 of 3) ...................................................................................... 464 The Building of a B-Tree For a Clustered Index (2 of 3) ...................................................................................... 465 The Building of a B-Tree For a Clustered Index (3 of 3) ...................................................................................... 466 The Row Offset Array is the Guidance System For Every Row ........................................................................... 467 The Row Offset Array Provides Two Search Options (1 of 2) ............................................................................. 468 The Row Offset Array Provides Two Search Options (2 of 2) ............................................................................. 469 The Row Offset Array Helps With Inserts ............................................................................................................ 470 Adding an Index ..................................................................................................................................................... 471 When Do I Create a Non Clustered Index? ........................................................................................................... 472 B-Tree For Non Clustered Index on a Clustered Table (1 of 2) ............................................................................ 473 B-Tree For Non Clustered Index on a Clustered Table (2 of 2) ............................................................................ 474 Adding A Non Clustered Index To A Heap........................................................................................................... 475 B-Tree For Non Clustered Index on a Heap Table (1 of 2) ................................................................................... 476 B-Tree for a Non Clustered Index on a Heap Table (2 of 2) ................................................................................. 477 SELECT INTO....................................................................................................................................................... 478 SELECT INTO....................................................................................................................................................... 479 A Primary Key Constraint...................................................................................................................................... 480 The Difference between a Primary Key vs. Unique Constraint ............................................................................ 481 Primary Key Foreign Key Constraints ................................................................................................................... 482 More Information about Foreign Key Constraints................................................................................................. 483 Check Constraint .................................................................................................................................................... 484 Default Values ........................................................................................................................................................ 485 Identity Columns .................................................................................................................................................... 486 Computed Columns ................................................................................................................................................ 487 Compression ........................................................................................................................................................... 488
Table of Contents ROWVERSION ..................................................................................................................................................... 489 Chapter 14 – View Functions ................................................................................................................................... 491 The Fundamentals of Views .................................................................................................................................. 492 Creating a Simple View to Restrict Sensitive Columns ........................................................................................ 493 Creating a Simple View to Restrict Rows ............................................................................................................. 494 Basic Rules for Views ............................................................................................................................................ 495 How to Modify a View .......................................................................................................................................... 496 Why Bother To ALTER A View ........................................................................................................................... 497 Two Exceptions to the ORDER BY Rule inside a View ....................................................................................... 498 How to Get HELP with a View ............................................................................................................................. 499 How to Get HELP with a View ............................................................................................................................ 500 Views sometimes CREATED for Formatting or Row Security ............................................................................ 501 Creating a View to Join Tables Together............................................................................................................... 502 You Select From a View ........................................................................................................................................ 503 Another Way to Alias Columns in a View CREATE ............................................................................................ 504 The Standard Way Most Aliasing is done ............................................................................................................. 505 What Happens When Both Aliasing Options Are Present .................................................................................... 506 Resolving Aliasing Problems in a View CREATE ............................................................................................... 507 Answer to Resolving Aliasing Problems in a View CREATE .............................................................................. 508 Aggregates on View Aggregates............................................................................................................................ 509 Altering A Table .................................................................................................................................................... 510 Altering a Table after a View has been Created .................................................................................................... 511 A View that Errors After An ALTER .................................................................................................................... 512 Troubleshooting a View ......................................................................................................................................... 513 Updating Data in a Table through a View ............................................................................................................. 514 Loading Data through a View ................................................................................................................................ 515 Maintenance Restrictions on a Table through a View ........................................................................................... 516
Table of Contents Chapter 15 – Data Manipulation Language (DML) ................................................................................................. 518 INSERT Syntax # 1 ................................................................................................................................................ 519 INSERT Example with Syntax 1 ........................................................................................................................... 520 INSERT Syntax #2 ................................................................................................................................................. 521 INSERT Example with Syntax 2 ........................................................................................................................... 522 INSERT Example with Syntax 3 ........................................................................................................................... 523 INSERT/SELECT Command ................................................................................................................................ 524 INSERT/SELECT Example using All Columns (*) .............................................................................................. 525 INSERT/SELECT Example with Less Columns ................................................................................................... 526 The UPDATE Command Basic Syntax ................................................................................................................. 527 Two UPDATE Examples ....................................................................................................................................... 528 Subquery UPDATE Command Syntax .................................................................................................................. 529 Example of Subquery UPDATE Command .......................................................................................................... 530 Join UPDATE Command Syntax .......................................................................................................................... 531 Example of an UPDATE Join Command .............................................................................................................. 532 Fast UPDATE ........................................................................................................................................................ 533 The DELETE Command Basic Syntax .................................................................................................................. 534 Two DELETE Examples to DELETE ALL Rows in a Table ............................................................................... 535 To DELETE or to TRUNCATE ............................................................................................................................ 536 TRUNCATE is Different from DELETE .............................................................................................................. 537 A DELETE Example Deleting only Some of the Rows ........................................................................................ 538 Want to know How Many Rows were just changed .............................................................................................. 539 Subquery and Join DELETE Command Syntax .................................................................................................... 540 Example of Subquery DELETE Command ........................................................................................................... 541 Example of Join DELETE Command .................................................................................................................... 542 MERGE INTO ....................................................................................................................................................... 543 MERGE INTO ....................................................................................................................................................... 544 MERGE INTO ....................................................................................................................................................... 545 MERGE INTO ....................................................................................................................................................... 546
Table of Contents MERGE INTO ....................................................................................................................................................... 547 MERGE INTO Example that Matches .................................................................................................................. 548 MERGE INTO Example that does NOT Match .................................................................................................... 549 User Defined Functions (UDFs) ............................................................................................................................ 550 User Defined Functions (UDFs) ............................................................................................................................ 551 User Defined Function Example ............................................................................................................................ 552 Replace ................................................................................................................................................................... 553 Replace ................................................................................................................................................................... 554 User Defined Types (UDTs) .................................................................................................................................. 555 Chapter 16 – Set Operators Functions ...................................................................................................................... 557 Rules of Set Operators ........................................................................................................................................... 558 INTERSECT Explained Logically......................................................................................................................... 559 INTERSECT Explained Logically......................................................................................................................... 560 UNION Explained Logically ................................................................................................................................. 561 UNION Explained Logically ................................................................................................................................. 562 UNION ALL Explained Logically ........................................................................................................................ 563 UNION ALL Explained Logically ........................................................................................................................ 564 EXCEPT Explained Logically ............................................................................................................................... 565 EXCEPT Explained Logically ............................................................................................................................... 566 Another EXCEPT Example ................................................................................................................................... 567 EXCEPT Explained Logically in Reverse Order................................................................................................... 568 An Equal Amount of Columns in both SELECT List ........................................................................................... 569 Columns in the SELECT list should be from the same Domain ........................................................................... 570 The Top Query handles all Aliases ........................................................................................................................ 571 The Bottom Query does the ORDER BY .............................................................................................................. 572 Great Trick: Place your Set Operator in a Derived Table..................................................................................... 573 UNION Vs UNION ALL ....................................................................................................................................... 574 Using UNION ALL and Literals ........................................................................................................................... 575
Table of Contents A Great Example of how EXCEPT works ............................................................................................................ 576 USING Multiple SET Operators in a Single Request............................................................................................ 577 Changing the Order of Precedence with Parentheses ............................................................................................ 578 Building Grouping Sets Using UNION ................................................................................................................. 579 Three Grouping Sets Using a UNION ................................................................................................................... 580 Chapter 17 – Stored Procedures ................................................................................................................................ 582 Creating a Stored Procedure .................................................................................................................................. 583 Executing a Stored Procedure ................................................................................................................................ 584 There are Three Ways to Execute a Stored Procedure .......................................................................................... 585 Dropping a Stored Procedure ................................................................................................................................. 586 Passing an Input Parameter to a Stored Procedure ................................................................................................ 587 Executing With Positional Parameters vs. Named Parameters.............................................................................. 588 Passing an Output Parameter to a Stored Procedure .............................................................................................. 589 Changing a Stored Procedure with an ALTER ...................................................................................................... 590 Answer Set for the Altered Stored Procedure ........................................................................................................ 591 Displaying a Stored Procedure’s Definition .......................................................................................................... 592 Encrypting a Stored Procedure’s Definition .......................................................................................................... 593 Using a Stored Procedure to Delete a Row ............................................................................................................ 594 A Different Method to Delete a Row ..................................................................................................................... 595 Deleting a Row Using an Input Parameter ............................................................................................................ 596 Using Loops in Stored Procedures ......................................................................................................................... 597 Stored Procedure Workshop .................................................................................................................................. 598 Looping with a WHILE Statement ........................................................................................................................ 599 Chapter 18 – Statistical Aggregate Functions........................................................................................................... 601 The Stats Table ....................................................................................................................................................... 602 Above is the Stats_Table data in which we will use in our statistical examples. .................................................. 602 The VAR and VARP Functions ............................................................................................................................. 603
Table of Contents A VAR Example .................................................................................................................................................... 604 A VARP Example .................................................................................................................................................. 605 The STDEV and STDEVP Functions .................................................................................................................... 606 A STDEV Example ................................................................................................................................................ 607 A STDEVP Example.............................................................................................................................................. 608 Chapter 19 – Nexus ................................................................................................................................................... 610 Nexus is Now Available on the Microsoft Azure Cloud ....................................................................................... 611 Nexus Queries Every Major System ...................................................................................................................... 612 Setup of Nexus is as easy as pie ............................................................................................................................. 613 Setup of Nexus is a Easy as 1, 2, 3 ........................................................................................................................ 614 Nexus Data Visualization ....................................................................................................................................... 615 Nexus Data Visualization ....................................................................................................................................... 616 Nexus Data Visualization Shows What Tables Can Be Joined ............................................................................. 617 Nexus is doing a Five-Table Join ........................................................................................................................... 618 Nexus Generates the SQL Automatically .............................................................................................................. 619 Nexus Delivers the Report ..................................................................................................................................... 620 Cross-System Joins from Teradata, Oracle and SQL Server ................................................................................. 621 The Tab of the Super Join Builder ......................................................................................................................... 622 The 9 Tabs of the Super Join Builder – Objects Tab 1 .......................................................................................... 623 Selecting Columns in the Objects Tab ................................................................................................................... 624 The 9 Tabs of the Super Join Builder – Columns Tab 2........................................................................................ 625 Removing Columns from the Report in the Columns Tab .................................................................................... 626 The 9 Tabs of the Super Join Builder – Sorting Tab 3 .......................................................................................... 627 The 9 Tabs of the Super Join Builder – Joins Tab 4 .............................................................................................. 628 The 9 Tabs of the Super Join Builder – Where Tab 5 ........................................................................................... 629 Using the WHERE Tab For Additional WHERE or AND .................................................................................... 630 The 9 Tabs of the Super Join Builder – SQL Tab 6............................................................................................... 631 The 9 Tabs of the Super Join Builder – Answer Set Tab 7 ................................................................................... 632
Table of Contents The 9 Tabs of the Super Join Builder – Analytics Tab 9 ....................................................................................... 633 Analytics Tab ......................................................................................................................................................... 634 Analytics Tab – OLAP Example ........................................................................................................................... 635 Analytics Tab – OLAP Example of SQL Generated ............................................................................................. 636 Analytics Tab – Grouping Sets Example ............................................................................................................... 637 Analytics Tab – Grouping Sets Answer Set .......................................................................................................... 638 Nexus Data Movement ........................................................................................................................................... 639 Moving a Single Table To a Different System ...................................................................................................... 640 The Single Table Data Movement Screen ............................................................................................................. 641 Moving an Entire Database To a Different System ............................................................................................... 642 The Database Mover Screen .................................................................................................................................. 643 The Database Mover Options Tab ......................................................................................................................... 644 Converting DDL Table Structures ......................................................................................................................... 645 Converting DDL Table Structures ......................................................................................................................... 646 Converting DDL Table Structures ......................................................................................................................... 647 Hound Dog Compression ....................................................................................................................................... 648 Hound Dog Compression on Teradata ................................................................................................................... 649 Hound Dog Compression on Teradata ................................................................................................................... 650
Chapter 1
Introduction
Chapter 1
Introduction
Chapter 1 – Introduction
“Fall seven times – Stand up eight.” – Japanese proverb
Page 1
Chapter 1
Introduction
Introduction to the Family of SQL Server Products Microsoft SQL Server Compact 4.0 Microsoft SQL Server Compact 4.0 is a compact database that is embedded inside Nexus and other desktops around the world. It is ideal for also embedding in web applications. SQL Server Compact 4.0 provides developers a common programming model with other SQL Server editions. This is important for developing both native and managed applications. SQL Server Compact provides outstanding flexibility, but in a small footprint.
SQL Server 2014 Express Edition Microsoft provides this for free! This powerful database engine is perfect for embedded applications or for redistribution with other solutions. Independent software vendors (ISV's) use it to build desktop applications. If you need support for greater than 10 GB databases, SQL Server Express is compatible with other editions of SQL Server.
SQL Server Standard Edition Microsoft's robust data management and business intelligence database is ideal for departments and small workgroups. It supports common development tools for both on premise and cloud applications. This edition enables effective database management with minimal IT resources and it is compatible with other editions.
Above are the first three offerings from Microsoft on SQL Server. Page 2
Chapter 1
Introduction
Introduction to the Family Continued Microsoft SQL Server Web Edition Microsoft's Web edition is a low total-cost-of-ownership option to host Web applications that provides scalability, affordability, and manageability capabilities for small to large scale Web initiatives.
SQL Server 2014 Business Intelligence Edition Microsoft's Business Intelligence edition is for the BI intelligence community and delivers a comprehensive platform. This empowers organizations to build and deploy secure, scalable and manageable BI solutions. It has browser based data exploration and visualization, plus includes powerful integration capabilities.
SQL Server 2014 Enterprise Edition Microsoft's SQL Server 2014 Enterprise edition delivers high-end datacenter capabilities with performance that has been enhanced for virtualization, business intelligence and integration capabilities. This enables high service levels for mission-critical workloads and end user access to data insights.
Above are the next three offerings from Microsoft on SQL Server. Page 3
Chapter 1
Introduction
Microsoft Azure SQL Data Warehouse Azure SQL Data Warehouse Microsoft's Azure SQL Data Warehouse is a massively parallel processing (MPP) data warehousing appliance built for any volume of relational data and provides integration to Hadoop. Azure SQL Data Warehouse can provide up to 100x performance gains over other SQL Server platforms. This is the MPP platform that provides linear scalability for when data volumes grow and the number of users increases.
Azure SQL Data Warehouse is designed to parallelize and distribute the processing across multiple Symmetric Multi-Processing (SMP ) compute nodes. Azure SQL Data Warehouse is only available as part of Microsoft’s Analytics Platform System (APS) appliance. Azure SQL Data Warehouse is a shared-nothing architecture, which means each processor has its own operating system, memory and set of disks. Nothing is shared! Data is “horizontally partitioned” across nodes. This means that each node has a subset of the rows from each table in the database. Each node is then responsible for processing only the rows on its own disks.
Above is the information about Microsoft's Azure SQL Data Warehouse, which is Microsoft's MPP system for cloud computing and available on the Azure cloud. Page 4
Chapter 1
Introduction
Nexus is Now Available on the Microsoft Azure Cloud
Why the Nexus Chameleon should be your query tool of choice: 1) Queries every major system 2) Provides visualization and automatically writes the SQL 3) Can perform cross-system joins with a few clicks of the mouse 4) Converts table structures and moves the table and data between systems 5) Compares and synchronizes databases 6) Can move an entire database of tables or views between systems 7) Has the "Garden of Analysis" to re-query answer sets inside your PC 8) Provides a dashboard of graphs and charts for answer sets Download the Nexus for a free trial at www.CoffingDW.com and use Nexus in-house or on the Azure cloud. Page 5
Chapter 1
Introduction
Symmetric Multi-Processing (SMP) CPU
CPU
CPU
CPU
Cache
Cache
Cache
Cache
Bus
Shared Memory Disk I/O
A Symmetric Multi-Processing system has multiple processors for extra power, but these processors share a single operating system, memory pool and they share access to the disks. This is a great architecture for speed, similar to a restaurant that is quick and organized, but it lacks the ability for unlimited expansion. When there are too many cooks in the kitchen you need an MPP system that scales many SMP systems together as one parallel processing data warehouse.
A Symmetric Multi-Processing (SMP) system is what Microsoft is known for in their SQL Server suite of products.
Page 6
Chapter 1
Introduction
Naming of Objects Object names can include the following characters: • • •
Letters from a-z, A-Z, and letter characters from other languages. Numeric characters 0-9. Special characters _, @, $, #.
Object names must also meet the following requirements: • • •
Object names must not be reserved words. Embedded spaces are not allowed in object names. Unicode supplementary characters are not allowed in object names.
More facts about objects: Database, table, and view names must be 1-128 characters long and begin with a letter character. They are case insensitive. Database names are unique per system. Table and view names must be unique per database. Index, column, and statistics names must be 1-32 characters long and begin with a letter character. They are case insensitive. Index names are unique per database. Column and statistics names must be unique per table.
Above are the key facts you need to understand SQL Server tables. Page 7
Chapter 2
Page 8
The Basics of SQL
Chapter 2
The Basics of SQL
Chapter 2 – The Basics of SQL
“As I would not be a slave, so I would not be a master.” - Abraham Lincoln
Page 9
Chapter 2
The Basics of SQL
Introduction Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
The Student_Table above will be used in our early SQL Examples
“The man who has no imagination has no wings.”
– Muhammad Ali
This is a pictorial of the Student_Table which we will use to present some basic examples of SQL and get some hands-on experience with querying this table. This book attempts to show you the table, show you the query, and show you the result set. This book is going to be so fun you will fly like a butterfly!
Page 10
Chapter 2
The Basics of SQL
Setting Your Default Database Use SQL_Class ;
We have set our default database to be SQL_Class
SELECT * FROM Student_Table ;
SELECT * FROM SQL_Class.Student_Table ;
The database is assumed to be SQL_Class
We have specified our database to be SQL_Class.
SQL Server allows you do set your default database. Above, we have set our default database to SQL_Class. If we run a query without specifying the database, then SQL Server will assume the database is SQL_Class.
Page 11
Chapter 2
The Basics of SQL
SELECT * (All Columns) in a Table SELECT * FROM Student_Table ;
Student_ID Last_Name ________ ________ 423400 125634 280023 260000 231222 234121 324652 123250 322133 333450
Larkins Hanson McRoberts Johnson Wilson Thomas Delaney Phillips Bond Smith
First_Name ________ Michael Henry Richard Stanley Susie Wendy Danny Martin Jimmy Andy
An asterisk (*) means you want to see ALL columns in the table on your report
Class_Code Grade_Pt _________ _______ FR FR JR ? SO FR SR SR JR SO
0.00 2.88 1.90 ? 3.80 4.00 3.35 3.00 3.95 2.00
Mostly every SQL statement will consist of a SELECT and a FROM. You SELECT the columns you want to see on your report and an Asterisk (*) means you want to see all columns in the table on the returning answer set!
Page 12
Chapter 2
The Basics of SQL
Fully Qualifying a Database, Schema and Table Database
Schema
TableName
CREATE TABLE SQL_Class.dbo.DEPT (DEPT_NO SMALLINT, DEPARTMENT_NAME CHARACTER(30), MGR_NO INT, BUDGET Decimal (10,2) );
SELECT * FROM DEPT; SELECT * FROM Dept;
We just created a table called Dept inside the SQL_Class Database in the dbo schema.
If you are in the Sales Database, then these statements are both valid because SQL Server and Azure SQL Data Warehouse are NOT case sensitive.
SELECT * FROM SQL_Class.dbo.DEPT ; SELECT * FROM SQL_Class..Dept ;
This is fully qualified
The .. represents the schema (owner)
To refer to objects in other databases on the SQL Server or Azure SQL Data Warehouse system, you must use three-level naming, which consists of the database, the schema (which is the name of the database owner), and the object (table or view etc.). The last example (SQL_Class..Dept) is a convenient way of specifying a fully qualified object name. The system supplies the schema name by internally inserting the current schema name.
Page 13
Chapter 2
The Basics of SQL
SELECT Specific Columns in a Table SELECT First_Name ,Last_Name ,Class_Code ,Grade_Pt FROM Student_Table ;
First_Name _________ Last_Name _________ Class_Code ________ Grade_Pt _________ Michael Henry Richard Stanley Susie Wendy Danny Martin Jimmy Andy
Larkins Hanson McRoberts Johnson Wilson Thomas Delaney Phillips Bond Smith
FR FR JR ? SO FR SR SR JR SO
This is a great way to show the columns you are selecting from the Table_Name.
Page 14
0.00 2.88 1.90 ? 3.80 4.00 3.35 3.00 3.95 2.00
Chapter 2
The Basics of SQL
Commas in the Front or Back? SELECT First_Name ,Last_Name 1 ,Class_Code ,Grade_Pt FROM Student_Table ;
SELECT First_Name, Last_Name, 2 Class_Code, Grade_Pt FROM Student_Table ;
First_Name _________ Last_Name _________ Class_Code ________ Grade_Pt _________ Michael Henry Richard Stanley Susie Wendy Danny Martin Jimmy Andy
Larkins Hanson McRoberts Johnson Wilson Thomas Delaney Phillips Bond Smith
FR FR JR ? SO FR SR SR JR SO
0.00 2.88 1.90 ? 3.80 4.00 3.35 3.00 3.95 2.00
Why is the example on the left better even though they are functionally equivalent? Errors are easier to spot and comments won't cause errors. Page 15
Chapter 2
The Basics of SQL
Place your Commas in front for better Debugging Capabilities
SELECT First_Name, Last_Name, Class_Code, Grade_Pt, FROM Student_Table ;
Sometimes if you Add or Remove a COLUMN you can overlook an ending Comma!
SELECT
First_Name ,Last_Name ,Class_Code ,Grade_Pt
FROM Student_Table ;
Error!
Successful
"A life filled with love may have some thorns, but a life empty of love will have no roses."
Anonymous
Having commas in front to separate column names makes it easier to debug. Remember our quote above. "A query filled with commas at the end just might fill you with thorns, but a query filled with commas in the front will allow you to always come up smelling like roses."
Page 16
Chapter 2
The Basics of SQL
Sort the Data with the ORDER BY Keyword Sorts the Answer Set in Ascending order by default
SELECT * FROM Student_Table ORDER BY Last_Name ;
Student_ID _________ Last_Name First_Name Class_Code Grade_Pt _________ ________ _________ _______ 322133 324652 125634 260000 423400 280023 123250 333450 234121 231222
Bond Delaney Hanson Johnson Larkins McRoberts Phillips Smith Thomas Wilson
Jimmy Danny Henry Stanley Michael Richard Martin Andy Wendy Susie
JR SR FR ? FR JR SR SO FR SO
3.95 3.35 2.88 ? 0.00 1.90 3.00 2.00 4.00 3.80
Rows typically come back to the report in random order. To order the result set, you must use an ORDER BY. When you order by a column, it will order in ASCENDING order. This is called the Major Sort!
Page 17
Chapter 2
The Basics of SQL
ORDER BY Defaults to Ascending Sorts the Answer Set In Ascending Order By Last_Name
SELECT * FROM Student_Table ORDER BY Last_Name ;
Student_ID _________ Last_Name First_Name Class_Code Grade_Pt _________ ________ _________ _______ 322133 324652 125634 260000 423400 280023 123250 333450 234121 231222
Bond Delaney Hanson Johnson Larkins McRoberts Phillips Smith Thomas Wilson
Jimmy Danny Henry Stanley Michael Richard Martin Andy Wendy Susie
JR SR FR ? FR JR SR SO FR SO
3.95 3.35 2.88 ? 0.00 1.90 3.00 2.00 4.00 3.80
Rows typically come back to the report in random order, but we decided to use the ORDER BY statement. Now, the data comes back ordered by Last_Name.
Page 18
Chapter 2
The Basics of SQL
Use the Name or the Number in your ORDER BY Statement SELECT * FROM Student_Table ORDER BY 2 ;
Sorts the Answer Set by Column 2 which is Last_Name
Sort by the 2nd column coming back on the report
Student_ID _________ Last_Name First_Name Class_Code Grade_Pt _________ ________ _________ _______ 322133 324652 125634 260000 423400 280023 123250 333450 234121 231222
Bond Delaney Hanson Johnson Larkins McRoberts Phillips Smith Thomas Wilson
Jimmy Danny Henry Stanley Michael Richard Martin Andy Wendy Susie
JR SR FR ? FR JR SR SO FR SO
3.95 3.35 2.88 ? 0.00 1.90 3.00 2.00 4.00 3.80
The ORDER BY can use a number to represent the sort column. The number 2 represents the second column on the report. Page 19
Chapter 2
The Basics of SQL
Two Examples of ORDER BY using Different Techniques SELECT * FROM Student_Table ORDER BY 5 ;
Student_ID _________ 260000 423400 280023 333450 125634 123250 324652 231222 322133 234121
Same Query
Last_Name First_Name _________ _________ Johnson Larkins McRoberts Smith Hanson Phillips Delaney Wilson Bond Thomas
Stanley Michael Richard Andy Henry Martin Danny Susie Jimmy Wendy
SELECT * FROM Student_Table ORDER BY Grade_Pt ;
Class_Code _________ Grade_Pt _______ ? FR JR SO FR SR SR SO JR FR
? 0.00 1.90 2.00 2.88 3.00 3.35 3.80 3.95 4.00
Notice that the answer set is sorted in ascending order based on the column Grade_Pt. Also notice that Grade_Pt is the fifth column coming back on the report. That is why the SQL in both statements is ordering by Grade_Pt. Did you notice that the null value came back first? Nulls sort first in ascending order and last in descending order.
Page 20
Chapter 2
The Basics of SQL
Changing the ORDER BY to Descending Order Sorts the Answer Set In DESC Order By Last_Name
Student_ID Last_Name ________ _________ 231222 Wilson 234121 Thomas 333450 Smith 123250 Phillips 280023 McRoberts 423400 Larkins 260000 Johnson 125634 Hanson 324652 Delaney 322133 Bond
SELECT * FROM Student_Table ORDER BY Last_Name DESC;
First_Name Class_Code Grade_Pt ________ _________ _______ Susie SO 3.80 Wendy FR 4.00 Andy SO 2.00 Martin SR 3.00 Richard JR 1.90 Michael FR 0.00 Stanley ? ? Henry FR 2.88 Danny SR 3.35 Jimmy JR 3.95
Notice that the answer set is sorted in descending order based on the column Last_Name. Also, notice that Last_Name is the second column coming back on the report. We could have done an Order By 2. If you spell out the word DESCENDING the query will fail, so you must remember to just use DESC.
Page 21
Chapter 2
The Basics of SQL
NULL Values sort First in Ascending Mode (Default) SELECT * FROM Student_Table ORDER BY 5 ;
Student_ID _________ 260000 423400 280023 333450 125634 123250 324652 231222 322133 234121
SELECT * FROM Student_Table ORDER BY Grade_Pt ;
Last_Name First_Name _________ _________ Johnson Larkins McRoberts Smith Hanson Phillips Delaney Wilson Bond Thomas
Stanley Michael Richard Andy Henry Martin Danny Susie Jimmy Wendy
Class_Code _________ Grade_Pt _______ ? FR JR SO FR SR SR SO JR FR
Nulls sort first in ASC Order
? 0.00 1.90 2.00 2.88 3.00 3.35 3.80 3.95 4.00
Did you notice that the null value came back first? Nulls sort first in ascending order and last in descending order.
Page 22
Chapter 2
The Basics of SQL
NULL Values sort Last in Descending Mode (DESC SELECT * FROM Student_Table ORDER BY 5 DESC ;
Student_ID Last_Name _________ __________ 234121 Thomas 322133 Bond 231222 Wilson 324652 Delaney 123250 Phillips 125634 Hanson 333450 Smith 280023 McRoberts 423400 Larkins 260000 Johnson
SELECT * FROM Student_Table ORDER BY Grade_Pt DESC ;
First_Name __________ Wendy Jimmy Susie Danny Martin Henry Andy Richard Michael Stanley
Class_Code ________ Grade_Pt __________ FR 4.00 JR 3.95 SO 3.80 SR 3.35 SR 3.00 FR 2.88 SO 2.00 Nulls sort JR 1.90 Last in FR 0.00 DESC Order ? ?
You can ORDER BY in descending order by putting a DESC after the column name or its corresponding number. Null Values will sort Last in DESC order.
Page 23
Chapter 2
The Basics of SQL
Major Sort vs. Minor Sorts SELECT * FROM Student_Table ORDER BY Class_Code DESC, Grade_Pt ASC;
Student_ID _________ Last_Name ________ 123250 324652 333450 231222 280023 322133 423400 125634 234121 260000
Phillips Delaney Smith Wilson McRoberts Bond Larkins Hanson Thomas Johnson
Major Sort on Class_Code Descending Minor Sort on Grade_Pt Ascending
First_Name _________ Class_Code Grade_Pt _________ _______ Martin Danny Andy Susie Richard Jimmy Michael Henry Wendy Stanley
SR SR SO SO JR JR FR FR FR ?
Major sorts first
3.00 3.35 2.00 3.80 1.90 3.95 0.00 2.88 4.00 ?
Minor sorts on ties
Major sort is the first sort. There can only be one major sort. A minor sort kicks in if there are Major Sort ties. There can be zero or more minor sorts. Page 24
Chapter 2
The Basics of SQL
Multiple Sort Keys using Names vs. Numbers SELECT * FROM Employee_Table ORDER BY Dept_No DESC ,Salary ASC ,Last_Name ASC;
SELECT * FROM Employee_Table ORDER BY 2 DESC, 5, 3 ASC ;
These queries sort identically Employee_No __________ 2341218 1256349 1121334 2312225 1324657 1333454 1232578 1000234 2000000
Dept_No _______ 400 400 400 300 200 200 100 10 ?
Last_Name _________ First_Name _______ Salary ________ Reilly Harrison Strickling Larkins Coffing Smith Chambers Smythe Jones
William Herbert Cletus Loraine Billy John Mandee Richard Squiggy
36000.00 54500.00 54500.00 40200.00 41888.88 48000.00 48850.00 64300.00 32800.50
In the example above, the Dept_No is the major sort and we have two minor sorts. The minor sorts are on the Salary and the Last_Name columns. Both Queries above have an equivalent Order by statement and sort exactly the same. Page 25
Chapter 2
The Basics of SQL
Sorts are Alphabetical, NOT Logical SELECT * FROM Student_Table ORDER BY Class_Code ;
Student_ID ________ Last_Name First_Name Grade_Pt ________ ________ Class_Code ________ ________ 260000 234121 125634 423400 322133 280023 231222 333450 324652 123250
Johnson Thomas Hanson Larkins Bond McRoberts Wilson Smith Delaney Phillips
Stanley Wendy Henry Michael Jimmy Richard Susie Andy Danny Martin
? FR FR FR JR JR SO SO SR SR
? 4.00 2.88 0.00 3.95 1.90 3.80 2.00 3.35 3.00
This sorts alphabetically. Can you change the sort so the Freshman come first, followed by the Sophomores, Juniors, Seniors and then the Null?
Can you change the query to Order BY Class_Code logically (FR, SO, JR, SR, ?)?
Page 26
Chapter 2
The Basics of SQL
Using A CASE Statement to Sort Logically SELECT * FROM Student_Table ORDER BY CASE Class_Code WHEN 'FR' WHEN 'SO' CASE in the WHEN 'JR' ORDER BY WHEN 'SR' Statement
THEN 1 THEN 2 THEN 3 THEN 4 ELSE 5
END; Student_ID ________ Last_Name First_Name Grade_Pt ________ ________ Class_Code ________ ________ 234121 125634 423400 333450 231222 280023 322133 123250 324652 260000
This is the way the pros do it.
Page 27
Thomas Hanson Larkins Smith Wilson McRoberts Bond Phillips Delaney Johnson
Wendy Henry Michael Andy Susie Richard Jimmy Martin Danny Stanley
FR FR FR SO SO JR JR SR SR ?
4.00 2.88 0.00 2.00 3.80 1.90 3.95 3.00 3.35 ?
Chapter 2
The Basics of SQL
An Order By That Uses an Expression SELECT RTRIM(Last_Name) + ', ' + First_Name AS FullName FROM Employee_Table ORDER BY RTRIM(Last_Name) + ', ' + First_Name; SELECT RTRIM(Last_Name) + ', ' + First_Name AS FullName FROM Employee_Table ORDER BY FullName;
FullName ________________ Chambers, Mandee Coffing, Billy Harrison, Herbert Jones, Squiggy Larkins, Loraine Reilly, William Smith, John Smythe, Richard Strickling, Cletus
The above examples are equivalent. We actually used the FullName expression in the ORDER BY of the first example. The second example uses the alias FullName. Page 28
Chapter 2
The Basics of SQL
How to ALIAS a Column Name SELECT First_Name AS Fname ,Last_Name Lname ,Class_Code "Class Code" ,Grade_Pt AS "AVG" ,Student_ID AS STU_ID FROM Student_Table WHERE Class_Code = 'FR' ; Fname ______ Lname ________ Class Code ____ AVG ______ STU_ID ______
Michael Larkins Henry Hanson Wendy Thomas
FR FR FR
0.00 2.88 4.00
423400 125634 234121
Different Techniques for Aliasing
Notice the column headers
ALIAS Rules! 1) AS is optional 2) Use Double Quotes when Spaces are in the Alias name 3) Use Double Quotes when the Alias is a reserved word
When you ALIAS a column, you give it a new name for the report header. You never need Double Quotes in SQL unless you are aliasing. Page 29
Chapter 2
The Basics of SQL
Aliasing a Column Name with Spaces or Reserved Words SELECT Employee_No AS Emp_Number , Dept_No Dept_Number , Last_name "Last Name" , First_name AS 'First Name' , Salary AS [Employee Pay] FROM Employee_table
For an Alias with a space or one that is also a reserved word use either: 1. Double quotes 2. Single quotes 3. Brackets
Emp_Number Dept_Number _________ Last Name __________ First_Name Employee Pay ____________ ____________ ____________ 1000234 10 Smythe Richard 64300.00 1121334 400 Strickling Cletus 54500.00 1232578 100 Chambers Mandee 48850.00 1236548 400 Mays Mary 50000.00 1256349 400 Harrison Herbert 54500.00 1324657 200 Coffing Billy 41888.88 1333454 200 Smith John 48000.00 2000000 ? Jones Squiggy 32800.50 2312225 300 Larkins Loraine 40200.00 2341218 400 Reilly William 36000.00
When you ALIAS a column, you give it a new name for the report header. If your alias is a reserved word or has a space in it you can still use it, but you must use either double quotes, single quotes or brackets. Page 30
Chapter 2
The Basics of SQL
A Missing Comma can by Mistake become an Alias SELECT First_Name, Last_Name, Class_Code Grade_Pt FROM Student_Table ; Missing a Comma
First_Name Last_Name _________ _________ Grade_Pt _______
Michael Susie Richard Jimmy Henry Andy Danny Stanley Wendy Martin
Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
FR SO JR JR FR SO SR ? FR SR
Aliased as Grade_Pt
Column names must be separated by commas. Notice in this example, there is a comma missing between Class_Code and Grade_Pt. What this will result in is only three columns appearing on your report with one being aliased wrong. Page 31
Chapter 2
The Basics of SQL
Comments using Double Dashes are Single Line Comments Comment
Student_ID _________ 260000 423400 280023 333450 125634 123250 324652 231222 322133 234121
-- Double Dashes provide a single line comment SELECT * FROM Student_Table ORDER BY Grade_Pt ;
Last_Name First_Name _________ _________ Johnson Stanley Larkins Michael McRoberts Richard Smith Andy Hanson Henry Phillips Martin Delaney Danny Wilson Susie Bond Jimmy Thomas Wendy
Class_Code _________ Grade_Pt _______ ? FR JR SO FR SR SR SO JR FR
Double dashes make a single line comment that will be ignored by the system.
Page 32
? 0.00 1.90 2.00 2.88 3.00 3.35 3.80 3.95 4.00
Chapter 2
The Basics of SQL
Comments for Multi-Lines Comment
/* This is how you can make multi-line comments to express what is going on in the code. */
SELECT * FROM Student_Table ORDER BY Grade_Pt ;
Student_ID _________ 260000 423400 280023 333450 125634 123250 324652 231222 322133 234121
Last_Name First_Name _________ _________ Johnson Larkins McRoberts Smith Hanson Phillips Delaney Wilson Bond Thomas
Stanley Michael Richard Andy Henry Martin Danny Susie Jimmy Wendy
Class_Code _________ Grade_Pt _______ ? FR JR SO FR SR SR SO JR FR
Slash Asterisk starts a multi-line comment and Asterisk Slash ends the comment. Page 33
? 0.00 1.90 2.00 2.88 3.00 3.35 3.80 3.95 4.00
Chapter 2
The Basics of SQL
Comments for Multi-Lines as Double Dashes Per Line
Comments
Student_ID _________ 260000 423400 280023 333450 125634 123250 324652 231222 322133 234121
-- This is how you can make multi-line comments -- also to express what is going on in the code. SELECT * FROM Student_Table ORDER BY Grade_Pt ;
Last_Name First_Name _________ _________ Johnson Stanley Larkins Michael McRoberts Richard Smith Andy Hanson Henry Phillips Martin Delaney Danny Wilson Susie Bond Jimmy Thomas Wendy
Class_Code _________ Grade_Pt _______ ? FR JR SO FR SR SR SO JR FR
Double Dashes in front of both lines comments both lines out and they’re ignored. Page 34
? 0.00 1.90 2.00 2.88 3.00 3.35 3.80 3.95 4.00
Chapter 2
The Basics of SQL
A Great Technique for Comments to Look for SQL Errors SELECT Student_ID ,Last_Name ,First_Name ,Class_Code as Sum ,Grade_Pt FROM Student_Table WHERE Grade_Pt > 3.6 ERROR
Comment
SELECT Student_ID ,Last_Name ,First_Name -,Class_Code as Sum ,Grade_Pt FROM Student_Table WHERE Grade_Pt > 3.6
Student_ID _________ Last_Name _________ First_Name _______ Grade_Pt ________ 234121 231222 322133
Thomas Wilson Bond
Wendy Susie Jimmy
4.00 3.80 3.95
The query on the left had an error because the keyword Sum is reserved. We can test if this is the problem by commenting out that line in our SQL (example on the right). Now, our query works. We know the problem is on the line that we commented out. Once we put "Sum" (double quotes around the alias) it works. Use comments to help you debug.
Page 35
Chapter 2
The Basics of SQL
sp_help at the Database Level sp_help ; Name _______________ Aggreg_Order_v Addresses Claims Course_table Customer_table Department_table Emp_Job_table Employee_table Hierarchy_table Job_table Order_table Providers Sales_table Services Stats_table Student_Course_table Student_table Subscribers Table_XYZ
Owner ____________ Object_Type _______ dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo dbo
view user table user table user table user table user table user table user table user table user table user table user table user table user table user table user table user table user table user table
The sp_help command lists the available objects in the current database. There is much more provided than just the objects, but each object will be displayed. Above, we ran the sp_help command while in the SQL_Class database.
Page 36
Chapter 2
The Basics of SQL
sp_help at the Object Level sp_help 'Customer_Table ; Name _____________
Owner ______
Customer_table
dbo
Type Created_datetime _________ ____________________ user table
03/21/2015 7:50:09.470
Column_Name Type Computed Length Precision Scale ________ Nullable _______________ _______ ________ ______ _______ ____ 10 0 yes Customer_Number int no 4 yes Customer_Name varchar no 20 yes Phone_Number char no 8
Data_located_on_filegroup ______________________ PRIMARY
Index_name __________ Index_description ________________________________ Index_keys ________________ clustered, unique located on PRIMARY Customer_Number Idx1
The sp_help Object_Name command lists a wide variety of information about an object. Above is an example of using the command on the Customer_Table. There are actually nine sets of data about the object such as: Name, creation date, owner, Columns, Identity columns, Row GUID columns, FileGroup location, Indexes, Constraints, Foreign Key references and View references. This information is essential when examining or troubleshooting a table. Page 37
Chapter 2
The Basics of SQL
Getting System Information
“Not all who wander are lost.” – J. R. R. Tolkien
The query above uses SQL Server and Azure SQL Data Warehouse reserved names to get the User, their login, the host and the application names. When you can get system information you are never lost!
Page 38
Chapter 2
The Basics of SQL
Getting Additional System Information
The query above uses SQL Server and Azure SQL Data Warehouse reserved names to get additional information on the system.
Page 39
Chapter 3
Page 40
The WHERE Clause
Chapter 3
The WHERE Clause
Chapter 3 – The WHERE Clause
“I saw the angel in the marble and carved until I set him free.” - Michelangelo
Page 41
Chapter 3
The WHERE Clause
The WHERE Clause limits Returning Rows Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT First_Name, Last_Name, Class_Code, Grade_Pt FROM Student_Table WHERE First_Name = 'Henry'; First_Name Class_Code _________ Grade_Pt __________ Last_Name __________ __________ Henry Hanson FR 2.88
The WHERE Clause here filters how many ROWS are coming back. In this example, I am asking for the report to only include rows WHERE the first name is Henry.
Page 42
Chapter 3
The WHERE Clause
Double Quoted Aliases are for Reserved Words and Spaces The AS keyword is always optional.
SELECT First_Name AS Fname ,Last_Name Lname If spaces are in the Alias, or it is a reserved word you ,Class_Code "Class Code" must use double quotes, ,Grade_Pt AS "AVG" single quotes or brackets. ,Student_ID FROM Student_Table If Double Quotes ORDER BY "AVG" ; are used, then use the Double Quotes throughout the SQL
“Write a wise saying and your name will live forever.” - Anonymous
When you ALIAS a column, you give it a new name for the report header, but a good rule of thumb is to refer to the column by the alias throughout the query. Whoever wrote the above quote was way off. "Write a wise alias and it will live until the query ends – bummer".
Page 43
Chapter 3
The WHERE Clause
Using A Column ALIAS In A WHERE Clause SELECT First_Name AS Fname ,Last_Name Lname ,Class_Code "Class Code" ,Grade_Pt AS "AVG" ,Student_ID FROM Student_Table WHERE Fname = 'Henry' ;
Aliasing a column
Using the ALIAS again in your WHERE Clause!
error
When you ALIAS a column, you give it a new name. An ALIAS created in a SELECT clause does not work in a WHERE clause.
Page 44
Chapter 3
The WHERE Clause
Using A Column ALIAS In An ORDER BY Clause SELECT First_Name AS Fname ,Last_Name Lname ,Class_Code "Class Code" ,Grade_Pt AS "AVG" ,Student_ID FROM Student_Table ORDER BY Fname ;
Aliasing a column
Using the ALIAS again in your ORDER BY Clause!
perfect
When you ALIAS a column, you give it a new name. An ALIAS created in a SELECT clause works in an ORDER BY clause.
Page 45
Chapter 3
The WHERE Clause
In What Order Does SQL Server Process A Query?
1
FROM
2
WHERE
3
GROUP BY
4
HAVING
5 SELECT 6
Aliases created here
ORDER BY
Nothing, even an alias, does not exist until it is created. This explains why ALIASES which are created in a SELECT clause can only be used again in an ORDER BY clause.
Page 46
Chapter 3
The WHERE Clause
Character Data needs Single Quotes in the WHERE Clause Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT * FROM Student_Table WHERE First_Name = 'Henry' ; Character data needs Single Quotes in the WHERE clause
In the WHERE clause, if you search for Character data such as first name, you need single quotes around it. You don’t single-quote integers.
Page 47
Chapter 3
The WHERE Clause
Character Data needs Single Quotes, but Numbers Don’t Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT * FROM Student_Table WHERE First_Name = 'Henry' ;
SELECT * FROM Student_Table WHERE Grade_Pt = 0.00 ; Numbers never need quotes
Character data (letters) need single quotes, but you need NO Single Quotes for Integers (numbers). Remember, you never use double quotes except for aliasing. Page 48
Chapter 3
The WHERE Clause
Declaring a Variable Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
DECLARE @Dept_Var as INT = 400; SELECT First_Name, Dept_No FROM Employee_Table WHERE Dept_No = @Dept_Var;
First_Name __________ Cletus Herbert William
Dept_No ________ 400 400 400
You can declare a variable and then use it in the select. WARNING: You must highlight these two queries together and run as one query. You cannot run the first query and then run the second query or it will error.
Page 49
Chapter 3
The WHERE Clause
Comparisons against a Null Value Col_A 1 2 3 4 5 6 7 8 9 10 NULL NULL NULL NULL NULL NULL
Operator + / * > >=
= Mgrs.Salary ;
Mgr ____ Y N Y N Y N N N Y
Which Workers make a bigger Salary than their Manager?
A Self Join gives itself 2 different Aliases, which is then seen as two different tables. Page 173
Chapter 6
Join Functions
The Self Join with ANSI Syntax Employee_Table2 Employee_No Dept_No Last_Name First_Name Salary ____________ _______ _________ _________ _______ 1232578 100 Chambers Mandee 48850.00 54500.00 1256349 400 Harrison Herbert 2341218 400 Reilly William 36000.00 54500.00 1121334 400 Strickling Cletus 2312225 300 Larkins Loraine 40200.00 2000000 ? Jones Squiggy 32800.50 1000234 10 Smythe Richard 32800.00 41888.88 1324657 200 Coffing Billy 48000.00 1333454 200 Smith John SELECT Mgrs.Dept_No , Mgrs.Last_Name as MgrName , Mgrs.Salary as MgrSal , Emps.Last_Name as EmpName , Emps.Salary as Empsal FROM Employee_Table2 as Emps INNER JOIN Employee_Table2 as Mgrs ON Emps.Dept_No = Mgrs.Dept_No WHERE Mgrs.Mgr = 'Y' AND Emps.Salary > Mgrs.Salary ;
Mgr ____ Y N Y N Y N N N Y
Which Workers make a bigger Salary than their Manager?
A Self Join gives itself 2 different Aliases, which is then seen as two different tables.
Page 174
Chapter 6
Join Functions
Quiz – Will both queries bring back the same Answer Set? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
SELECT * FROM Customer_Table as Cust INNER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number WHERE Customer_Name like 'Billy%' ORDER BY 1; Will both queries bring back the same result set?
Page 175
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
SELECT * FROM Customer_Table as Cust INNER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number AND Customer_Name like 'Billy%' ORDER BY 1;
Chapter 6
Join Functions
Answer – Will both queries bring back the same Answer Set? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
SELECT * FROM Customer_Table as Cust INNER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number WHERE Customer_Name like 'Billy%' ORDER BY 1;
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
SELECT * FROM Customer_Table as Cust INNER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number AND Customer_Name like 'Billy%' ORDER BY 1;
Will both queries bring back the same result set? Yes! Because they’re both inner joins.
Page 176
12347.53 8005.91 5111.47 15231.62 23454.84
Chapter 6
Join Functions
Quiz – Will both queries bring back the same Answer Set? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
SELECT * FROM Customer_Table as Cust LEFT OUTER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number WHERE Customer_Name like 'Billy%' ORDER BY 1;
Will both queries bring back the same result set? Page 177
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
SELECT * FROM Customer_Table as Cust LEFT OUTER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number AND Customer_Name like 'Billy%' ORDER BY 1;
Chapter 6
Join Functions
Answer – Will both queries bring back the same Answer Set? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
SELECT * FROM Customer_Table as Cust LEFT OUTER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number WHERE Customer_Name like 'Billy%' ORDER BY 1;
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
SELECT * FROM Customer_Table as Cust LEFT OUTER JOIN Order_Table as ORD ON Cust.Customer_Number = Ord.Customer_Number AND Customer_Name like 'Billy%' ORDER BY 1;
Will both queries bring back the same result set? NO! The WHERE is performed last.
Page 178
12347.53 8005.91 5111.47 15231.62 23454.84
Chapter 6
Join Functions
How would you Join these two tables? Course_Table Course_ID Course_Name Credits _________ _________________ ______ Seats ____ 100 Database Concepts 3 50 200 Introduction to SQL 3 20 210 Advanced SQL 3 22 220 V2R3 SQL Features 2 25 300 Physical Database Design 4 20 400 Database Administration 4 16 Student_Table Student_ID __________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name __________ Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
First_Name Class_Code Grade_Pt __________ __________ ________ Michael FR 0.00 Susie SO 3.80 Richard JR 1.90 Jimmy JR 3.95 Henry FR 2.88 Andy SO 2.00 Danny SR 3.35 Stanley ? ? Wendy FR 4.00 Martin SR 3.00
How would you join these two tables together? You can't do it. There is no matching column with like data. There is no Primary Key/Foreign Key relationship between these two tables. That is why you are about to be introduced to a bridge table. It is formally called an Associative table or a Lookup table. Page 179
Chapter 6
Join Functions
An Associative Table is a Bridge that Joins Two Tables Associative
Course_Table
Table
Course_ID Course_Name Credits _________ _________________ ______ Seats ____ 100 Database Concepts 3 50 200 Introduction to SQL 3 20 210 Advanced SQL 3 22 220 V2R3 SQL Features 2 25 300 Physical Database Design 4 20 400 Database Administration 4 16
Student_Course_Table Student_ID Course_ID 280023 231222 125634 231222 125634 322133 125634 322133 324652 333450 260000 333450 234121 123250
210 210 100 220 200 220 220 300 200 500 400 400 100 100
Student_Table Student_ID __________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name __________ Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
First_Name Class_Code Grade_Pt __________ __________ ________ Michael FR 0.00 Susie SO 3.80 Richard JR 1.90 Jimmy JR 3.95 Henry FR 2.88 Andy SO 2.00 Danny SR 3.35 Stanley ? ? Wendy FR 4.00 Martin SR 3.00
The Associative Table is a bridge between the Course_Table and Student_Table.
Page 180
Chapter 6
Join Functions
Quiz – Can you write the 3-Table Join? Associative
Course_Table
Table
Course_ID Course_Name Credits _________ _________________ ______ Seats ____ 100 Database Concepts 3 50 200 Introduction to SQL 3 20 210 Advanced SQL 3 22 220 V2R3 SQL Features 2 25 300 Physical Database Design 4 20 400 Database Administration 4 16
Student_Course_Table Student_ID Course_ID 280023 231222 125634 231222 125634 322133 125634 322133 324652 333450 260000 333450 234121 123250
210 210 100 220 200 220 220 300 200 500 400 400 100 100
Student_Table Student_ID __________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name __________ Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
First_Name Class_Code Grade_Pt __________ __________ ________ Michael FR 0.00 Susie SO 3.80 Richard JR 1.90 Jimmy JR 3.95 Henry FR 2.88 Andy SO 2.00 Danny SR 3.35 Stanley ? ? Wendy FR 4.00 Martin SR 3.00
SELECT ALL Columns from the Course_Table and Student_Table and Join them. Page 181
Chapter 6
Join Functions
Answer to Quiz – Can you Write the 3-Table Join? Student_Course_Table Student_Table
Student_ID Last_Name First_Name Class_Code Grade_Pt
Course_Table Student_ID Course_ID
SELECT S.*, C.* FROM Student_Table as S, Course_Table as C, Student_Course_Table as SC Where S.Student_ID = SC.Student_ID AND C.Course_ID = SC.Course_ID ;
Course_ID Course_Name Credits Seats
Notice the * technique of getting ALL columns from both tables!
The Associative Table is a bridge between the Course_Table and Student_Table, and its sole purpose is to join these two tables together.
Page 182
Chapter 6
Join Functions
Quiz – Can you write the 3-Table Join to ANSI Syntax? Student_Course_Table Student_Table
Student_ID Last_Name First_Name Class_Code Grade_Pt
Course_Table Student_ID Course_ID
Course_ID Course_Name Credits Seats
SELECT S.*, C.* FROM Student_Table as S, Course_Table as C, Student_Course_Table as SC Where S.Student_ID = SC.Student_ID AND C.Course_ID = SC.Course_ID ; Convert this query to ANSI syntax Please re-write the above query using ANSI Syntax.
Page 183
Chapter 6
Join Functions
Answer – Can you Write the 3-Table Join to ANSI Syntax? Student_Course_Table
Student_Table Student_ID Last_Name First_Name Class_Code Grade_Pt
Course_Table Student_ID Course_ID
Course_ID Course_Name Credits Seats
ANSI Syntax Traditional Syntax SELECT S.*, C.* FROM Student_Table as S, Course_Table as C, Student_Course_Table as SC Where S.Student_ID = SC.Student_ID AND C.Course_ID = SC.Course_ID ;
Select S.*, C.* From Student_Table as S INNER JOIN Student_Course_Table as SC ON S.Student_ID = SC.Student_ID INNER JOIN Course_Table as C ON C.Course_ID = SC.Course_ID;
The above queries show both traditional and ANSI form for this three table join.
Page 184
Chapter 6
Join Functions
Quiz – Can you Place the ON Clauses at the End? Student_Course_Table Student_Table Student_ID Last_Name First_Name Class_Code Grade_Pt
Course_Table Student_ID Course_ID
Course_ID Course_Name Credits Seats
ANSI Syntax Select S.*, C.* From Student_Table as S INNER JOIN Student_Course_Table as SC ON S.Student_ID = SC.Student_ID INNER JOIN Course_Table as C ON C.Course_ID = SC.Course_ID; Please re-write the above query and place both ON Clauses at the end.
Page 185
Can you rewrite this and place all of the ON clauses at the end?
Chapter 6
Join Functions
Answer – Can you Place the ON Clauses at the End? Student_Course_Table Student_Table Student_ID Last_Name First_Name Class_Code Grade_Pt
Course_Table Student_ID Course_ID
Course_ID Course_Name Credits Seats
Select S.*, C.* The trick is to From Student_Table as S put the first ON INNER JOIN clause for the Student_Course_Table as SC last join and go INNER JOIN backwards Course_Table as C ON C.Course_ID = SC.Course_ID ON SC.Student_ID = S.Student_ID;
This is tricky. The only way it works is to place the ON clauses backwards. The first ON Clause represents the last INNER JOIN and then moves backwards. Page 186
Chapter 6
Join Functions
The 5-Table Join – Logical Insurance Model Addresses
Subscriber_No
Subscribers
Claims
Subscriber_No
Subscriber_No
Member_No
Member_No
Services Service_Code
Claim_Service
Providers Provider_Code
Provider_No
Above is the logical model for the insurance tables showing the Primary Key and Foreign Key relationships (PK/FK).
Page 187
Chapter 6
Join Functions
Quiz - Write a Five Table Join Using ANSI Syntax Addresses
Subscriber_No
Subscribers
Claims
Subscriber_No
Subscriber_No
Member_No
Member_No
Services Service_Code
Claim_Service
Providers Provider_Code
Provider_No
Your mission is to write a five table join selecting all columns using ANSI syntax.
Page 188
Chapter 6
Join Functions
Answer - Write a Five Table Join Using ANSI Syntax SELECT cla1.*, sub1.*, add1.* ,pro1.*, ser1.* FROM CLAIMS AS cla1 INNER JOIN SUBSCRIBERS AS sub1 ON cla1.Subscriber_No = sub1.Subscriber_No AND cla1.Member_No = sub1.Member_No INNER JOIN ADDRESSES AS add1 ON sub1.Subscriber_No = add1.Subscriber_No INNER JOIN PROVIDERS AS pro1 ON cla1.Provider_No = pro1.Provider_Code INNER JOIN SERVICES AS ser1 ON cla1.Claim_Service = ser1.Service_Code ;
Above is the example writing this five table join using ANSI syntax.
Page 189
Chapter 6
Join Functions
Quiz - Write a Five Table Join Using Non-ANSI Syntax Addresses
Subscriber_No
Subscribers
Claims
Subscriber_No
Subscriber_No
Member_No
Member_No
Services Service_Code
Claim_Service
Providers Provider_Code
Provider_No
Your mission is to write a five table join selecting all columns using Non-ANSI syntax.
Page 190
Chapter 6
Join Functions
Answer - Write a Five Table Join Using Non-ANSI Syntax SELECT FROM
WHERE AND AND AND AND
cla1.*, sub1.*, add1.* ,pro1.*, ser1.* CLAIMS AS cla1, SUBSCRIBERS AS sub1, ADDRESSES AS add1, PROVIDERS AS pro1, SERVICES AS ser1 cla1.Subscriber_No = sub1.Subscriber_No cla1.Member_No = sub1.Member_No sub1.Subscriber_No = add1.Subscriber_No cla1.Provider_No = pro1.Provider_Code cla1.Claim_Service = ser1.Service_Code ;
Above is the example writing this five table join using Non-ANSI syntax.
Page 191
Chapter 6
Join Functions
Quiz –Re-Write this putting the ON clauses at the END SELECT cla1.*, sub1.*, add1.* ,pro1.*, ser1.* FROM CLAIMS AS cla1 INNER JOIN SUBSCRIBERS AS sub1 ON cla1.Subscriber_No = sub1.Subscriber_No AND cla1.Member_No = sub1.Member_No INNER JOIN ADDRESSES AS add1 ON sub1.Subscriber_No = add1.Subscriber_No INNER JOIN PROVIDERS AS pro1 ON cla1.Provider_No = pro1.Provider_Code INNER JOIN SERVICES AS ser1 ON cla1.Claim_Service = ser1.Service_Code ;
Above is the example writing this five table join using Non-ANSI syntax.
Page 192
Chapter 6
Join Functions
Answer –Re-Write this putting the ON clauses at the END SELECT cla1.*, sub1.*, add1.* ,pro1.*, ser1.* FROM PROVIDERS AS pro1 INNER JOIN ADDRESSES AS add1 INNER JOIN SUBSCRIBERS AS sub1 INNER JOIN SERVICES AS ser1 INNER JOIN CLAIMS as cla1 ON cla1.Claim_Service = ser1.Service_Code ON cla1.Subscriber_No = sub1.Subscriber_No AND cla1.Member_No = sub1.Member_No ON sub1.Subscriber_No =add1.Subscriber_No ON cla1.Provider_No = pro1.Provider_Code ;
Above is the example writing this five table join using ANSI syntax with the ON clauses at the end. We had to move the tables around also to make this happen. Notice that the first ON clause represents the last two tables being joined, and then it works backwards.
Page 193
Chapter 7
Page 194
Date Functions
Chapter 7
Date Functions
Chapter 7 – Date Function
"An inch of time cannot be bought with an inch of gold." - Chinese Proverb
Page 195
Chapter 7
Date Functions
Current_Timestamp
Above is the keyword Current_Timestamp that allows a user to get the timestamp. This is a reserved word and so the system will deliver the timestamp to you when requested.
Page 196
Chapter 7
Date Functions
Getdate This example uses the Getdate() function to return the timestamp.
SELECT Getdate() as "The Date"; The Date -----------03/30/2015 8:46:04.567
“Speak in a moment of anger and you’ll deliver the greatest speech you’ll ever regret.” – Anonymous
The Getdate command will return today's date and time just like the Current_Timestamp command. This is not ANSI.
Page 197
Chapter 7
Date Functions
Date and Time Keywords SELECT GETDATE() AS [GETDATE] , CURRENT_TIMESTAMP AS [CURRENT_TIMESTAMP] , GETUTCDATE() AS [GETUTCDATE] GETDATE CURRENT_TIMESTAMP 03/30/2015 8:42:04.833 03/30/2015 8:42:04.833 Date and Time
Date and Time ANSI
SELECT SYSDATETIME() ,SYSUTCDATETIME()
Date and Time UTC
AS [SYSDATETIME] AS [SYSUTCDATETIME]
SYSDATETIME 2015-03-30 08:42:04.8355769 Date and Time
GETUTCDATE 03/30/2015 1:42:04.833
SYSUTCDATETIME 2015-03-30 13:42:04.8355769 Date and Time UTC
The above examples show how to get the date and time. The GETDATE and CURRENT_TIMESTAMP are equivalent, but CURRENT_TIMESTAMP is ANSI compliant. The differences between the top and bottom examples are that the top has a data type of DATETIME and the bottom DATETIME2, which is an expanded form of DATETIME. Page 198
Chapter 7
Date Functions
SYSDATETIMEOFFSET Provides the Timezone Offset SELECT SYSUTCDATETIME() AS [SYSUTCDATETIME] ,SYSDATETIMEOFFSET() AS [SYSDATETIMEOFFSET]; GETUTCDATE 2015-03-30 13:42:04.8355769 Date and Time UTC
SYSDATETIMEOFFSET 2015-03-30 08:42:04.8355769 -05:00 Date and Time with a Timezone offset
“Life is a succession of lessons, which must be lived to be understood”. --Ralph Waldo Emerson
The CETUTCDATE function will provide a Current_Timestamp, but in Universal Time Coordinate (UTC) time. The SYSDATETIMEOFFSET shows the timezone difference between UTC and the local Current_Timestamp. Remember, date and time are a succession of days and hours, which must be queried to be understood!
Page 199
Chapter 7
SYSDATETIMEOFFSET Provides the Timezone Offset
This is how you can get just the current_date and the current_time..
Page 200
Date Functions
Chapter 7
Date Functions
Using both CAST and CONVERT in Literal Values SELECT CAST('20150216' AS DATE) as "Date YMD"; Date YMD 2015-02-16
SELECT CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112) AS "Converted" ; Converted
20150330 This converts the current date and time to CHAR(8) by using style 112 ('YYYYMMDD')
This is an example of using the CAST function with a date literal. The first SQL example converts the character string literal ‘20150216’ to a DATE data type. The second SQL example converts the current date and current time to a CHAR (8) data type using the style 112, which is a 'YYYYMMDD' format.
Page 201
Chapter 7
Date Functions
Using Both CAST and CONVERT in Literal Values
Converts the current date and time value to a CHAR(12) by using style 114 ('hh:mm:ss.nnn').
This example converts the current date and time value to CHAR (12) by using style 114 ('hh:mm:ss.nnn').
Page 202
Chapter 7
Date Functions
Using both CAST and CONVERT in Literal Values SELECT SYSDATETIME() as "Local Time Eastern" ,SWITCHOFFSET(SYSDATETIMEOFFSET(), '-06:00') as "Timestamp Central" ,SWITCHOFFSET(SYSDATETIMEOFFSET(), '-07:00') as "Timestamp Mountain" ,SWITCHOFFSET(SYSDATETIMEOFFSET(), '-08:00') as "Timestamp Pacific" ; 2015-03-30 11:03:38.9877064 2015-03-30 10:03:38.9877064 -06:00 2015-03-30 09:03:38.9877064 -07:00 2015-03-30 08:03:38.9877064 -08:00
Local Time Eastern Timestamp Central Timestamp Mountain Timestamp Pacific
The times above are the converted times, but they are displayed vertically to save space on the screen
The SWITCHOFFSET function can be used to adjust an input DATETIMEOFFSET value to a specified time zone. We are showing in the example SQL above how to convert to Central, Mountain and Pacific time. Page 203
Chapter 7
Date Functions
The DATEADD Function
Valid values for the part input include year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, and nanosecond. You can also specify the part in abbreviated form, such as yy instead of year.
The syntax for the DATEADD function is DATEADD (part, n, date_value). Valid values for the part are year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, and nanosecond.
Page 204
Chapter 7
Date Functions
The DATEDIFF Function
The syntax for the DATEDIFF function is DATEDIFF (part, dt_val1, dt_val2). Above we have used the literal dates of '2014-01-30 (January 30, 2014) and '2015-06-30' (June 30, 2015). We then can see the differences in the number of years, months, days, hours, minutes and seconds.
Page 205
Chapter 7
Date Functions
DATEADD Function SELECT Order_Date ,DateAdd (Day, 60,Order_Date) as "Due Date" ,Order_Total ,DateAdd (Day, 50,Order_Date) as Discount ,Cast(Order_Total *.98 as Decimal(8,2)) as Discount_Total FROM Order_Table ORDER BY 1 ;
Order_Date __________ 05/04/1998 01/01/1999 09/09/1999 10/01/1999 10/10/1999
Due Date Order_Total _________ Discount _____________ Discount_Total _________ __________ 07/03/1998 03/02/1999 11/08/1999 11/30/1999 12/09/1999
12347.53 8005.91 23454.84 5111.47 15231.62
06/23/1998 02/20/1999 10/29/1999 11/20/1999 11/29/1999
12,100.58 7,845.79 22,985.74 5,009.24 14,926.99
Valid values for the part argument include year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond, TZoffset, and ISO_WEEK. Page 206
Chapter 7
Date Functions
A Real World Example for DateAdd Using the Order Table SELECT Order_Date ,DateAdd (Day, 60,Order_Date) as "Due Date" ,Order_Total ,DateAdd (Day, 50,Order_Date) as Discount ,Cast(Order_Total *.98 as Decimal(8,2)) as Discount_Total FROM Order_Table ORDER BY 1 ;
Order_Date __________ 05/04/1998 01/01/1999 09/09/1999 10/01/1999 10/10/1999
Due Date Order_Total _________ Discount _____________ Discount_Total _________ __________ 07/03/1998 03/02/1999 11/08/1999 11/30/1999 12/09/1999
12347.53 8005.91 23454.84 5111.47 15231.62
06/23/1998 02/20/1999 10/29/1999 11/20/1999 11/29/1999
The example above uses a real world example from the Order_Table.
Page 207
12,100.58 7,845.79 22,985.74 5,009.24 14,926.99
Chapter 7
Date Functions
DATEPART Function SELECT Order_Date ,DateAdd (Day, 60,Order_Date) as "Due Date" ,Order_Total ,DateAdd (Day, 50,Order_Date) as Discount ,Cast(Order_Total *.98 as Decimal(8,2)) as Discount_Total FROM Order_Table WHERE DATEPART(Month, Order_Date) = 10 ORDER BY 1 ;
Order_Date __________
Due Date Order_Total _________ Discount _____________ Discount_Total _________ __________
10/01/1999 11/30/1999 10/10/1999 12/09/1999
5111.47 11/20/1999 15231.62 11/29/1999
5,009.24 14,926.99
This example only looks for orders that happened in October. This is done by using the DATEPART function in the WHERE clause. Valid values for the part argument include year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond, TZoffset, and ISO_WEEK.
Page 208
Chapter 7
Date Functions
DATEPART Function Examples SELECT * FROM Order_Table WHERE DATEPART(Year, Order_Date) = 1998 ;
Year = 1998
SELECT * FROM Order_Table WHERE DATEPART(Quarter, Order_Date) = 4 ;
Quarter = 4th
SELECT * FROM Order_Table WHERE DATEPART(Month, Order_Date) = 10 ;
Month = October
SELECT * FROM Order_Table WHERE DATEPART(Day, Order_Date) = 4 ;
Day = 4th day of the month
SELECT * FROM Order_Table WHERE DATEPART(DayofYear, Order_Date) = 1 ;
Day of year = January 1st
SELECT * FROM Order_Table WHERE DATEPART(Week, Order_Date) = 1 ;
Week = 1st week of year
SELECT * FROM Order_Table WHERE DATEPART(WeekDay, Order_Date) = 1 ;
Week Day = Sunday
Above are some excellent examples to pull from using the DATEPART function. Page 209
Chapter 7
Date Functions
YEAR, MONTH, and DAY Functions SELECT Order_Date ,Year(Order_Date) as "Yr" ,Month(Order_Date) as "Mo" ,Day(Order_Date) as "Day" FROM Order_Table ORDER BY 1 ;
Order_Date ____ Yr Mo __________ ___ 1998-05-04 1999-01-01 1999-09-09 1999-10-01 1999-10-10
1998 1999 1999 1999 1999
5 1 9 10 10
Day ____ 4 1 9 1 10
The YEAR, MONTH, and DAY functions are abbreviations for the DATEPART function.
Page 210
Chapter 7
Date Functions
A Better Technique for YEAR, MONTH, and DAY Functions SELECT Order_Number, Customer_Number, Order_Date, Order_Total FROM Order_Table WHERE YEAR(order_date) = 1999 AND MONTH(order_date) = 10;
SELECT Order_Number, Customer_Number, Order_Date, Order_Total FROM Order_Table This approach is more efficient for SQL WHERE order_date >= '19991001' Server and Azure SQL Data Warehouse. AND order_date < '19991101' Indexes can take advantage of this technique!
Both queries above do the same thing and deliver the same result set, but the bottom query could be much faster.
Order_Number ________________ Customer_Number Order_Date _____________ __________ Order_Total __________ 123552 123585
31323134 87323456
1999-10-01 1999-10-10
5111.47 15231.62
Above are the tale of two queries. The top query applies manipulation on the filtered column, in most cases SQL Server can’t use an index efficiently when using this technique. The bottom query uses a range filter instead. Brilliant! Page 211
Chapter 7
Date Functions
DATENAME Function SELECT Order_Date ,DATENAME(Year, Order_Date) as "Yr" ,DATENAME(Month, Order_Date) as "Mo" ,DATENAME(Day, Order_Date) as "Day" FROM Order_Table ORDER BY 1 ;
Order_Date ____ Yr __________ 1998-05-04 1999-01-01 1999-09-09 1999-10-01 1999-10-10
1998 1999 1999 1999 1999
Mo _________
Day ____
May January September October October
4 1 9 1 10
The DATENAME function returns the name of the requested part rather than the number. Notice above that only the Month returns the actual name of the month, but both the Year and the Day still return the integer values.
Page 212
Chapter 7
Date Functions
Date Formatting SELECT GetDate() as [default] ,CONVERT(VARCHAR(20), GETDATE(), 100) [Month AM PM] ,CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] ,CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY] ,CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD] default Month AM PM MM/DD/YY MM/DD/YYYY YY.MM.DD __________________ __________________ ___________ _____________ ___________ 05/14/2015 4:53:31.320 May 14 2015 4:53PM 05/14/15
05/14/2015
15.05.14
SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD] ,CONVERT(VARCHAR(26), GETDATE(), 109) AS [Month Time 3 Mil] ,DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY] ,REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') AS [DD-Mon-YYYY] YYYY.MM.DD _________________________ Month Time 3 Mil Month DD, YYYY _______________ DD-Mon-YYYYY _____________ _______________ 2015.05.14 May 14 2015 5:00:42:013PM May 14, 2015 14-May-2015
How about those examples!
Page 213
Chapter 7
Date Functions
Time Formatting SELECT substring(convert(varchar(20), GetDate(), 9), 13, 5) + ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2) As [time AM PM] ,convert(varchar, getdate(), 108) [hh:mm:ss] ,convert(varchar, getdate(), 109) [mon dd yyyy hh:mm:ss:mmmAM]; time AM PM hh:mm:ss mon dd yyyy hh:mm:ss:mmmAM ___________ ________ ____________________________ 5:16 PM
17:16:36
May 14 2015 5:16:36:960PM
SELECT convert(varchar, getdate(), 121) [yyyy-mm-dd hh:mm:ss.mmm] ,convert(varchar, getdate(), 126) [yyyy-mm-ddThh:mm:ss.mmm] ,convert(varchar, getdate(), 114) [hh:mm:ss:mmm(24h)] ; yyyy-mm-dd hh:mm:ss.mmm ________________________ yyyy-mm-dd hh:mm:ss.mmm __________________ hh:mm:ss:mmm(24h) ________________________ 2015-05-14 17:21:03.300 2015-05-14T17:21:03.300 17:21:03:300
How about those examples! What great timing!
Page 214
Chapter 7
Date Functions
ISDATE Function
The ISDATE function accepts a character string as input and returns a Boolean. ISDATE returns a 1 if it is convertible to a date and time data type. It returns a 0 if it is not convertible to a date and time data type. Above, we have used the date of February 29th. This is only a valid date during a leap year. It only returns a 1 when the date is valid.
Page 215
Chapter 8
Page 216
Temporary Tables
Chapter 8
Temporary Tables
Chapter 8 - Temporary Tables
“Graffiti’s always been a temporary art form. You make your mark and then they scrub it off.” - Banksy
Page 217
Chapter 8
Temporary Tables
There are three types of Temporary Tables in TEMPDB Private Temporary Table Is always created as #tablename Space comes from tempdb Can only be used by the connection that created the table Can be created by the User, then populated with an INSERT/SELECT Table and Data are deleted after the connection that created the table is closed
Global Temporary Table Is always created as ##tablename Space comes from tempdb Can be used by any connection Can be created by the User, then populated with an INSERT/SELECT Table and Data are deleted after the creating connection is closed and all queries running against the table complete
Direct Temporary Table Is always created as tempdb.tablename Space comes from tempdb Can be used by anyone who has specifically been granted access. Can be created by the User, then populated with an INSERT/SELECT Table and Data exist after the creating connection is closed
The three types of Temporary tables in TEMPDB are Private, Global, and Direct Temporary Tables. Page 218
Chapter 8
Temporary Tables
Tables in TEMPDB are not your only Temporary Storage Derived Table Is a SELECT statement within a SELECT statement Is purely logical as opposed to physical Exists only within a query Has its execution optimized at run time along with the rest of a query
Common Table Expression(CTE) Is defined by using a WITH statement Can be referenced numerous times in a query Is purely logical as opposed to physical Exists only within a query Has its execution optimized at run time along with the rest of a query Can be used for recursive queries
Table Variables Are variables Are written to disk in tempdb Exist for the duration of the batch that holds them Perform better for smaller datasets
Data can be stored temporarily in Derived Tables, Common Table Expressions, and Table Variables. Page 219
Chapter 8
Temporary Tables
What is TEMPDB? TEMPDB is a database similar to all other SQL Server databases
It is recreated every time SQL Server is started
Allows for transactions to be rolled back, but does not allow for database recovery
Because of limited logging, operations in TEMPDB can be much faster than in other databases
Is the storage location for private, global, and direct temporary tables; as well as table variables Like most things in life, TEMPDB is temporary. It is wonderful for temporary data storage. Just do not count on it having data that will be there for you in the future. Page 220
Chapter 8
Temporary Tables
Creating a Private Temporary Table Private Temporary Tables can be viewed and used only from the connection that creates them. They only exist for the life of the session that creates them.
CREATE TABLE #Dept_Agg_Vol ( Dept_no Integer ,Sum_Salary Decimal(10,2) ) ; CREATE CLUSTERED INDEX IDX_#Dept_Agg_Vol_Dept_no ON #Dept_Agg_Vol (Dept_no) ; Private Temporary Tables can have clustered and non-clustered indexes just like “regular “tables. Both the tables and their indexes are stored in tempdb.
Only you or your connection can use a Private Temporary Table.!
Page 221
Chapter 8
Temporary Tables
You Populate a Private Temporary Table with an INSERT/SELECT CREATE TABLE #Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ;
2
INSERT INTO #Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) FROM Employee_Table GROUP BY Dept_no ; Private Temporary Tables can be Materialized with An Insert/Select statement
1) A USER Creates a Private Temporary Table and then 2) populates it with an INSERT/SELECT Statement. The space to materialize this table comes from tempdb. Now you can query this table all session long. When the session is logged off the table and the data are automatically deleted. Page 222
Chapter 8
Temporary Tables
The Three Steps to Use a Private Temporary Table CREATE TABLE #Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ; INSERT INTO #Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) 2 FROM Employee_Table GROUP BY Dept_no ;
3 SELECT * FROM #Dept_Agg_Vol ORDER BY 1;
Only you can see this data because your session number is associated with your Private Temporary Tables. You can’t even see this table if you login and query it from another session!
1) A USER Creates a Private Temporary Table and then 2) populates the it with an INSERT/SELECT Statement, and then 3) Queries it until Logging off. Page 223
Chapter 8
Temporary Tables
Creating a Global Temporary Table Global Temporary Tables can be viewed and used ny any connection. They exist funtil both the connection that created them closes and any active queries using them complete.
CREATE TABLE ##Dept_Agg_Vol ( Dept_no Integer ,Sum_Salary Decimal(10,2) ) ; CREATE CLUSTERED INDEX IDX_##Dept_Agg_Vol_Dept_no ON ##Dept_Agg_Vol (Dept_no) ; GlobalTemporary Tables can have clustered and non-clustered indexes just like “regular “tables. Both the tables and their indexes are stored in tempdb.
All connections can use a Global Temporary Table! Think of it as not being private at all.
Page 224
Chapter 8
Temporary Tables
You Populate a Global Temporary Table with an INSERT/SELECT CREATE TABLE ##Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ;
2
INSERT INTO ##Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) FROM Employee_Table GROUP BY Dept_no ; Global Temporary Tables can be Materialized with An Insert/Select statement
1) A USER Creates a Global Temporary Table and then 2) populates it with an INSERT/SELECT Statement. The space to materialize this table comes from tempdb. Any connection can query this table. When the creating connection is logged off and the last active query using the table completes the table and the data are automatically deleted. Page 225
Chapter 8
Temporary Tables
The Three Steps to Use a Global Temporary Table CREATE TABLE ##Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ; INSERT INTO ##Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) 2 FROM Employee_Table GROUP BY Dept_no ;
3 SELECT * FROM ##Dept_Agg_Vol ORDER BY 1;
Everyone can see and use this Global Temporary Table. So be careful with data that needs to stay private.
1) A USER Creates a Global Temporary Table and then 2) a user populates the it with an INSERT/SELECT Statement, and then 3) everyone can query it until the creating user logsoff and the last active query completes. Page 226
Chapter 8
Temporary Tables
Creating a Direct Temporary Table Direct Temporary Tables can be viewed and used only by users who have been granted access to them. They exist until they are DROPPED or the SQL Server is stopped and restarted.
CREATE TABLE TEMPDB.Dept_Agg_Vol ( Dept_no Integer ,Sum_Salary Decimal(10,2) ) ; CREATE CLUSTERED INDEX IDX_Dept_Agg_Vol_Dept_no ON TEMPDB.Dept_Agg_Vol (Dept_No) ; Direct Temporary Tables can have clustered and non-clustered indexes just like “regular “tables. Both the tables and their indexes are stored in tempdb.
To create Direct Temporary Tables, you must have CREATE TABLE priviledges in TEMPDB. You might want to talk to your database administrator about these priviledges.
Only users that have specifically been granted access can use a Direct Temporary Table! This allows you to protect your data. Access to your data is limited to people who should be allowed to see your data. Page 227
Chapter 8
Temporary Tables
You Populate a Direct Temporary Table with an INSERT/SELECT CREATE TABLE TEMPDB.Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ;
2
INSERT INTO TEMPDB.Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) FROM Employee_Table GROUP BY Dept_no ; Direct Temporary Tables can be Materialized with An Insert/Select statement
1) A USER Creates a Direct Temporary Table and then 2) GRANTS users access and then 3) a user populates it with an INSERT/SELECT Statement. 4) Direct Temporary Tables remain available until they are DROPPED or the SQL Server is stopped and restarted. Page 228
Chapter 8
Temporary Tables
The Three Steps to Use a Direct Temporary Table CREATE TABLE TEMPDB.Dept_Agg_Vol ( Dept_no Integer 1 ,Sum_Salary Decimal(10,2) ) ; INSERT INTO TEMPDB.Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) 2 FROM Employee_Table GROUP BY Dept_no ;
3 SELECT * FROM TEMPDB.Dept_Agg_Vol ORDER BY 1;
Direct Temporary Tables can be viewed and used only by users who have been granted access to them.
1) A USER Creates a Direct Temporary Table and then 2) GRANTS users access and then 3) a user populates it with an INSERT/SELECT Statement. 4) Direct Temporary Tables remain available until they are DROPPED or the SQL Server is stopped and restarted.
Page 229
Chapter 8
Temporary Tables
CREATING A Derived Table • • • •
Is a SELECT Statement with a SELECT Statement Is purely logical as opposed to physical Exists only within a query Has its execution optimized at run time along with the rest of a query
SELECT * FROM (SELECT AVG(salary) FROM Employee_Table) AS TeraTom(AVGSAL) ; A query within a query.
AVGSAL ________ 46782.15
Answer Set
The SELECT Statement that creates and populates the Derived table is always inside Parentheses. Page 230
Chapter 8
Temporary Tables
Naming the Derived Table SELECT * FROM (SELECT AVG(salary) FROM Employee_Table) AS TeraTom(AVGSAL) ;
The name of the Derived Table is TeraTom
AVGSAL ________ 46782.15
Answer Set
In the example above, TeraTom is the name we gave the Derived Table. It is mandatory that you always name the table or it errors. Page 231
Chapter 8
Temporary Tables
Aliasing the Column Names in the Derived Table SELECT * FROM (SELECT AVG(salary) FROM Employee_Table) AS TeraTom(AVGSAL) ; AVGSALis the Column Name in the derived table named TeraTom
AVGSAL ________
46782.15
Answer Set
AVGSAL is the name we gave to the column in our Derived Table that we call TeraTom. Our SELECT (which builds the columns) shows we are only going to have one column in our derived table, and we have named that column AVGSAL.
Page 232
Chapter 8
Temporary Tables
Multiple Ways to Alias the Columns in a Derived Table 1
SELECT * FROM (SELECT AVG(salary) FROM Employee_Table) AS TeraTom(AVGSAL) ; The derived table must always be named
2
SELECT * FROM (SELECT AVG(salary) AS AVGSAL FROM Employee_Table) AS TeraTom ; The derived table must always be named
Page 233
Alias CAN be done here
Alias CAN be done inside the derived SELECT statement
Chapter 8
Temporary Tables
CREATING a Derived Table using the WITH Command Create the Derived Table while we run the query!
WITH TeraTom(AVGSAL) AS (SELECT AVG(salary)FROM Employee_Table) SELECT * FROM TeraTom ;
AVGSAL ________ 46782.15
Answer Set
When using the WITH Command, we can CREATE our Derived table while running the main query.
Page 234
Chapter 8
Temporary Tables
The Same Derived Query shown Three Different Ways
1
SELECT * FROM (SELECT AVG(salary) FROM Employee_Table) TeraTom (AVGSAL) ; Alias CAN be done here or here
2
3
Page 235
SELECT * FROM (SELECT AVG(salary) as AVGSAL FROM Employee_Table) TeraTom ;
WITH TeraTom(AVGSAL) AS (SELECT AVG(salary)FROM Employee_Table) SELECT * FROM TeraTom ;
Chapter 8
Temporary Tables
MULTIPLE Derived Tables using the WITH Command 1st Derived Table
2nd Derived Table
WITH WellPaid(Employee_No, Last_Name) AS (SELECT Employee_No, Last_Name FROM Employee_Table WHERE Salary > (SELECT AVG(Salary) FROM Employee_Table)) ,DeptMgr(Mgr_No, Department_Name) AS (SELECT Mgr_No, Department_Name FROM Department_Table JOIN WellPaid ON (Employee_No = Mgr_No)) SELECT Last_Name AS WellPaidMgr , Department_Name FROM WellPaid JOIN DeptMgr ON (Employee_No = Mgr_No) ;
Using the WITH Command, we can CREATE multiple Derived tables that can be referenced elsewhere in the query. Page 236
Chapter 8
Temporary Tables
Column Alias Can Default For Normal Columns I don't need to alias this SELECT E.*, AVGSAL because it can default to its FROM Employee_Table as E current name INNER JOIN (SELECT Dept_No, AVG(salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) AS TeraTom ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
TeraTom Dept_No AVGSAL ________ ________ ? 32800.50 10 64300.00 100 48850.00 200 44944.44 300 40200.00 400 48333.33 The derived table is built first
In a derived table, you will always have a SELECT query in parenthesis, and you will always name the table. You have options when aliasing the columns. As in the example above, you can let normal columns default to their current name. Page 237
Chapter 8
Temporary Tables
Most Derived Tables Are Used To Join To Other Tables SELECT E.*, AVGSAL The SELECT is the FROM Employee_Table as E Derived Table INNER JOIN (SELECT Dept_No, AVG(salary) FROM Employee_Table GROUP BY Dept_No) AS TeraTom (Dept_No, AVGSAL) ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
The derived table name is TeraTom
The columns are aliased
Employee_No _______ Dept_No Last_Name First_Name ______ Salary ___________ ________ ________ 1000234 1232578 1324657 1333454 2312225 1121334 1256349 2341218
10 100 200 200 300 400 400 400
Smythe Chambers Coffing Smith Larkins Strickling Harrison Reilly
Richard Mandee Billy John Loraine Cletus Herbert William
64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 54500.00 36000.00
AVGSAL _______ 64300.00 48850.00 44944.44 44944.44 40200.00 48333.33 48333.33 48333.33
The first five columns in the Answer Set came from the Employee_Table. AVGSAL came from the derived table named TeraTom Page 238
Chapter 8
Temporary Tables
A Join Example Showing Different Column Alias Styles SELECT E.*, AVGSAL This does not need an alias because it can default to its FROM Employee_Table as E current name INNER JOIN (SELECT Dept_No as Dept_No, AVG(salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) AS TeraTom This must have ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
an alias because it is an aggregate
Employee_No ________ Dept_No _________ Last_Name _________ First_Name _______ Salary AVGSAL __________ _______ 1000234 1232578 1324657 1333454 2312225 1121334 1256349 2341218
Page 239
10 100 200 200 300 400 400 400
Smythe Chambers Coffing Smith Larkins Strickling Harrison Reilly
Richard Mandee Billy John Loraine Cletus Herbert William
64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 54500.00 36000.00
64300.00 48850.00 44944.44 44944.44 40200.00 48333.33 48333.33 48333.33
Chapter 8
Temporary Tables
The Three Components of a Derived Table SELECT E.*, Salary, AVGSAL FROM Employee_Table as E INNER JOIN (SELECT Dept_No, AVG(salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) AS TeraTom ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
Dept_No AVGSAL ________ ________ ? 32800.50 10 64300.00 100 48850.00 200 44944.44 300 40200.00 400 48333.33 The derived table is optimized with the rest of the query.
1
A derived table is a SELECT query. The SELECT query always starts with an open parenthesis and ends with a close parenthesis.
2
The derived table must be given a name. Above we called our derived table TeraTom.
3
You will need to define (alias) the columns in the derived table. Above we could allow Dept_No to default to Dept_No, but we had to specifically alias AVG(Salary) as AVGSAL.
Every derived table must have the three components listed above Page 240
TeraTom
Chapter 8
Temporary Tables
Visualize This Derived Table SELECT E.*, (Salary - AVGSAL) as PlusMinAvg FROM Employee_Table as E INNER JOIN (SELECT Dept_No, AVG(salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) AS TeraTom ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
Employee_No ____________ Dept_No ________ 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 1256349 400 2341218 400
TeraTom Dept_No AVGSAL ________ ________ ? 32800.50 10 64300.00 100 48850.00 200 44944.44 300 40200.00 400 48333.33 The derived table is built first
Last_Name Salary PlusMinAvg ___________ First_Name ___________ ________ ___________ Smythe Richard 64300.00 0.00 Chambers Mandee 48850.00 0.00 Coffing Billy 41888.88 -3055.56 Smith John 48000.00 3055.56 Larkins Loraine 40200.00 0.00 Strickling Cletus 54500.00 6166.67 Harrison Herbert 54500.00 6166.67 Reilly William 36000.00 -12333.33
Our example above shows the data in the derived table named TeraTom. This query allows us to see each employee and the plus or minus avg of their salary compared to the other workers in their department. Page 241
Chapter 8
Temporary Tables
Our Join Example With The WITH Syntax WITH TeraTom (Dept_No, AVGSAL) AS (SELECT Dept_No , AVG(Salary) FROM Employee_Table GROUP BY Dept_No) SELECT E.*, AVGSAL FROM Employee_Table as E INNER JOIN TeraTom ON E.Dept_No = TeraTom.Dept_No ORDER BY E.Dept_No ;
Now, the lower portion of the query refers to TeraTom Almost like it is a permanent table, but it is not!
Page 242
TeraTom Dept_No AVGSAL ________ ________ ? 32800.50 10 64300.00 100 48850.00 200 44944.44 300 40200.00 400 48333.33
Chapter 8
Temporary Tables
Quiz - Answer the Questions SELECT Dept_No, First_Name, Last_Name, AVGSAL FROM Employee_Table INNER JOIN (SELECT Dept_No, AVG(Salary) FROM Employee_Table GROUP BY Dept_No) as TeraTom (Depty, AVGSAL) ON Dept_No = Depty ;
1) What is the name of the derived table? 2) How many columns are in the derived table? 3) What is the name of the derived table columns?
4) Is there more than one row in the derived table? 5) What common keys join the Employee and Derived? 6) Why were the join keys named differently?
Page 243
Chapter 8
Temporary Tables
Answer to Quiz - Answer the Questions SELECT Dept_No, First_Name, Last_Name, AVGSAL FROM Employee_Table INNER JOIN (SELECT Dept_No, AVG(Salary) FROM Employee_Table GROUP BY Dept_No) as TeraTom (Depty, AVGSAL) ON Dept_No = Depty ;
1) What is the name of the derived table? TeraTom 2) How many columns are in the derived table? 2
3) What’s the name of the derived columns? Depty and AVGSAL 4) Is their more than one row in the derived table? Yes 5) What keys join the tables? Dept_No and Depty 6) Why were the join keys named differently? If both were named Dept_No, we would error unless we full qualified.
Page 244
Chapter 8
Temporary Tables
Clever Tricks on Aliasing Columns in a Derived Table SELECT Dept_No, First_Name, Last_Name, AVGSAL FROM Employee_Table Alias Here INNER JOIN
1
(SELECT Dept_No as Depty, AVG(Salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) as TeraTom ON Dept_No = Depty ;
SELECT E.Dept_No, First_Name, Last_Name, AVGSAL FROM Employee_Table as E INNER JOIN Alias Here
2
(SELECT Dept_No, AVG(Salary) as AVGSAL FROM Employee_Table GROUP BY Dept_No) as TeraTom ON E.Dept_No = TeraTom.Dept_No ;
Page 245
Chapter 8
Temporary Tables
A Derived Table lives only for the lifetime of a single query
First query
1
Second query
WITH T (Dept_No, AVGSAL) AS (SELECT Dept_No, AVG(Salary) FROM Employee_Table GROUP BY Dept_No) SELECT T.Dept_No, First_Name, Last_Name, AVGSAL FROM Employee_Table as E INNER JOIN T ON E.Dept_No = T.Dept_No ;
SELECT * FROM T ;
2
The semi-colon (;) indicates the end of the query.
Page 246
Error – Query Fails…. T does Not exist.
Chapter 8
Temporary Tables
An Example of Two Derived Tables in a Single Query WITH T (Dept_No, AVGSAL) AS (SELECT Dept_No, AVG(Salary) FROM Employee_Table GROUP BY Dept_No) SELECT T.Dept_No, First_Name, Last_Name, AVGSAL, Counter FROM Employee_Table as E INNER JOIN T ON E.Dept_No = T.Dept_No INNER JOIN (SELECT Employee_No, SUM(1) OVER(PARTITION BY Dept_No ORDER BY Dept_No, Last_Name Rows Unbounded Preceding) FROM Employee_Table) as S (Employee_No, Counter) ON E.Employee_No = S.Employee_No ORDER BY T.Dept_No;
Page 247
Chapter 8
Temporary Tables
RECURSIVE Derived Table Hierarchy TeraTom Coffing CEO
Jane Stevens VP North
Ricardo Gonzales VP South
Hitesh Patel North Manager
Inquayee Mumba South Manager
North Analysts
South Analysts
Robert Pantelle Ming Zao Constantine Mikas
Betty Boston Kelly Roberts Brett Valens
Above is a company hierarchy and this is what we will use to perform our WITH Recursive query.
Page 248
Chapter 8
Temporary Tables
RECURSIVE Derived Table Query WITH TeraTom (Emp, Mgr, LastN, Pos_Name, DEPTH) AS (SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, 0 FROM Hierarchy_Table WHERE Mgr_Employee_No IS NULL UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom INNER JOIN Hierarchy_Table ON Emp= Mgr_Employee_No ) SELECT * FROM TeraTom ORDER BY 5,2,1 ;
Above is the WITH Recursive query.
Page 249
Chapter 8
Temporary Tables
RECURSIVE Derived Table Definition This is a recursive query
The recursive derived table's name
The recursive derived table is defined with 5 columns. They are Emp, Mgr, LastN, Pos_Name, DEPTH
WITH TeraTom (Emp, Mgr, LastN, Pos_Name, DEPTH) AS (SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, 0 FROM Hierarchy_Table WHERE Mgr_Employee_No IS NULL UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom INNER JOIN Hierarchy_Table ON Emp= Mgr_Employee_No ) SELECT * FROM TeraTom ORDER BY 5,2,1 ;
Above is the WITH Recursive query and the highlighted part explains the recursive derived table definition itself. Page 250
Chapter 8
Temporary Tables
WITH RECURSIVE Derived Table Seeding WITH TeraTom This entire (Emp, Mgr, LastN, Pos_Name, DEPTH) AS highlighted (SELECT Employee_No, Mgr_Employee_No, section will Last_Name, Position_Name, 0 produce only FROM Hierarchy_Table a single row in our WHERE Mgr_Employee_No IS NULL derived table UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom INNER JOIN Hierarchy_Table ON Emp= Mgr_Employee_No ) SELECT * FROM TeraTom ORDER BY 5,2,1 ; One row is Emp Mgr ________ LastN Pos_Name ______ Depth ____ ____ __________
1
?
Coffing
CEO
0
placed in our derived table. That is called "seeding the Table".
Above is the WITH Recursive query and the highlighted part explains how the first row is placed inside the derived table. The only employee with no manager is the CEO, Tom Coffing. His Mgr_Employee_No is NULL. The table is now seeded! Page 251
Chapter 8
Temporary Tables
WITH RECURSIVE Derived Table Looping WITH TeraTom (Emp, Mgr, LastN, Pos_Name, DEPTH) AS (SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, 0 FROM Hierarchy_Table WHERE Mgr_Employee_No IS NULL UNION ALL The highlighted SELECT Employee_No, Mgr_Employee_No, section joins the derived table Last_Name, Position_Name, DEPTH+1 to the FROM TeraTom Hierarchy_Table INNER JOIN and loops Hierarchy_Table until finished ON Emp= Mgr_Employee_No ) SELECT * FROM TeraTom ORDER BY 5,2,1 ; Emp Mgr ________ LastN Pos_Name ______ Depth ____ ____ __________ 1
?
Coffing
CEO
0
Above is the WITH Recursive query and the highlighted part explains how the derived table is joined to the Hierarchy_Table in a looping fashion. The highlighted part keeps looping and adding rows until it loops and adds no rows. Then it is done.
Page 252
Chapter 8
Temporary Tables
RECURSIVE Derived Table Looping in Slow Motion UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom The first loop INNER JOIN places two more Hierarchy_Table ON Emp= Mgr_Employee_No rows inside the derived table
TeraTom Emp Mgr ________ LastN Pos_Name ______ Depth ____ ____ __________ 1 10 20
? 1 1
Coffing Stevens Gonzales
CEO VP NORTH VP SOUTH
0 1 1
Above is the WITH Recursive query and the highlighted part explains how the derived table is joined to the Hierarchy_Table in a looping fashion. The highlighted part keeps looping and adding rows until it loops and adds no rows. Then it is done. This is the first loop and as you can see two rows were added. That is because our join condition is Emp = Mgr_Employee_No. Both Stevens and Gonzales report to a manager with an Emp = 1.
Page 253
Chapter 8
Temporary Tables
RECURSIVE Derived Table Looping Continued UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom The second loop INNER JOIN places two more Hierarchy_Table ON Emp= Mgr_Employee_No rows inside the derived table
TeraTom Emp Mgr ________ LastN Pos_Name ______ Depth ____ ____ __________ 1 10 20 100 200
? 1 1 10 20
Coffing Stevens Gonzales Patel Mumba
CEO VP NORTH VP SOUTH North Manager South Manager
0 1 1 2 2
Above is the WITH Recursive query and the highlighted part explains how the derived table is joined to the Hierarchy_Table in a looping fashion. The highlighted part keeps looping and adding rows until it loops and adds no rows. Then it is done. This is the second loop and as you can see two rows were added. That is because our join condition is Emp = Mgr_Employee_No. Both Patel and Mumba report to a manager inside our recursive derived table. Page 254
Chapter 8
Temporary Tables
RECURSIVE Derived Table Looping Continued UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom INNER JOIN Hierarchy_Table ON Emp= Mgr_Employee_No
Emp Mgr ____ ____
The third loop places six more TeraTom rows inside the LastN Pos_Name ______ Depth derived table ________ __________
1 10 20 100 200 1000 3000 5000 2000 4000 6000
Coffing Stevens Gonzales Patel Mumba Mikas Zao Pantelle Valens Roberts Boston
? 1 1 10 20 100 100 100 200 200 200
Six rows are added in the third loop.
Page 255
CEO VP NORTH VP SOUTH North Manager South Manager Analyst North Analyst North Analyst North Analyst South Analyst South Analyst South
0 1 1 2 2 3 3 3 3 3 3
Chapter 8
Temporary Tables
RECURSIVE Derived Table Ends the Looping UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom INNER JOIN Hierarchy_Table ON Emp= Mgr_Employee_No The fourth loop added no rows!
TeraTom Emp Mgr ________ LastN Pos_Name ______ Depth ____ ____ __________ 1 10 20 100 200 1000 3000 5000 2000 4000 6000
? 1 1 10 20 100 100 100 200 200 200
Coffing Stevens Gonzales Patel Mumba Mikas Zao Pantelle Valens Roberts Boston
CEO VP NORTH VP SOUTH North Manager South Manager Analyst North Analyst North Analyst North Analyst South Analyst South Analyst South
No rows were added in the fourth loop. This loop is done!
Page 256
0 1 1 2 2 3 3 3 3 3 3
The loop is finished
Chapter 8
Temporary Tables
RECURSIVE Derived Table Definition WITH TeraTom (Emp, Mgr, LastN, Pos_Name, DEPTH) AS (SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, 0 FROM Hierarchy_Table WHERE Mgr_Employee_No IS NULL UNION ALL SELECT Employee_No, Mgr_Employee_No, Last_Name, Position_Name, DEPTH+1 FROM TeraTom When the loop INNER JOIN failed to add Hierarchy_Table a row the system knows it is done ON Emp= Mgr_Employee_No looping ) SELECT * Now it runs the FROM TeraTom final SELECT to ORDER BY 5,2,1 ; get the answer set. Above is the WITH Recursive query and the highlighted part is now run so the final answer set can be delivered.
Page 257
Chapter 8
RECURSIVE Derived Table Answer Set
Page 258
Temporary Tables
Chapter 8
Temporary Tables
Using a Table Variable DECLARE @Dept_Agg_Vol TABLE ( Dept_no Integer ,Sum_Salary Decimal(10,2)) INSERT INTO @Dept_Agg_Vol SELECT Dept_no ,SUM(Salary) FROM Employee_Table GROUP BY Dept_no SELECT * FROM @Dept_Agg_Vol ;
Notice that the DECLARE statement for a Table Variable looks a lot like a CREATE TABLE statement. This is because Table Variables are similar to tables.
The only semicolon is at the end of the batch.
Table Variables only exist for the duration of their batch, stored procedure or function.
Page 259
Chapter 9
Page 260
Sub-query Functions
Chapter 9
Sub-query Functions
Chapter 9 – Sub-query Functions
“An invasion of Armies can be resisted, but not an idea whose time has come.” - Victor Hugo
Page 261
Chapter 9
Sub-query Functions
An IN List is much like a Subquery Employee_Table
Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
SELECT * FROM Employee_Table WHERE Dept_No IN (100, 200) ; Employee_No ____________ Dept_No ________ 1232578 100 1324657 200 1333454 200
Last_Name _________ Chambers Coffing Smith
First_Name _______ Salary __________ Mandee 48850.00 Billy 41888.88 John 48000.00
This query is very simple and easy to understand. It uses an IN List to find all Employees who are in Dept_No 100 or Dept_No 200.
Page 262
Chapter 9
Sub-query Functions
An IN List Never has Duplicates – Just like a Subquery Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name __________ Jones Squiggy Smythe Richard Chambers Mandee Coffing Billy Smith John Larkins Loraine Strickling Cletus Reilly William Harrison Herbert
Salary _______ 32800.50 64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 36000.00 54500.00
SELECT * FROM Employee_Table WHERE Dept_No IN (100, 100,200, 200) ;
What is going on with this IN List? Why in the world are their duplicates in there? Will this query even work? What will the result set look like? Turn the page!
Page 263
Chapter 9
Sub-query Functions
An IN List Ignores Duplicates Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Duplicate values in SELECT * a list are irrelevant FROM Employee_Table WHERE Dept_No IN (100, 100,200, 200) ;
Employee_No ____________ Dept_No ________ 1232578 100 The answer set still 1324657 200 produced only 3 rows 1333454 200
Last_Name _________ Chambers Coffing Smith
First_Name _______ Salary __________ Mandee 48850.00 Billy 41888.88 John 48000.00
Duplicate values are ignored here. We got the same rows back as before, and it is as if the system ignored the duplicate values in the IN List. That is exactly what happened.
Page 264
Chapter 9
Sub-query Functions
The Subquery Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
There is a Top Query and a Bottom Query!
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Department_Table
Dept_No ________________ Department_Name ________
SELECT * FROM Employee_Table WHERE Dept_No IN ( SELECT Dept_No FROM Department_Table) ;
100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
Which Query Runs First?
The query above is a Subquery which means there are multiple queries in the same SQL. The bottom query runs first, and its purpose in life is to build a distinct list of values that it passes to the top query. The top query then returns the result set. This query solves the problem: Show all Employees in Valid Departments! Page 265
Chapter 9
Sub-query Functions
The Three Steps of How a Basic Subquery Works Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
SELECT * FROM Employee_Table 1 WHERE Dept_No IN ( SELECT Dept_No The Bottom Query runs first! FROM Department_Table) ;
Department_Table Dept_No ________________ Department_Name ________ 100 200 300 400 500
100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
2 The result is passed to the top query!
3 SELECT * FROM Employee_Table WHERE Dept_No IN (100, 200, 300, 400, 500) ;
The top query runs using the bottom query answer set
The bottom query runs first and builds a distinct IN list. Then the top query runs using the list.
Page 266
Chapter 9
Sub-query Functions
These are Equivalent Queries Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
1
2
Department_Table Dept_No ________________ Department_Name ________ 100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
SELECT * FROM Employee_Table WHERE Dept_No IN ( SELECT Dept_No FROM Department_Table) ;
SELECT * FROM Employee_Table WHERE Dept_No IN (100, 200, 300, 400, 500) ;
Both queries above are the same. Query 2 has values in an IN list. Query 1 runs a subquery to build the values in the IN list. Page 267
Chapter 9
Sub-query Functions
The Final Answer Set from the Subquery Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400 Remember that a subquery never has columns return in the final answer set
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Dept_No ________________ Department_Name ________ 100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources Notice that No employees are in dept 500
SELECT * FROM Employee_Table WHERE Dept_No IN ( SELECT Dept_No FROM Department_Table) ; Employee_No Dept_No ____________ ________ 1232578 100 1324657 200 1333454 200 2312225 300 1256349 400 2341218 400 1121334 400
Page 268
Department_Table
Last_Name __________ Chambers Coffing Smith Larkins Harrison Reilly Strickling
First_Name __________ Mandee Billy John Loraine Herbert William Cletus
Salary ________ 48850.00 41888.88 48000.00 40200.00 54500.00 36000.00 54500.00
Chapter 9
Sub-query Functions
Quiz- Answer the Difficult Question Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Department_Table Dept_No ________________ Department_Name ________ 100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
How are Subqueries similar to Joins between two tables?
A great question was asked above. Do you know the key to answering? Turn the page!
Page 269
Chapter 9
Sub-query Functions
Answer to Quiz- Answer the Difficult Question Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Department_Table
Dept_No ________________ Department_Name ________ 100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
Primary Key
Foreign Key
How are Subqueries similar to Joins between two tables?
A Subquery between two tables or a Join between two tables will each need a common key that represents the relationship. This is called a Primary Key/Foreign Key relationship.
A Subquery will use a common key linking the two tables together very similar to a join! When subquerying between two tables, look for the common link between the two tables. Most of the time they both have a column with the same name but not always. Page 270
Chapter 9
Sub-query Functions
Should you use a Subquery of a Join? Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Department_Table
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
When do I Subquery? SELECT * FROM Employee_Table WHERE Dept_No IN ( SELECT Dept_No FROM Department_Table) ;
Dept_No ________________ Department_Name ________ 100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
When do I perform a Join? SELECT E.*, Department_Name FROM Employee_Table as E Inner Join Department_Table as D ON E.Dept_No = D.Dept_No;
If you only want to see a report where the final result set has only columns from one table, use a Subquery. Obviously, if you need columns on the report where the final result set has columns from both tables, you have to do a Join. Page 271
Chapter 9
Sub-query Functions
Quiz- Write the Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________
11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery
Select all columns in the Customer_Table if the customer has placed an order!
Here is your opportunity to show how smart you are. Write a Subquery that will bring back everything from the Customer_Table if the customer has placed an order in the Order_Table. Good luck! Advice: Look for the common key among both tables!
Page 272
Chapter 9
Sub-query Functions
Answer to Quiz- Write the Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________
11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery
Select all columns in the Customer_Table if the customer has placed an order!
SELECT * FROM Customer_Table WHERE Customer_Number IN ( SELECT Customer_Number FROM Order_Table) ;
Customer_Number ________________ 31323134 57896883 11111111 87323456
Customer_Name ______________ ACE Consulting XYZ Plumbing Billy's Best Choice Databases N-U
The common key among both tables is Customer_Number. The bottom query runs first and delivers a distinct list of Customer_Numbers which the top query uses in the IN List! Page 273
Chapter 9
Sub-query Functions
Quiz- Write the More Difficult Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery Select all columns in the Customer_Table if the customer has placed an order over $10,000.00 Dollars!
Here is your opportunity to show how smart you are. Write a Subquery that will bring back everything from the Customer_Table if the customer has placed an order in the Order_Table that is greater than $10,000.00.
Page 274
Chapter 9
Sub-query Functions
Answer to Quiz- Write the More Difficult Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery Select all columns in the Customer_Table if the customer has placed an order over $10,000.00 Dollars!
SELECT * FROM Customer_Table WHERE Customer_Number IN ( SELECT Customer_Number FROM Order_Table WHERE Order_Total > 10000.00) ;
Here is your answer!
Page 275
Customer_Number Customer_Name _______________ _______________ 11111111 Billy's Best Choice 57896883 XYZ Plumbing 87323456 Databases N-U
Chapter 9
Sub-query Functions
Quiz – Write the Extreme Subquery Course_Table Course_ID Course_Name _________ _________________ Student_Course_Table Student_ID Course_ID 280023 210 231222 210 125634 100 231222 220 125634 200 322133 220 125634 220 322133 300 324652 200 333450 500 260000 400 333450 400 234121 100 123250 100
100 200 210 220 300 400
Credits ______ Seats ____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 V2R3 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16 Student_Table
__________ Student_ID 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
__________ Last_Name Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
__________ First_Name __________ Class_Code Grade_Pt ________ Michael FR 0.00 Susie SO 3.80 Richard JR 1.90 Jimmy JR 3.95 Henry FR 2.88 Andy SO 2.00 Danny SR 3.35 Stanley ? ? Wendy FR 4.00 Martin SR 3.00
Write SQL that will bring back an answer set that selects all columns from the Student_Table if that student is taking a course that has four (4) credits.
Use a subquery to get the answer set requested above. The answer is on the next page. Page 276
Chapter 9
Sub-query Functions
Answer to Quiz – Write the Extreme Subquery SELECT S.* FROM Student_Table as S WHERE Student_ID IN (SELECT Student_ID FROM Student_Course_Table WHERE Course_ID IN (SELECT Course_ID FROM Course_Table WHERE Credits=4))
Student_ID _________ 260000 322133 333450
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Johnson Bond Smith
Above is something to enjoy and learn from.
Page 277
Stanley Jimmy Andy
? JR SO
? 3.95 2.00
Chapter 9
Sub-query Functions
Quiz- Write the Subquery with an Aggregate Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name __________ Jones Squiggy Smythe Richard Chambers Mandee Coffing Billy Smith John Larkins Loraine Strickling Cletus Reilly William Harrison Herbert
Salary _______ 32800.50 64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 36000.00 54500.00
Write the Subquery Select all columns in the Employee_Table if the employee makes a greater Salary than the AVERAGE Salary.
Another opportunity knocking! Would someone please answer the query door?
Page 278
Chapter 9
Sub-query Functions
Answer to Quiz- Write the Subquery with an Aggregate Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Write the Subquery Select all columns in the Employee_Table if the employee makes a greater Salary than the AVERAGE Salary. SELECT * FROM Employee_Table WHERE Salary > ( SELECT AVG(Salary) FROM Employee_Table) ;
Page 279
Chapter 9
Sub-query Functions
Quiz- Write the Correlated Subquery Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Write the Correlated Subquery
Select all columns in the Employee_Table if the employee makes a greater Salary than the AVERAGE Salary (within their own Department).
Another opportunity knocking! This is a tough one, and only the best get this written correctly. Page 280
Chapter 9
Sub-query Functions
Answer to Quiz- Write the Correlated Subquery Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Write the Correlated Subquery Select all columns in the Employee_Table if the employee makes a greater Salary than the AVERAGE Salary (within their own Department). SELECT * FROM Employee_Table as EE WHERE Salary > ( SELECT AVG(Salary) FROM Employee_Table as EEEE WHERE EE.Dept_No = EEEE.Dept_No) ;
Page 281
Chapter 9
Sub-query Functions
The Basics of a Correlated Subquery The Top Query is Co-Related (Correlated) with the Bottom Query. The table name from the top query and the table name from the bottom query are given a different alias.
The bottom query WHERE clause co-relates Dept_No from Top and Bottom. The top query is run first. The bottom query is run one time for each distinct value delivered from the top query. SELECT * FROM Employee_Table as EE WHERE Salary > ( SELECT AVG(Salary) FROM Employee_Table as EEEE WHERE EE.Dept_No = EEEE.Dept_No) ;
A correlated subquery breaks all the rules. It is the top query that runs first. Then, the bottom query is run one time for each distinct column in the bottom WHERE clause. In our example, this is the column Dept_No. This is because in our example, the WHERE clause is comparing the column Dept_No. After the top query runs and brings back its rows, the bottom query will run one time for each distinct Dept_No. If this is confusing, it is not you. These take a little time to understand, but I have a plan to make you an expert. Keep reading! Page 282
Chapter 9
Sub-query Functions
The Top Query always runs first in a Correlated Subquery The Top Query runs first (colored in blue)
SELECT * FROM Employee_Table as EE WHERE Salary > ( SELECT AVG(Salary) FROM Employee_Table as EEEE WHERE EE.Dept_No = EEEE.Dept_No)
EE.Dept_No = EEEE.Dept_No
SELECT * FROM Employee_Table as EE Employee_No ____________ Dept_No ________ Last_Name _________ Null is 2000000 skipped ? Jones 1000234 10 Smythe 1232578 100 Chambers 1324657 200 Coffing 1333454 200 Smith 2312225 300 Larkins 1121334 400 Strickling 2341218 400 Reilly 1256349 400 Harrison
First_Name _______ Salary _________ Squiggy 32800.50 Richard 64300.00 Mandee 48850.00 Billy 41888.88 John 48000.00 Loraine 40200.00 Cletus 54500.00 William 36000.00 Herbert 54500.00
Dept_No ________ 10 100 200 300 400
Employee_No ________ Dept_No __________ Last_Name __________ First_Name _______ Salary ____________ 1333454 1256349 1121334
Page 283
200 400 400
Smith Harrison Strickling
John Herbert Cletus
The bottom Query (in red) runs 1 time for each distinct Dept_No
48000.00 54500.00 54500.00
AVGSAL ________ 64300.00 48850.00 44944.44 40200.00 48333.33
Only these three employees make more than the AVG salary within their own department
Chapter 9
Sub-query Functions
Correlated Subquery Example vs. a Join with a Derived Table SELECT Last_Name, Dept_No, Salary FROM Employee_Table as EE WHERE Salary > ( SELECT AVG(Salary) FROM Employee_Table as EEEE WHERE EE.Dept_No = EEEE.Dept_No) ;
SELECT E.*, AVGSAL FROM Employee_Table as E INNER JOIN (SELECT Dept_No, AVG(Salary) FROM Employee_Table GROUP BY Dept_No) as TeraTom (Depty, AVGSAL) ON Dept_No = Depty AND Salary > AVGSAL ;
Correlated Subquery Last_Name ________ Dept_No _______ Salary __________ Smith 200 48000.00 Harrison 400 54500.00 Strickling 400 54500.00
Join with a Derived Table Last_Name ________ Dept_No _______ Salary AVGSAL _________ ________ Smith 200 48000.00 44944.44 Harrison 400 54500.00 48333.33 Strickling 400 54500.00 48333.33
Both queries above will bring back all employees making a salary that is greater than the average salary in their department. The biggest difference is that the Join with the Derived Table also shows the Average Salary in the result set.
Page 284
Chapter 9
Sub-query Functions
Quiz- A Second Chance to Write a Correlated Subquery Sales_Table
Product_ID _________ Sale_Date __________ 1000 10/02/2000 1000 09/30/2000 1000 10/01/2000 All Rows are 2000 10/04/2000 NOT 2000 10/02/2000 Displayed 2000 09/28/2000 3000 10/04/2000 3000 10/02/2000 3000 10/03/2000
Daily_Sales __________ 32800.50 36000.07 40200.43 32800.50 36021.93 41888.88 15675.33 19678.94 21553.79
Write the Correlated Subquery Select all columns in the Sales_Table if the Daily_Sales column is greater than the Average Daily_Sales within its own Product_ID.
Another opportunity knocking! This is your second chance. I will even give you a third chance.
Page 285
Chapter 9
Sub-query Functions
Answer - A Second Chance to Write a Correlated Subquery Select all columns in the Sales_Table if the Daily_Sales column is greater than the Average Daily_Sales within its own Product_ID. SELECT * FROM Sales_Table as TopS WHERE Daily_Sales > ( SELECT AVG(Daily_Sales) FROM Sales_Table as BotS WHERE TopS.Product_ID = BotS.Product_ID) ORDER BY Product_ID, Sale_Date ; Product_ID _________ Sale_Date __________ Daily_Sales __________
Answer Set
Page 286
1000 1000 1000 1000 2000 2000 2000 3000 3000 3000
09/28/2000 09/29/2000 10/03/2000 10/04/2000 09/29/2000 09/30/2000 10/01/2000 09/28/2000 09/29/2000 09/30/2000
48850.40 54500.22 64300.00 54553.10 48000.00 49850.03 54850.29 61301.77 34509.13 43868.86
Chapter 9
Sub-query Functions
Quiz- A Third Chance to Write a Correlated Subquery Sales_Table
Product_ID _________ Sale_Date __________ 1000 10/02/2000 1000 09/30/2000 1000 10/01/2000 All Rows are 2000 10/04/2000 NOT 2000 10/02/2000 Displayed 2000 09/28/2000 3000 10/04/2000 3000 10/02/2000 3000 10/03/2000
Daily_Sales __________ 32800.50 36000.07 40200.43 32800.50 36021.93 41888.88 15675.33 19678.94 21553.79
Write the Correlated Subquery Select all columns in the Sales_Table if the Daily_Sales column is greater than the Average Daily_Sales within its own Sale_Date.
Another opportunity knocking! There is just one minor adjustment and you are home free.
Page 287
Chapter 9
Sub-query Functions
Answer - A Third Chance to Write a Correlated Subquery Select all columns in the Sales_Table if the Daily_Sales column is greater than the Average Daily_Sales within its own Sale_Date. SELECT * FROM Sales_Table as TopS WHERE Daily_Sales > ( SELECT AVG(Daily_Sales) FROM Sales_Table as BotS WHERE TopS.Sale_Date = BotS.Sale_Date) ORDER BY Sale_Date ; Product_ID _________ Sale_Date __________ Daily_Sales __________
Answer Set
Page 288
3000 2000 1000 3000 2000 2000 2000 1000 2000 1000 1000
09/28/2000 09/29/2000 09/29/2000 09/30/2000 09/30/2000 10/01/2000 10/02/2000 10/02/2000 10/03/2000 10/03/2000 10/04/2000
61301.77 48000.00 54500.22 43868.86 49850.03 54850.29 36021.93 32800.50 43200.18 64300.00 54553.10
Chapter 9
Sub-query Functions
Quiz- Last Chance To Write a Correlated Subquery Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
Write the Correlated Subquery Select all columns in the Student_Table if the Grade_Pt column is greater than the Average Grade_Pt within its own Class_Code. Another opportunity knocking! There is just one minor adjustment and you are home free.
Page 289
Chapter 9
Sub-query Functions
Answer – Last Chance to Write a Correlated Subquery Select all columns in the Student_Table if the Grade_Pt column is greater than the Average Grade_Pt within its own Class_Code.
SELECT * FROM Student_Table as TopS WHERE Grade_Pt > ( SELECT AVG(Grade_Pt) FROM Student_Table as BotS WHERE TopS. Class_Code = BotS.Class_Code ) ORDER BY Class_Code ;
Answer Set Student_ID Last_Name First_Name __________ __________ __________ Class_Code __________ Grade_Pt ________ 234121 125634 322133 231222 324652
Page 290
Thomas Hanson Bond Wilson Delaney
Wendy Henry Jimmy Susie Danny
FR FR JR SO SR
4.00 2.88 3.95 3.80 3.35
Chapter 9
Sub-query Functions
Quiz – Write the Extreme Correlated Subquery Course_Table Course_ID Course_Name _________ _________________ Student_Course_Table Student_ID Course_ID 280023 210 231222 210 125634 100 231222 220 125634 200 322133 220 125634 220 322133 300 324652 200 333450 500 260000 400 333450 400 234121 100 123250 100
100 200 210 220 300 400
Credits ______ Seats ____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 V2R3 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16 Student_Table
__________ Student_ID 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
__________ Last_Name Larkins Wilson McRoberts Bond Hanson Smith Delaney Johnson Thomas Phillips
First_Name __________ __________ Class_Code Grade_Pt ________ Michael FR 0.00 Susie SO 3.80 Richard JR 1.90 Jimmy JR 3.95 Henry FR 2.88 Andy SO 2.00 Danny SR 3.35 Stanley ? ? Wendy FR 4.00 Martin SR 3.00
Write a correlated subquery that will bring back an answer set that returns all columns from the Course_Table if that course is being taken by a student who has a greater than average grade point within their own class code.
Use a subquery to get the answer set requested above. The answer is on the next page. Page 291
Chapter 9
Sub-query Functions
Answer To Quiz – Write the Extreme Correlated Subquery SELECT * FROM Course_Table WHERE Course_ID IN (SELECT Course_ID FROM Student_Course_Table WHERE Student_ID IN (SELECT Student_ID FROM Student_Table AS s1 WHERE Grade_Pt > (SELECT AVG(Grade_Pt) FROM Student_Table AS s2 WHERE s1.Class_Code=s2.Class_Code) ) ); Course_ID _________ 200 100 220 300 210
Page 292
Course_Name _____________________ Credits ______ Seats _____ Introduction to SQL 3 20 Database Concepts 3 50 V2R3 SQL Features 2 25 Physical Database Design 4 20 Advanced SQL 3 22
Chapter 9
Sub-query Functions
Quiz- Write the NOT Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
Write the Subquery Select all columns in the Customer_Table if the Customer has NOT placed an order.
Another opportunity knocking! Write the above query
Page 293
12347.53 8005.91 5111.47 15231.62 23454.84
Chapter 9
Sub-query Functions
Answer to Quiz- Write the NOT Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________
11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Select all columns in the Customer_Table if the Customer has NOT placed an order. SELECT * FROM Customer_Table WHERE Customer_Number NOT IN (SELECT Customer_Number FROM Order_Table WHERE Customer_Number IS NOT NULL) ;
Page 294
Nulls are a NOT IN nightmare. Notice how I account for them!
Chapter 9
Sub-query Functions
Quiz- Write the Subquery using a WHERE Clause Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
Write the Subquery Select all columns in the Order_Table that were placed by a customer with ‘Bill’ anywhere in their name.
Another opportunity to show your brilliance is ready for you to make it happen. Page 295
12347.53 8005.91 5111.47 15231.62 23454.84
Chapter 9
Sub-query Functions
Answer - Write the Subquery using a WHERE Clause Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
Write the Subquery Select all columns in the Order_Table that were placed by a customer with ‘Bill’ anywhere in their name.
SELECT * FROM Order_Table WHERE Customer_Number IN (SELECT Customer_Number FROM Customer_Table WHERE Customer_Name like '%Bill%') ;
Great job on writing your query just like the above.
Page 296
12347.53 8005.91 5111.47 15231.62 23454.84
Chapter 9
Sub-query Functions
Quiz – Write the Triple Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery
What is the Customer_Name who has the highest dollar order among all customers? This query will have multiple Subqueries!
Good luck in writing this. Remember that this will involve multiple Subqueries. Page 297
Chapter 9
Sub-query Functions
Answer to Quiz – Write the Triple Subquery Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Write the Subquery What is the Customer_Name who has the highest dollar order among all customers? This query will have multiple Subqueries! SELECT Customer_Name XYZ Plumbing FROM Customer_Table WHERE Customer_Number IN 58796883 This runs (SELECT Customer_Number FROM Order_Table second WHERE Order_Total IN (SELECT Max(Order_Total) FROM Order_Table)) ; 23454.84 This runs first This runs third
The query is above and, of course, the answer is XYZ Plumbing. Page 298
Chapter 9
Sub-query Functions
Quiz – How many rows return on a NOT IN with a NULL? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777 000099
11111111 11111111 31323134 87323456 57896883 NULL
We added a Null Value to the Order_Table
12347.53 8005.91 5111.47 15231.62 23454.84 9999.99 NULL
SELECT Customer_Name FROM Customer_Table WHERE Customer_Number NOT IN (SELECT Customer_Number FROM Order_Table ) ;
How many rows return from the query now that a NULL value is in a Customer_Number? We really didn’t place a new row inside the Order_Table with a NULL value for the Customer_Number column, but in theory, if we had, how many rows would return? Page 299
Chapter 9
Sub-query Functions
Answer – How many rows return on a NOT IN with a NULL? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777 000099
11111111 11111111 31323134 87323456 57896883 NULL
We added a Null Value to the Order_Table
12347.53 8005.91 5111.47 15231.62 23454.84 9999.99 NULL
SELECT Customer_Name FROM Customer_Table WHERE Customer_Number NOT IN (SELECT Customer_Number FROM Order_Table ) ;
How many rows return from the query now that a NULL value is in a Customer_Number? ZERO rows will return
The answer is no rows come back. This is because when you have a NULL value in a NOT IN list, the system doesn’t know the value of NULL, so it returns nothing. Page 300
Chapter 9
Sub-query Functions
How to handle a NOT IN with Potential NULL Values Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777 000099
11111111 11111111 31323134 87323456 57896883 NULL
We added a Null Value to the Order_Table
12347.53 8005.91 5111.47 15231.62 23454.84 9999.99 NULL
SELECT Customer_Name FROM Customer_Table WHERE Customer_Number NOT IN (SELECT Customer_Number FROM Order_Table WHERE Customer_Number IS NOT NULL) ;
How many rows return NOW from the query? 1 Acme Products
You can utilize a WHERE clause that tests to make sure Customer_Number IS NOT NULL. This should be used when a NOT IN could encounter a NULL. Page 301
Chapter 9
Sub-query Functions
Using a Correlated Exists Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Use EXISTS to find which Customers have placed an Order?
SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ; The EXISTS command will determine via a Boolean if something is True or False. If a customer placed an order, it EXISTS, and using the Correlated Exists statement, only customers who have placed an order will return in the answer set. EXISTS is different than IN as it is less restrictive as you will soon understand. Page 302
Chapter 9
Sub-query Functions
How a Correlated Exists matches up Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Does not Acme Products Exist in ACE Consulting Order_Table XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ; Customer_Number ________________
________________ Customer_Name
11111111 31323134 57896883 87323456
Billy’s Best Choice ACE Consulting XYZ Plumbing Databases N-U
Only customers who placed an order return with the above Correlated EXISTS. Page 303
Chapter 9
Sub-query Functions
The Correlated NOT Exists Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Use NOT EXISTS to find which Customers have NOT placed an Order? SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE NOT EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ;
The EXISTS command will determine via a Boolean if something is True or False. If a customer placed an order, it EXISTS, and using the Correlated Exists statement, only customers who have placed an order will return in the answer set. EXISTS is different than IN as it is less restrictive as you will soon understand. Page 304
Chapter 9
Sub-query Functions
The Correlated NOT Exists Answer Set Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
12347.53 8005.91 5111.47 15231.62 23454.84
Use NOT EXISTS to find which Customers have NOT placed an Order? SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE NOT EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ;
Customer_Number Customer_Name ________________ ______________ 31313131
Acme Products
The only customer who did NOT place an order was Acme Products.
Page 305
Chapter 9
Sub-query Functions
Quiz – How many rows come back from this NOT Exists? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777 000099
11111111 11111111 31323134 87323456 57896883 NULL
We added a Null Value to the Order_Table
12347.53 8005.91 5111.47 15231.62 23454.84 9999.99 NULL
SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE NOT EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ;
How many rows return from the query?
A NULL value in a list for queries with NOT IN returned nothing, but you must now decide if that is also true for the NOT EXISTS. How many rows will return? Page 306
Chapter 9
Sub-query Functions
Answer – How many rows come back from this NOT Exists? Customer_Table
Order_Table
Customer_Number Customer_Name Order_Number ______________ Customer_Number _________ Order_Total _____________ ______________ ___________ 11111111 31313131 31323134 57896883 87323456
Billy’s Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
123456 123512 123552 123585 123777 000099
11111111 11111111 31323134 87323456 57896883 NULL
We added a Null Value to the Order_Table
12347.53 8005.91 5111.47 15231.62 23454.84 9999.99 NULL
SELECT Customer_Number, Customer_Name FROM Customer_Table as Top1 WHERE NOT EXISTS (SELECT * FROM Order_Table as Bot1 Where Top1.Customer_Number = Bot1.Customer_Number ) ; How many rows return from the query? One row Acme Products
NOT EXISTS is unaffected by a NULL in the list, that’s why it is more flexible! Page 307
Chapter 10
Page 308
Window Functions OLAP
Chapter 10
Window Functions OLAP
Chapter 10 – Window Functions OLAP
“Don’t count the days, make the days count.” - Mohammad Ali
Page 309
Chapter 10
Window Functions OLAP
The Row_Number Command SELECT Product_ID ,Sale_Date , Daily_Sales, ROW_NUMBER() OVER (ORDER BY Product_ID, Sale_Date) AS Seq_Number FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID __________ Sale_Date ________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01
Daily_Sales ___________ Seq_Number _________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29
1 2 3 4 5 6 7 8 9 10 11
The ROW_NUMBER() Keyword(s) caused Seq_Number to increase sequentially. Notice that this does NOT have a Rows Unbounded Preceding, and it still works! Page 310
Chapter 10
Window Functions OLAP
Quiz – How did the Row_Number Reset? SELECT Product_ID ,Sale_Date , Daily_Sales, ROW_NUMBER() OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ) AS StartOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID ________ Sale_Date ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
What Keyword(s) caused StartOver to reset? Page 311
Daily_Sales _________
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50 .
StartOver _______
1 2 3 4 5 6 7 1 2 3 4 5 6 7
Chapter 10
Window Functions OLAP
Quiz – How did the Row_Number Reset? SELECT Product_ID ,Sale_Date , Daily_Sales, ROW_NUMBER() OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ) AS StartOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID ________
Sale_Date ________
Daily_Sales _________
StartOver _______
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
1 2 3 4 5 6 7 1 2 3 4 5 6 7
What Keyword(s) caused StartOver to reset? It is the PARTITION BY statement. Page 312
Chapter 10
Window Functions OLAP
Using a Derived Table and Row_Number WITH Results AS ( SELECT ROW_NUMBER() OVER(ORDER BY Product_ID, Sale_Date) AS RowNumber, Product_ID, Sale_Date FROM Sales_Table ) SELECT * FROM Results WHERE RowNumber BETWEEN 8 AND 14 RowNumber __________ Product_ID _________ Sale_Date _________ 8 2000 2000-09-28 9 2000 2000-09-29 10 2000 2000-09-30 11 2000 2000-10-01 12 2000 2000-10-02 13 2000 2000-10-03 14 2000 2000-10-04
In the example above we are using a derived table called Results and then using a WHERE clause to only take certain RowNumbers. Page 313
Chapter 10
Window Functions OLAP
Ordered Analytics OVER SELECT TOP (9) Product_ID as Prod ,Sale_Date ,Daily_Sales ,SUM(Daily_Sales) OVER(PARTITION BY Sale_Date) AS Total ,AVG(Daily_Sales) OVER(PARTITION BY Sale_Date) AS Avg ,COUNT(Daily_Sales) OVER(PARTITION BY Sale_Date) AS Cnt ,MIN(Daily_Sales) OVER(PARTITION BY Sale_Date) AS Min ,MAX(Daily_Sales) OVER(PARTITION BY Sale_Date) AS Max FROM Sales_Table Prod ____ 1000 2000 3000 3000 2000 1000 1000 2000 3000
Sale_Date __________ Daily_Sales ________ Total Avg Cnt Min _________ ________ ___ ________ 2000-09-28 48850.40 152041.05 50680.35 3 41888.88 2000-09-28 41888.88 152041.05 50680.35 3 41888.88 2000-09-28 61301.77 152041.05 50680.35 3 41888.88 2000-09-29 34509.13 137009.35 45669.78 3 34509.13 2000-09-29 48000.00 137009.35 45669.78 3 34509.13 2000-09-29 54500.22 137009.35 45669.78 3 34509.13 2000-09-30 36000.07 129718.96 43239.65 3 36000.07 2000-09-30 49850.03 129718.96 43239.65 3 36000.07 2000-09-30 43868.86 129718.96 43239.65 3 36000.07
Above is an example of the Ordered Analytics using the keyword OVER.
Page 314
Max _______ 61301.77 61301.77 61301.77 54500.22 54500.22 54500.22 49850.03 49850.03 49850.03
Chapter 10
Window Functions OLAP
RANK and DENSE RANK SELECT TOP (5) Product_ID, Daily_Sales, RANK() OVER (ORDER BY Daily_Sales ASC) as [Rank], DENSE_RANK() OVER(Order By Daily_Sales ASC) as [DenseRank] FROM Sales_Table WHERE Product_ID in(1000, 2000)
Prod ____ 1000 2000 1000 2000 1000
Daily_Sales Rank DenseRank __________ _____ __________ 32800.50 1 1 32800.50 1 1 36000.07 3 2 36021.93 4 3 40200.43 5 4
Above is an example of the RANK and DENSE_RANK commands. Notice the difference in the ties and the next ranking. Page 315
Chapter 10
Window Functions OLAP
RANK Defaults to Ascending Order SELECT Product_ID ,Sale_Date , Daily_Sales, RANK() OVER (ORDER BY Daily_Sales) AS Rank1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID _________
Sale_Date ________
1000 2000 1000 2000 1000 Not all 2000 rows 2000 are displayed 2000 1000 2000 1000 1000 2000
10/02/2000 10/04/2000 09/30/2000 10/02/2000 10/01/2000 09/28/2000 10/03/2000 09/29/2000 09/28/2000 09/30/2000 09/29/2000 10/04/2000 10/01/2000
The RANK() OVER command defaults the Sort to ASC. Page 316
Daily_Sales Rank1 _________ _____ 1 32800.50 1 32800.50 3 36000.07 4 36021.93 5 40200.43 6 41888.88 7 43200.18 8 48000.00 9 48850.40 10 49850.03 11 54500.22 12 54553.10 13 54850.29
Chapter 10
Window Functions OLAP
Getting RANK to Sort in DESC Order SELECT Product_ID ,Sale_Date , Daily_Sales, RANK() OVER (ORDER BY Daily_Sales DESC) AS Rank1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID _________ 1000 2000 1000 1000 2000 1000 2000 2000 2000 1000 2000 1000 2000 1000
Sale_Date ________
Daily_Sales _________
10/03/2000 10/01/2000 10/04/2000 09/29/2000 09/30/2000 09/28/2000 09/29/2000 10/03/2000 09/28/2000 10/01/2000 10/02/2000 09/30/2000 10/04/2000 10/02/2000
64300.00 54850.29 54553.10 54500.22 49850.03 48850.40 48000.00 43200.18 41888.88 40200.43 36021.93 36000.07 32800.50 32800.50
Rank1 _____ 1 2 3 4 5 6 7 8 9 10 11 12 13 13
Utilize the DESC keyword in the ORDER BY statement to rank in descending order. Page 317
Chapter 10
Window Functions OLAP
RANK() OVER and PARTITION BY SELECT Product_ID ,Sale_Date , Daily_Sales, RANK() OVER (PARTITION BY Product_ID ORDER BY Daily_Sales DESC) AS Rank1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID ________ Sale_Date ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
10/03/2000 10/04/2000 09/29/2000 09/28/2000 10/01/2000 09/30/2000 10/02/2000 10/01/2000 09/30/2000 09/29/2000 10/03/2000 09/28/2000 10/02/2000 10/04/2000
Daily_Sales Rank1 _________ _____ 64300.00 54553.10 54500.22 48850.40 40200.43 36000.07 32800.50 54850.29 49850.03 48000.00 43200.18 41888.88 36021.93 32800.50
1 2 3 4 5 6 7 1 2 3 4 5 6 7
What does the PARTITION Statement in the RANK() OVER do? It resets the rank. Page 318
Chapter 10
Window Functions OLAP
CSUM – Rows Unbounded Preceding Explained SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Sale_Date ROWS UNBOUNDED PRECEDING) AS CsumAnsi FROM Sales_Table WHERE Product_ID BETWEEN 1000 and 2000 ;
Product_ID Sale_Date ___________ Daily_Sales __________ _________ 2000 2000-09-28 41888.88 1000 2000-09-28 48850.40 2000 2000-09-29 48000.00 Not all rows 1000 2000-09-29 54500.22 are displayed 1000 2000-09-30 36000.07 in this 49850.03 answer set 2000 2000-09-30 1000 2000-10-01 40200.43 2000 2000-10-01 54850.29 1000 2000-10-02 32800.50 2000 2000-10-02 36021.93
CsumAnsi ________ 41888.88 90739.28 138739.28 193239.50 229239.57 279089.60 319290.03 374140.32 406940.82 442962.75
The keywords Rows Unbounded Preceding determines that this is a cumulative sum (CSUM). There are only a few different statements and Rows Unbounded Preceding is the main one. It means start calculating at the beginning row, and continue calculating until the last row. Page 319
Chapter 10
Window Functions OLAP
CSUM – Making Sense of the Data SELECT
Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Sale_Date ROWS UNBOUNDED PRECEDING) AS SUMOVER FROM Sales_Table WHERE Product_ID BETWEEN 1000 and 2000 ;
Product_ID ________ Sale_Date ________
Not all rows are displayed in this answer set
2000 1000 2000 1000 1000 2000 1000 2000 1000 2000 1000
2000-09-28 2000-09-28 2000-09-29 2000-09-29 2000-09-30 2000-09-30 2000-10-01 2000-10-01 2000-10-02 2000-10-02 2000-10-03
Daily_Sales _________ 41888.88 48850.40 48000.00 54500.22 36000.07 49850.03 40200.43 54850.29 32800.50 36021.93 64300.00
SUMOVER _________ 41888.88 90739.28 138739.28 193239.50 229239.57 279089.60 319290.03 374140.32 406940.82 442962.75 507262.75
The second “SUMOVER” row is 90739.28. That is derived by the first row’s Daily_Sales (41888.88) added to the SECOND row’s Daily_Sales (48850.40). Page 320
Chapter 10
Window Functions OLAP
CSUM – Making Even More Sense of the Data SELECT
Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Sale_Date ROWS UNBOUNDED PRECEDING) AS SUMOVER FROM Sales_Table WHERE Product_ID BETWEEN 1000 and 2000 ; Product_ID Sale_Date _________ Daily_Sales _________ SUMOVER _________ _________
Not all rows are displayed in this answer set
2000 1000 2000 1000 1000 2000 1000 2000 1000 2000 1000
2000-09-28 2000-09-28 2000-09-29 2000-09-29 2000-09-30 2000-09-30 2000-10-01 2000-10-01 2000-10-02 2000-10-02 2000-10-03
41888.88 48850.40 48000.00 54500.22 36000.07 49850.03 40200.43 54850.29 32800.50 36021.93 64300.00
41888.88 90739.28 138739.28 193239.50 229239.57 279089.60 319290.03 374140.32 406940.82 442962.75 507262.75
The third “SUMOVER” row is 138739.28. That is derived by taking the first row’s Daily_Sales (41888.88) and adding it to the SECOND row’s Daily_Sales (48850.40). Then, you add that total to the THIRD row’s Daily_Sales (48000.00). Page 321
Chapter 10
Window Functions OLAP
CSUM – The Major and Minor Sort Key(s) SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS SumOVER FROM Sales_Table ;
Product_ID Sale_Date Daily_Sales _________ SumOVER ________ _________ _________ 1000 2000-09-28 48850.40 48850.40 1000 2000-09-29 54500.22 103350.62 1000 2000-09-30 36000.07 139350.69 1000 2000-10-01 40200.43 179551.12 Not all rows are displayed 1000 2000-10-02 32800.50 212351.62 in this 1000 2000-10-03 64300.00 276651.62 answer set 1000 2000-10-04 54553.10 331204.72 2000 2000-09-28 41888.88 373093.60 2000 2000-09-29 48000.00 421093.60 2000 2000-09-30 49850.03 470943.63 2000 2000-10-01 54850.29 525793.92
You can have more than one SORT KEY. In the top query, Product_ID is the MAJOR Sort, and Sale_Date is the MINOR Sort. Remember, the data is sorted first and then the cumulative sum is calculated. That is why they are called Ordered Analytics. Page 322
Chapter 10
Window Functions OLAP
The ANSI CSUM – Getting a Sequential Number SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) as SUMOVER, SUM(1) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS Seq_Number FROM Sales_Table ; Product_ID Daily_Sales ___________ SUM OVER ___________ Seq_Number __________ Sale_Date _________ __________ 1000 1000 Not all rows 1000 are displayed 1000 in this 1000 answer set 1000 1000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 373093.60 421093.60 470943.63
1 2 3 4 5 6 7 8 9 10
With “Seq_Number”, because you placed the number 1 in the area which calculates the cumulative sum, it’ll continuously add 1 to the answer for each row.
Page 323
Chapter 10
Window Functions OLAP
Troubleshooting The ANSI OLAP on a GROUP BY SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Sale_Date ROWS UNBOUNDED PRECEDING) AS AnsiCsum FROM Sales_Table GROUP BY Product_ID ; Error! Why?
Never GROUP BY in a SUM()Over or with any ANSI Syntax OLAP command. If you want to reset you use a PARTITION BY Statement, but never a GROUP BY.
Page 324
Chapter 10
Window Functions OLAP
Reset with a PARTITION BY Statement SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS SumANSI FROM Sales_Table ;
Product_ID Sale_Date ________ ________
Not all rows are displayed in this answer set
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
Daily_Sales SumANSI _________ ________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 41888.88 89888.88 139738.91
CSUM Resets on Product_ID break
The PARTITION Statement is how you reset in ANSI. This will cause the SUMANSI to start over (reset) on its calculating for each NEW Product_ID. Page 325
Chapter 10
Window Functions OLAP
PARTITION BY only Resets a Single OLAP not ALL of them SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS Subtotal, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS GrandTotal FROM Sales_Table ; Product_ID ________ Sale_Date Daily_Sales Subtotal GrandTotal _________ _________ ________ ________
Not all rows are displayed in this answer set
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 41888.88 89888.88 139738.91
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 373093.60 421093.60 470943.63
Above are two OLAP statements. Only one has PARTITION BY, so only it resets. The other continuously does a CSUM. Page 326
Chapter 10
Window Functions OLAP
CURRENT ROW AND UNBOUNDED FOLLOWING SELECT Product_ID, Sale_Date ,Daily_Sales ,SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS CumulativeTotal FROM Sales_Table
Product_ID ________ Sale_Date Daily_Sales CumulativeTotal _________ _________ ____________
Not all rows are displayed in this answer set
3000 3000 3000 3000 3000 3000 3000 2000 2000 2000 2000
10/04/2000 10/03/2000 10/02/2000 10/01/2000 09/30/2000 09/29/2000 09/28/2000 10/04/2000 10/03/2000 10/02/2000 10/01/2000
15675.33 21553.79 19678.94 28000.00 43868.86 34509.13 61301.77 32800.50 43200.18 36021.93 54850.29
15675.33 37229.12 56908.06 84908.06 128776.92 163286.05 224587.82 257388.32 300588.50 336610.43 391460.72
Above we used the ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING to produce a CSUM, but notice that the Product_ID and the Sale_Date are reversed. We see the Product_ID of 3000 first and the latest date first. Page 327
Chapter 10
Window Functions OLAP
Different Windowing Options SELECT Product_ID, Sale_Date, Daily_Sales ,SUM(Daily_Sales) OVER( PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW ) as Row_Preceding ,SUM(Daily_Sales) OVER( PARTITION BY Product_ID ORDER BY Product_Id, Sale_Date ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) as Row_Following FROM Sales_Table Product_ID ________ Sale_Date Daily_Sales Row_Preceding Row_Following _________ _________ ____________ ___________
Not all rows are displayed in this answer set
1000 1000 1000 1000 1000 1000 1000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00
48850.40 103350.62 90500.29 76200.50 73000.93 97100.50 118853.10 41888.88 89888.88
103350.62 90500.29 76200.50 73000.93 97100.50 118853.10 54553.10 89888.88 97850.03
The example above uses ROWS BETWEEN 1 PRECEDING AND CURRENT ROW and then it uses a different example with ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING. Notice how the report came out? Page 328
Chapter 10
Window Functions OLAP
Moving Sum has a Moving Window SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding)AS Sum3_ANSI FROM Sales_Table ; Moving Window of 3 rows
Calculate the Current Row and 2 rows preceding
Product_ID ________ Sale_Date Daily_Sales _________ _________ Sum3_ANSI _________
Not all rows are displayed in this answer set
1000 1000 1000 1000 1000 1000 1000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33
The SUM () Over allows you to get the moving SUM of a certain column. The moving window in ANSI form always includes the current row. A Rows 2 Preceding statement means the current row and two preceding, which is a moving window of 3. Page 329
Chapter 10
Window Functions OLAP
How ANSI Moving SUM Handles the Sort SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS Sum3_ANSI FROM Sales_Table ; Major and Minor Sort keys Product_ID _________ Sale_Date _________ Daily_Sales _________
Not all rows are displayed in this answer set
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
The SUM OVER places the sort after the ORDER BY. Page 330
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
Sum3_AN SI __________
48850.40 103350.62 139350.69 130700.72 109001.00 137300.93 151653.60 160741.98 144441.98 139738.91 152700.32 140722.25
Chapter 10
Window Functions OLAP
Quiz – How is that Total Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS Sum3_ANSI FROM Sales_Table ; Product_ID ________
Sale_Date _________
Daily_Sales _________
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
Not all rows are displayed in this answer set
Sum3_ANSI __________ 48850.40 103350.62 139350.69 130700.72 109001.00 137300.93 151653.60 160741.98 144441.98 139738.91 152700.32 140722.25
With a Moving Window of 3, how is the 139350.69 amount derived in the Sum3_ANSI column in the third row? Page 331
Chapter 10
Window Functions OLAP
Answer to Quiz – How is that Total Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS Sum3_ANSI FROM Sales_Table ; Product_ID ________
Sale_Date _________
Daily_Sales _________
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
Not all rows are displayed in this answer set
Sum3_ANSI __________ 48850.40 103350.62 139350.69 130700.72 109001.00 137300.93 151653.60 160741.98 144441.98 139738.91 152700.32 140722.25
With a Moving Window of 3, how is the 139350.69 amount derived in the Sum3_ANSI column in the third row? It is the sum of 48850.40, 54500.22 and 36000.07. The current row of Daily_Sales plus the previous two rows of Daily_Sales.
Page 332
Chapter 10
Window Functions OLAP
Moving SUM every 3-rows Vs a Continuous Average SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS SUM3, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED Preceding) AS Continuous FROM Sales_Table; Product_ID ________ Sale_Date _________ Daily_Sales ________ SUM3 Continuous _________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00
48850.40 103350.62 139350.69 130700.72 109001.00 137300.93 151653.60 160741.98 144441.98
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 373093.60 421093.60
Not all rows are displayed in this answer set
The ROWS 2 Preceding gives the MSUM for every 3 rows. The ROWS UNBOUNDED Preceding gives the continuous MSUM. Page 333
Chapter 10
Window Functions OLAP
Partition By Resets an ANSI OLAP SELECT Product_ID , Sale_Date, Daily_Sales, SUM(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS SUM3, SUM(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ANSI reset ROWS UNBOUNDED Preceding) AS Continuous much Like a FROM Sales_Table; GROUP BY Product_ID __________ 1000 1000 1000 Not all 1000 rows are 1000 displayed 1000 1000 2000 2000
Sale_Date Daily_Sales _________ SUM3 __________ __________ 2000-09-28 48850.40 48850.40 2000-09-29 54500.22 103350.62 2000-09-30 36000.07 139350.69 2000-10-01 40200.43 130700.72 2000-10-02 32800.50 109001.00 2000-10-03 64300.00 137300.93 2000-10-04 54553.10 151653.60 2000-09-28 41888.88 160741.98 2000-09-29 48000.00 144441.98
Continuous __________ 48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 41888.88 89888.88
Use a PARTITION BY Statement to Reset the ANSI OLAP. Notice it only resets the OLAP command containing the Partition By statement, but not the other OLAPs. Page 334
Chapter 10
Window Functions OLAP
The Moving Window is Current Row and Preceding SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding)AS AVG_3_ANSI FROM Sales_Table ; Moving Window of 3 rows
Calculate the Current Row and 2 rows preceding
Product_ID Sale_Date _________ Daily_Sales AVG_3_ANSI _________ _________ ___________ 48850.40 1000 2000-09-28 48850.40 54500.22 1000 2000-09-29 51675.31 36000.07 1000 2000-09-30 46450.23 40200.43 1000 2000-10-01 43566.91 Not all rows 32800.50 1000 2000-10-02 36333.67 are 64300.00 1000 2000-10-03 45788.98 displayed 54553.10 1000 2000-10-04 50551.20 41888.88 2000 2000-09-28 53580.66 48000.00 2000 2000-09-29 48147.33 49850.03 2000 2000-09-30 46579.11
The AVG () Over allows you to get the moving average of a certain column. The Rows 2 Preceding is a moving window of 3 in ANSI. Page 335
Chapter 10
Window Functions OLAP
How Moving Average Handles the Sort SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3_ANSI FROM Sales_Table ; Major and Minor Sort keys
Product_ID ________ Sale_Date _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
Daily_Sales ___________ AVG_3_ANSI ________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33 46579.11 50900.11 46907.42
Much like the SUM OVER Command, the Average OVER places the sort keys via the ORDER BY keywords. Page 336
Chapter 10
Window Functions OLAP
Moving Average SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3 FROM Sales_Table ; Product_ID ________ Sale_Date _________ Daily_Sales _________
AVG_3 __________
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
48850.400000 51675.310000 46450.230000 43566.906666 36333.666666 45766.976666 50551.200000 53580.660000 48147.326666 46579.636666
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
Notice the Moving Window of 3 in the syntax and that it is a ROWS 2 Preceding. That is because in ANSI, it is considered the Current Row and 2 preceding. You could get microseconds in the moving average. The next page will use the CAST command to provide a precision of 2 decimal places. Page 337
Chapter 10
Window Functions OLAP
Moving Average SELECT Product_ID , Sale_Date, Daily_Sales, CAST(AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) as Decimal (8,2)) AS AVG_3 FROM Sales_Table ;
Product_ID ________ Sale_Date _________ Daily_Sales _______ AVG_3 _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
48850.40 51675.31 46450.23 43566.91 36333.67 45766.98 50551.20 53580.66 48147.33 46579.64
Notice the Moving Window of 3 in the syntax and that it is a 2 in the ANSI version. That is because in ANSI, it is considered the Current Row and 2 preceding. Page 338
Chapter 10
Window Functions OLAP
Quiz – How is that Total Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3_ANSI FROM Sales_Table ; Product_ID _________
Sale_Date _________
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
Not all rows are displayed
Daily_Sales ___________ AVG_3_ANSI _________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33 46579.11 50900.11 46907.42
With a Moving Window of 3, how is the 46450.23 amount derived in the AVG_3_ANSI column in the third row?
Page 339
Chapter 10
Window Functions OLAP
Answer to Quiz – How is that Total Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3_ANSI FROM Sales_Table ; Product_ID Sale_Date _________ Daily_Sales ___________ AVG_3_ANSI ________ ________ 1000 2000-09-28 48850.40 48850.40 2000-09-29 1000 51675.31 54500.22 2000-09-30 1000 46450.23 36000.07 1000 2000-10-01 43566.91 40200.43 Not all 1000 2000-10-02 36333.67 32800.50 rows 1000 2000-10-03 45788.98 64300.00 are 1000 2000-10-04 50551.20 54553.10 displayed 2000 2000-09-28 53580.66 41888.88 2000 2000-09-29 48147.33 48000.00 2000 2000-09-30 46579.11 49850.03 2000 2000-10-01 50900.11 54850.29 2000 2000-10-02 46907.42 36021.93 AVG of 48850.40, 54500.22, and 36000.07
With a Moving Window of 3, the 46450.23 amount derived in the third row is the average of 48850.40, 54500.22, and 36000.07.
Page 340
Chapter 10
Window Functions OLAP
Quiz – How is that 4th Row Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3_ANSI FROM Sales_Table ;
Product_ID _________ Sale_Date Daily_Sales ___________ AVG_3_ANSI ________ _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33 46579.11 50900.11 46907.42
With a Moving Window of 3, how is the 43566.91 amount derived in the AVG_3_ANSI column in the fourth row? Page 341
Chapter 10
Window Functions OLAP
Answer to Quiz – How is that 4th Row Calculated? SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG_3_ANSI FROM Sales_Table ; Product_ID _________ Sale_Date _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
Daily_Sales AVG_3_ANSI _________ __________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33 46579.11 50900.11 46907.42
AVG of 54500.22, 36000.07 and 40200.43
With a Moving Window of 3, how is the 43566.91 amount derived in the AVG_3_ANSI column in the fourth row? The current row plus Rows 2 Preceding. Page 342
Chapter 10
Window Functions OLAP
Moving Average every 3-rows Vs a Continuous Average SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG3, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED Preceding) AS Continuous FROM Sales_Table;
Product_ID Sale_Date Daily_Sales _______ AVG3 Continuous _________ _________ _________ _________ 1000 1000 1000 Not all rows 1000 are 1000 displayed 1000 1000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33
48850.40 51675.31 46450.23 44887.78 42470.32 46108.60 47314.96 46636.70 46788.18
The ROWS 2 Preceding gives the MAVG for every 3 rows. The ROWS UNBOUNDED Preceding gives the continuous MAVG. Page 343
Chapter 10
Window Functions OLAP
Partition By Resets an ANSI OLAP SELECT Product_ID , Sale_Date, Daily_Sales, AVG(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS 2 Preceding) AS AVG3, AVG(Daily_Sales) OVER (PARTITION BY Product_ID ANSI reset ORDER BY Product_ID, Sale_Date much Like a ROWS UNBOUNDED Preceding) AS Continuous GROUP BY FROM Sales_Table; Product_ID Sale_Date Daily_Sales _______ AVG3 Continuous _________ _________ _________ _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00
48850.40 51675.31 46450.23 43566.91 36333.67 45788.98 50551.20 53580.66 48147.33
48850.40 51675.31 46450.23 44887.78 42470.32 46108.60 47314.96 41888.88 44944.44
Use a PARTITION BY Statement to Reset the ANSI OLAP. The Partition By statement only resets the column using the statement. Notice that only Continuous resets. Page 344
Chapter 10
Window Functions OLAP
Moving Difference using ANSI Syntax SELECT Product_ID, Sale_Date, Daily_Sales, Daily_Sales - SUM(Daily_Sales) OVER ( ORDER BY Product_ID ASC, Sale_Date ASC ROWS BETWEEN 4 PRECEDING AND 4 PRECEDING) AS "MDiff_ANSI" FROM Sales_Table ; Product_ID _________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000
Sale_Date __________ Daily_Sales MDiff_ANSI _________ __________ 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29
? ? ? ? -16049.90 9799.78 18553.03 1688.45 15199.50 -14449.97 297.19
This is how you do a MDiff using the ANSI Syntax with a moving window of 4. Page 345
Chapter 10
Window Functions OLAP
Moving Difference using ANSI Syntax with Partition By SELECT Product_ID, Sale_Date, Daily_Sales, Daily_Sales - SUM(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID ASC, Sale_Date ASC ROWS BETWEEN 4 PRECEDING AND 4 PRECEDING) AS "MDiff_ANSI" FROM Sales_Table; Product_ID _________ Sale_Date __________ Daily_Sales ___________ MDiff_ANSI __________ Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93
? ? ? ? -16049.90 9799.78 18553.03 ? ? ? ? -5866.95
Wow! This is how you do a MDiff using the ANSI Syntax with a moving window of 4 and with a PARTITION BY statement. Page 346
Chapter 10
Window Functions OLAP
COUNT OVER for a Sequential Number SELECT Product_ID ,Sale_Date , Daily_Sales, COUNT(*) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS Seq_Number FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID _________ Sale_Date _________ Daily_Sales Seq_Number ________ __________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29
1 2 3 4 5 6 7 8 9 10 11
This is the COUNT OVER. It will provide a sequential number starting at 1. The Keyword(s) ROWS UNBOUNDED PRECEDING causes Seq_Number to start at the beginning and increase sequentially to the end. Page 347
Chapter 10
Window Functions OLAP
COUNT OVER without Rows Unbounded Preceding SELECT Product_ID ,Sale_Date , Daily_Sales, COUNT(*) OVER (ORDER BY Product_ID, Sale_Date) AS No_Seq FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date __________ Daily_Sales _______ No_Seq __________ _________ 48850.40 1 1000 2000-09-28 54500.22 2 1000 2000-09-29 36000.07 3 1000 2000-09-30 40200.43 4 1000 2000-10-01 32800.50 5 1000 2000-10-02 64300.00 6 1000 2000-10-03 54553.10 7 14 rows 1000 2000-10-04 came 41888.88 8 2000 2000-09-28 back 48000.00 9 2000 2000-09-29 49850.03 10 2000 2000-09-30 54850.29 11 2000 2000-10-01 36021.93 12 2000 2000-10-02 43200.18 13 2000 2000-10-03 32800.50 14 2000 2000-10-04 When you don’t have a ROWS UNBOUNDED PRECEDING this still works just fine. Page 348
Chapter 10
Window Functions OLAP
Quiz – What caused the COUNT OVER to Reset? SELECT Product_ID ,Sale_Date , Daily_Sales, COUNT(*) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS StartOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID _________ Sale_Date _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
What Keyword(s) caused StartOver to reset? Page 349
Daily_Sales _________
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
StartOver _______
1 2 3 4 5 6 7 1 2 3 4 5 6 7
Chapter 10
Window Functions OLAP
Answer to Quiz – What caused the COUNT OVER to Reset? SELECT Product_ID ,Sale_Date , Daily_Sales, COUNT(*) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS StartOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID ________ Sale_Date ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
Daily_Sales _________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
StartOver _______ 1 2 3 4 5 6 7 1 2 3 4 5 6 7
What Keyword(s) caused StartOver to reset? It is the PARTITION BY statement. Page 350
Chapter 10
Window Functions OLAP
The MAX OVER Command SELECT Product_ID ,Sale_Date , Daily_Sales, MAX(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS MaxOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID ________ Sale_Date _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
Daily_Sales _________
MaxOver _______
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
48850.40 54500.22 54500.22 54500.22 54500.22 64300.00 64300.00 64300.00 64300.00 64300.00 64300.00 64300.00 64300.00 64300.00
After the sort, the Max() Over shows the Max Value up to that point. Page 351
Chapter 10
Window Functions OLAP
MAX OVER with PARTITION BY Reset SELECT Product_ID ,Sale_Date , Daily_Sales, MAX(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS MaxOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID ________ Sale_Date _________ 1000 1000 1000 1000 Not all 1000 rows 1000 are displayed 1000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01
Daily_Sales _________
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29
MaxOver ________
48850.40 54500.22 54500.22 54500.22 54500.22 64300.00 64300.00 41888.88 48000.00 49850.03 54850.29
The largest value is 64300.00 in the column MaxOver. Once it was evaluated, it did not continue until the end because of the PARTITION BY reset. Page 352
Chapter 10
Window Functions OLAP
MAX OVER Without Rows Unbounded Preceding SELECT Product_ID ,Sale_Date , Daily_Sales, MAX(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ) AS MaxOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Daily_Sales ________ MaxOver __________ Sale_Date ________ __________
Not all rows are displayed
1000 1000 1000 1000 1000 1000 1000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03
You don't need the Rows Unbounded Preceding with the MAX OVER. Page 353
48850.40 54500.22 54500.22 54500.22 54500.22 64300.00 64300.00 64300.00 64300.00 64300.00
Chapter 10
Window Functions OLAP
The MIN OVER Command SELECT Product_ID, Sale_Date ,Daily_Sales ,MIN(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS MinOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
Sale_Date ________ 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
Daily_Sales _________
MinOver _______
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
48850.40 48850.40 36000.07 36000.07 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50
After the sort, the MIN () Over shows the Max Value up to that point. Page 354
Chapter 10
Window Functions OLAP
Troubleshooting MIN OVER SELECT Product_ID ,Sale_Date , Daily_Sales, MIN(Daily_Sales) OVER (ORDER BY Product_ID, Sale_Date ) AS MinOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID __________ Sale_Date _________ Daily_Sales __________ 48850.40 1000 2000-09-28 54500.22 1000 2000-09-29 36000.07 1000 2000-09-30 40200.43 1000 2000-10-01 Not all rows 32800.50 1000 2000-10-02 are 64300.00 1000 2000-10-03 displayed 1000 2000-10-04 54553.10 41888.88 2000 2000-09-28 48000.00 2000 2000-09-29 49850.03 2000 2000-09-30 54850.29 2000 2000-10-01 You don't need the Rows Unbounded Preceding with the MIN OVER Page 355
MinOver ________ 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50 32800.50
Chapter 10
Window Functions OLAP
Finding a Value of a Column in the Next Row with MIN SELECT Product_ID, Sale_Date, Daily_Sales, MIN(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS BETWEEN 1 Following and 1 Following) AS NextSale FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID __________ 1000 1000 1000 1000 Not all rows 1000 are 1000 displayed 1000 2000 2000 2000 2000
Sale_Date Daily_Sales _________ __________ 48850.40 09/28/2000 54500.22 09/29/2000 36000.07 09/30/2000 40200.43 10/01/2000 32800.50 10/02/2000 64300.00 10/03/2000 54553.10 10/04/2000 41888.88 09/28/2000 48000.00 09/29/2000 49850.03 09/30/2000 54850.29 10/01/2000
NextSale ________ 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 ? 48000.00 49850.03 54850.29 36021.93
The above example finds the value of a column in the next row for Daily_Sales. Notice it is partitioned, so there is a Null value at the end of each Product_ID. Page 356
Chapter 10
Window Functions OLAP
The CSUM For Each Product_Id and the Next Start Date SELECT ROW_NUMBER() OVER (PARTITION BY Product_ID ORDER BY Sale_Date) As Rnbr ,Product_Id as PROD ,Sale_Date ,MIN(Sale_Date) OVER (PARTITION BY Product_ID ORDER BY Sale_Date ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) As Next_Start_Dt ,Daily_Sales ,SUM(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Sale_Date ROWS UNBOUNDED PRECEDING) As To_Date_Revenue FROM Sales_Table Rnbr Prod _________ Sale_Date ____________ Next_Start_Dt __________ Daily_Sales To_Date_Revenue ____ ____ ____ ___________ 1 2 3 4 5 6 7 1
1000 1000 1000 1000 1000 1000 1000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000
09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 ? 09/29/2000
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88
Not all rows are displayed
48850.40 103350.62 139350.69 179551.12 212351.62 276651.62 331204.72 41888.88
The above example shows the cumulative SUM for the Daily_Sales and the next date on the same line. Page 357
Chapter 10
Window Functions OLAP
Quiz – Fill in the Blank SELECT Product_ID ,Sale_Date , Daily_Sales, MIN(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS MinOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID _________ Sale_Date Daily_Sales MinOver ________ _________ ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
The last two answers (MinOver) are blank, so you can fill in the blank. Page 358
48850.40 48850.40 36000.07 36000.07 32800.50 32800.50 32800.50 41888.88 41888.88 41888.88 41888.88 36021.93
Chapter 10
Window Functions OLAP
Answer – Fill in the Blank SELECT Product_ID ,Sale_Date , Daily_Sales, MIN(Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Product_ID, Sale_Date ROWS UNBOUNDED PRECEDING) AS MinOver FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04 2000-09-28 2000-09-29 2000-09-30 2000-10-01 2000-10-02 2000-10-03 2000-10-04
The last two answers (MinOver) are filled in. Page 359
Daily_Sales _________
MinOver ________
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
48850.40 48850.40 36000.07 36000.07 32800.50 32800.50 32800.50 41888.88 41888.88 41888.88 41888.88 36021.93 36021.93 32800.50
Chapter 10
Window Functions OLAP
How Ntile Works SELECT Product_ID, Sale_Date, Daily_Sales ,NTILE (4) OVER (ORDER BY Daily_Sales , Sale_Date ) AS "Quartiles" FROM Sales_Table WHERE Product_ID = 1000;
Product_ID Sale_Date __________ Daily_Sales ________ Quartiles __________ _________ 1000 1000 1000 1000 1000 1000 1000
10/02/2000 09/30/2000 10/01/2000 09/28/2000 09/29/2000 10/04/2000 10/03/2000
32800.50 36000.07 40200.43 48850.40 54500.22 54553.10 64300.00
1 1 2 2 3 3 4
Assigning a different value to the indicator of the Ntile function changes the number of partitions established. Each Ntile partition is assigned a number starting at 1 increasing to a value that is one less than the partition number specified. So, with an Ntile of 4 the partitions are 1 through 4. Then, all the rows are distributed as evenly as possible into each partition from highest to lowest values. Normally, extra rows with the lowest value begin back in the lowest numbered partitions. Page 360
Chapter 10
Window Functions OLAP
Ntile SELECT Last_Name, Grade_Pt, NTILE(5) OVER (ORDER BY Grade_Pt) as "Tile" FROM Student_Table ORDER BY "Tile" DESC;
Last_Name Grade_Pt ____ Tile ________ _________ 3.95 5 Bond 4.00 5 Thomas 3.35 4 Delaney 3.80 4 Wilson 2.88 3 Hanson 3.00 3 Phillips 1.90 2 McRoberts 2.00 2 Smith ? 1 Johnson 0.00 1 Larkins
The Ntile function organizes rows into n number of groups. These groups are referred to as tiles. The tile number is returned. For example, the example above has 10 rows, so NTILE(5) splits the 10 rows into five equally sized tiles. There are 2 rows in each tile in the order of the OVER() clause's ORDER BY. Page 361
Chapter 10
Window Functions OLAP
Ntile Continued SELECT Dept_No, EmployeeCount, NTILE(2) OVER (ORDER BY EmployeeCount) as "Tile" FROM (SELECT Dept_No, COUNT(*) as EmployeeCount FROM Employee_Table GROUP BY Dept_No ) AS Q ORDER BY "Tile" DESC; Dept_No ________ EmployeeCount _____________ Tile ____ 1 2 300 2 2 200 3 2 400 1 1 ? 1 1 10 1 1 100 The Ntile function organizes rows into n number of groups. These groups are referred to as tiles. The tile number is returned. For example, the example above has 6 rows, so NTILE(2) splits the 10 rows into 2 equally sized tiles. There are 3 rows in each tile in the order of the OVER() clause's ORDER BY. Page 362
Chapter 10
Window Functions OLAP
Ntile Percentile SELECT Claim_ID, Claim_Date, ClaimCount, NTILE(100) OVER (ORDER BY ClaimCount) as Percentile FROM (SELECT Claim_ID, Claim_Date, COUNT(*) as ClaimCount FROM Claims GROUP BY Claim_ID, Claim_Date ) AS Q ORDER BY Percentile DESC Claim_ID _________ 1302111 4307444 3306333 1304111 2303222 4305444 4303555 3402222 3308333
Claim_Date ClaimCount ___________ __________ 2003-03-01 4 2003-07-05 3 2003-06-28 3 2003-04-28 2 2003-03-12 2 2003-05-12 2 2004-03-01 2 2004-02-28 2 2003-08-01 2
Percentile _________ 26 25 24 23 22 21 20 19 18
Not all rows are displayed
The Ntile function organizes rows into n number of groups. These groups are referred to as tiles. The tile number is returned. Above is a way to get the percentile.
Page 363
Chapter 10
Window Functions OLAP
Another Ntile Example This example determines the percentile for every row in the Sales table based on the daily sales amount and sorts it into sequence by the value being categorized, which here is daily sales. SELECT Product_ID, Sale_Date, Daily_Sales ,NTILE(100) OVER (ORDER BY Daily_Sales) AS "Quantile" FROM Sales_Table WHERE Product_ID < 2000 ;
Product_ID _________ 1000 1000 1000 1000 1000 1000 1000 Above is another Ntile example. Page 364
Sale_Date _________
Daily_Sales ________ Quantile __________
10/02/2000 09/30/2000 10/01/2000 09/28/2000 09/29/2000 10/04/2000 10/03/2000
32800.50 36000.07 40200.43 48850.40 54500.22 54553.10 64300.00
1 2 3 4 5 6 7
Chapter 10
Window Functions OLAP
Using Tertiles (Partitions of Four) SELECT Product_ID, Sale_Date, Daily_Sales ,NTILE (4) OVER (Order by Daily_Sales , Sale_Date ) AS "Quartiles" FROM Sales_Table WHERE Product_ID in (1000, 2000) ;
Product_ID __________ 1000 2000 1000 2000 1000 2000 2000 2000 1000 2000 1000 1000 2000 1000
Sale_Date __________ Daily_Sales ________ Quartiles _________ 10/02/2000 32800.50 1 10/04/2000 32800.50 1 09/30/2000 36000.07 1 10/02/2000 36021.93 1 10/01/2000 40200.43 2 09/28/2000 41888.88 2 10/03/2000 43200.18 2 09/29/2000 48000.00 2 09/28/2000 48850.40 3 09/30/2000 49850.03 3 09/29/2000 54500.22 3 10/04/2000 54553.10 4 10/01/2000 54850.29 4 10/03/2000 64300.00 4
Instead of 100 the example above uses a quartile (QUANTILE based on 4 partitions). Page 365
Chapter 10
Window Functions OLAP
NTILE SELECT Product_ID ,Sale_Date , Daily_Sales, NTILE(4) OVER (ORDER BY Daily_Sales) AS Bucket FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID ________ Sale_Date ________ 1000 2000 1000 1000 2000 1000 2000 2000 2000 1000 2000 1000 1000 2000
10/03/2000 10/01/2000 10/04/2000 09/29/2000 09/30/2000 09/28/2000 09/29/2000 10/03/2000 09/28/2000 10/01/2000 10/02/2000 09/30/2000 10/02/2000 10/04/2000
Daily_Sales _________ 64300.00 54850.29 54553.10 54500.22 49850.03 48850.40 48000.00 43200.18 41888.88 40200.43 36021.93 36000.07 32800.50 32800.50
Bucket ________ 1 1 1 1 2 2 2 2 3 3 3 4 4 4
The NTILE() function divides the rows into buckets as evenly as possible. In this example, because PARTITION BY is omitted, the entire input will be sorted using the ORDER BY clause, and then divided into the number of buckets specified. Page 366
Chapter 10
Window Functions OLAP
NTILE Using a Value of 10 SELECT Product_ID ,Sale_Date , Daily_Sales, NTILE(10) OVER (ORDER BY Daily_Sales) AS Bucket FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date __________ _________ 1000 10/03/2000 2000 10/01/2000 1000 10/04/2000 1000 09/29/2000 2000 09/30/2000 1000 09/28/2000 2000 09/29/2000 2000 10/03/2000 2000 09/28/2000 1000 10/01/2000 2000 10/02/2000 1000 09/30/2000 1000 10/02/2000 2000 10/04/2000
Daily_Sales Bucket __________ _____ 64300.00 54850.29 54553.10 54500.22 49850.03 48850.40 48000.00 43200.18 41888.88 40200.43 36021.93 36000.07 32800.50 32800.50
1 1 2 2 3 3 4 4 5 6 7 8 9 10
The NTILE() function divides the rows into buckets as evenly as possible. In this example, because PARTITION BY is omitted, the entire input will be sorted using the ORDER BY clause, and then divided into the number of buckets specified. This example uses a value of 10 in the NTILE. Page 367
Chapter 10
Window Functions OLAP
NTILE With a Partition SELECT Product_ID ,Sale_Date , Daily_Sales, NTILE(3) OVER (PARTITION BY Product_ID ORDER BY Daily_Sales) AS Bucket FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID Sale_Date Daily_Sales __________ _________ __________ 32800.50 1000 10/02/2000 36000.07 1000 09/30/2000 40200.43 1000 10/01/2000 48850.40 1000 09/28/2000 54500.22 1000 09/29/2000 54553.10 1000 10/04/2000 64300.00 1000 10/03/2000 32800.50 2000 10/04/2000 36021.93 2000 10/02/2000 41888.88 2000 09/28/2000 43200.18 2000 10/03/2000 48000.00 2000 09/29/2000 49850.03 2000 09/30/2000 54850.29 2000 10/01/2000
Bucket ______ 1 1 1 2 2 3 3 1 1 1 2 2 3 3
The NTILE() function divides the rows into buckets as evenly as possible. In this example, because PARTITION BY is listed, the data will first be sorted by Product_ID and then sorted using the ORDER BY clause (within Product_ID), and then divided into the number of buckets specified. This example uses a value of 3 in the NTILE. Notice that the PARTITION BY statement causes the answer set to reset when the Product_ID goes from 1000 to 2000. Page 368
Chapter 10
Window Functions OLAP
Using FIRST_VALUE SELECT Last_name, first_name, dept_no ,FIRST_VALUE(first_name) OVER (ORDER BY dept_no, last_name desc rows unbounded preceding) AS "First All" ,FIRST_VALUE(first_name) OVER (PARTITION BY dept_no ORDER BY dept_no, last_name desc rows unbounded preceding) AS "First Partition" FROM SQL_Class..Employee_Table; LAST_NAME Jones Smythe Chambers Smith Coffing Larkins Strickling Reilly Harrison
FIRST_NAME DEPT_NO Squiggy ? Richard 10 Mandee 100 John 200 Billy 200 Loraine 300 Cletus 400 William 400 Herbert 400
First All Squiggy Squiggy Squiggy Squiggy Squiggy Squiggy Squiggy Squiggy Squiggy
First Partition Squiggy Richard Mandee John John Loraine Cletus Cletus Cletus
The above example uses FIRST_VALUE to show you the very first first_name returned. It also uses the keyword Partition to show you the very first first_name returned in each department. Page 369
Chapter 10
Window Functions OLAP
FIRST_VALUE SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - First_Value (Daily_Sales) OVER (ORDER BY Sale_Date) AS Delta_First FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ ________ 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000
09/28/2000 09/28/2000 09/29/2000 09/29/2000 09/30/2000 09/30/2000 10/01/2000 10/01/2000 10/02/2000 10/02/2000 10/03/2000 10/03/2000 10/04/2000 10/04/2000
Daily_Sales _________ Delta_First __________ 48850.40 41888.88 54500.22 48000.00 36000.07 49850.03 40200.43 54850.29 32800.50 36021.93 64300.00 43200.18 54553.10 32800.50
0.00 -6961.52 5649.82 -850.40 -12850.33 999.63 -8649.97 5999.89 -16049.90 -12828.47 15449.60 -5650.22 5702.70 -16049.90
Above, after sorting the data by Sale_Date, we compute the difference between the first row's Daily_Sales and the Daily_Sales of each following row. All rows Daily_Sales are compared with the first row's Daily_Sales, thus the name First_Value. Page 370
Chapter 10
Window Functions OLAP
FIRST_VALUE After Sorting by the Highest Value SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - First_Value (Daily_Sales) OVER (ORDER BY Daily_Sales DESC) AS Delta_First FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ _________ 1000 2000 1000 1000 2000 1000 2000 2000 2000 1000 2000 1000 1000 2000
10/03/2000 10/01/2000 10/04/2000 09/29/2000 09/30/2000 09/28/2000 09/29/2000 10/03/2000 09/28/2000 10/01/2000 10/02/2000 09/30/2000 10/02/2000 10/04/2000
Daily_Sales _________
Delta_First _________
64300.00 54850.29 54553.10 54500.22 49850.03 48850.40 48000.00 43200.18 41888.88 40200.43 36021.93 36000.07 32800.50 32800.50
0.00 -9449.71 -9746.90 -9799.78 -14449.97 -15449.60 -16300.00 -21099.82 -22411.12 -24099.57 -28278.07 -28299.93 -31499.50 -31499.50
Above, after sorting the data by Daily_Sales DESC, we compute the difference between the first row's Daily_Sales and the Daily_Sales of each following row. All rows Daily_Sales are compared with the first row's Daily_Sales, thus the name First_Value. This example shows that how much less each Daily_Sales is compared to 64,300.00 (our highest sale). Page 371
Chapter 10
Window Functions OLAP
FIRST_VALUE with Partitioning SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - First_Value (Daily_Sales) OVER (PARTITION BY Product_ID ORDER BY Sale_Date) AS Delta_First FROM Sales_Table WHERE Product_ID IN (1000, 2000) ;
Product_ID Sale_Date _________ ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000
Daily_Sales _________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
Delta_First _________ 0.00 5649.82 -12850.33 -8649.97 -16049.90 15449.60 5702.70 0.00 6111.12 7961.15 12961.41 -5866.95 1311.30 -9088.38
We are now comparing the Daily_Sales of the first Sale_Date for each Product_ID with the Daily_Sales of all other rows within the Product_ID partition. Each row is only compared with the first row (First_Value) in it's partition. Page 372
Chapter 10
Window Functions OLAP
Using LAST_VALUE SELECT Last_name, first_name, dept_no ,LAST_VALUE(first_name) OVER (ORDER BY dept_no, last_name desc rows unbounded preceding) AS "Last All" ,LAST_VALUE(first_name) OVER (PARTITION BY dept_no ORDER BY dept_no, last_name desc rows unbounded preceding) AS "Last Partition" FROM sql_class.Employee_Table; LAST_NAME Jones Smythe Chambers Smith Coffing Larkins Strickling Reilly Harrison
FIRST_NAME DEPT_NO Squiggy ? Richard 10 Mandee 100 John 200 Billy 200 Loraine 300 Cletus 400 William 400 Herbert 400
Last All Squiggy Richard Mandee John Billy Loraine Cletus William Herbert
Last Partition Squiggy Richard Mandee John Billy Loraine Cletus William Herbert
The FIRST_VALUE and LAST_VALUE are good to use anytime you need to propagate a value from one row to all or multiple rows based on a sorted sequence. However, the output from the LAST_VALUE function appears to be incorrect and is a little misleading until you understand a few concepts. The SQL request specifies "rows unbounded preceding“, and LAST_VALUE looks at the last row. The current row is always the last row and therefore, it appears in the output. Page 373
Chapter 10
Window Functions OLAP
LAST_VALUE SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - LAST_Value (Daily_Sales) OVER (ORDER BY Sale_Date) AS Delta_Last FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ ________ 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000 1000 2000
09/28/2000 09/28/2000 09/29/2000 09/29/2000 09/30/2000 09/30/2000 10/01/2000 10/01/2000 10/02/2000 10/02/2000 10/03/2000 10/03/2000 10/04/2000 10/04/2000
Daily_Sales _________
Delta_Last ________
48850.40 41888.88 54500.22 48000.00 36000.07 49850.03 40200.43 54850.29 32800.50 36021.93 64300.00 43200.18 54553.10 32800.50
6961.52 0.00 6500.22 0.00 -13849.96 0.00 -14649.86 0.00 -3221.43 0.00 21099.82 0.00 21752.60 0.00
Above, after sorting the data by Sale_Date, we compute the difference between the last row's Daily_Sales and the Daily_Sales of each following row (from the same Sale_Date). Since there is only two product totals for each day, there is always a 0.00 for one of the rows. Page 374
Chapter 10
Window Functions OLAP
Using LAG and LEAD Compatibility: SQL Server and Azure SQL Data Warehouse Extension The LAG and LEAD functions allow you to compare different rows of a table by specifying an offset from the current row. You can use these functions to analyze change and variation. Syntax for LAG and LEAD: {LAG | LEAD} (, [ [, ]]) OVER ([PARTITION BY [,...]] ORDER BY [ASC | DESC] [,...] ) ;
The above provides information and the syntax for LAG and LEAD.
Page 375
Chapter 10
Window Functions OLAP
Using LEAD SELECT Last_Name, Dept_No ,LEAD(Dept_No) OVER (ORDER BY Dept_No, Last_Name) as "Lead All" ,LEAD(Dept_No) OVER (PARTITION BY Dept_No ORDER BY Dept_No, Last_Name) as "Lead Partition" FROM Employee_Table; LAST_NAME Jones Smythe Chambers Coffing Smith Larkins Harrison Reilly Strickling
DEPT_NO ? 10 100 200 200 300 400 400 400
Lead All 10 100 200 200 300 400 400 400 ?
Lead Partition ? ? ? 200 ? ? 400 400 ?
As you can see, the first LEAD brings back the value from the next row except for the last which has no row following it. The offset value was not specified in this example, so it defaulted to a value of 1 row.
Page 376
Chapter 10
Window Functions OLAP
Using LEAD With and Offset of 2 SELECT Last_Name, Dept_No ,LEAD(Dept_No,2) OVER (ORDER BY Dept_No, Last_Name) as "Lead All" ,LEAD(Dept_No,2) OVER (PARTITION BY Dept_No ORDER BY Dept_No, Last_Name) as "Lead Partition" FROM Employee_Table;
LAST_NAME Jones Smythe Chambers Coffing Smith Larkins Harrison Reilly Strickling
DEPT_NO ? 10 100 200 200 300 400 400 400
Lead All 100 200 200 300 400 400 400 ? ?
Lead Partition ? ? ? ? ? ? 400 ? ?
Above, each value in the first LEAD is 2 rows away, and the partitioning only shows when values are contained in each value group with 1 more than offset value.
Page 377
Chapter 10
Window Functions OLAP
LEAD SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - LEAD(Daily_Sales, 1, 0) OVER (ORDER BY Product_ID, Sale_Date) AS Lead1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000
Daily_Sales _________
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
Lead1 ________
-5649.82 18500.15 -4200.36 7399.93 -31499.50 9746.90 12664.22 -6111.12 -1850.03 -5000.26 18828.36 -7178.25 10399.68 32800.50
Above, we compute the difference between a product's Daily_Sales and that of the next Daily_Sales in the sort order (which will be the next row's Daily_Sales, or one whose Daily_Sales is the same). The expression LEAD(Daily_Sales, 1, 0) tells LEAD() to evaluate the expression Daily_Sales on the row that is positioned one row following the current row. If there is no such row (as is the case on the last row of the partition or relation), then the default value of 0 is used. Page 378
Chapter 10
Window Functions OLAP
LEAD With Partitioning SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - LEAD(Daily_Sales, 1, 0) OVER (PARTITION BY Product_ID ORDER BY Sale_Date) AS Lead1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ ________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000
Daily_Sales _________ 48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
Lead1 ________ -5649.82 18500.15 -4200.36 7399.93 -31499.50 9746.90 54553.10 -6111.12 -1850.03 -5000.26 18828.36 -7178.25 10399.68 32800.50
Above, we compute the difference between a product's Daily_Sales and that of the next Daily_Sales in the sort order (which will be the next row's Daily_Sales, or one whose Daily_Sales is the same). We also partitioned the data by Product_ID. Page 379
Chapter 10
Window Functions OLAP
Using LAG SELECT Last-Name, Dept_No ,LAG(Dept_No) OVER (ORDER BY Dept_No, Last_Name) as "Lag All" ,LAG(Dept_No) OVER (PARTITION BY Dept_No ORDER BY Dept_No, Last_Name) as "Lag Partition" FROM Employee_Table;
LAST_NAME DEPT_NO Jones ? Smythe 10 Chambers 100 Coffing 200 Smith 200 Larkins 300 Harrison 400 Reilly 400 Strickling 400
Lag All ? ? 10 100 200 200 300 400 400
Lag Partition ? ? ? ? 200 ? ? 400 400
From the example above, you see that LAG uses the value from a previous row and makes it available in the next row. For LAG, the first row(s) will contain a null based on the value in the offset, here it defaulted to 1. The first null comes from the function where as the second row gets the null from the first row. Page 380
Chapter 10
Window Functions OLAP
Using LAG With an Offset of 2 SELECT Last_Name, Dept_No ,LAG(Dept_No,2) OVER (ORDER BY Dept_No, Last_Name) as "Lag All" ,LAG(Dept_No,2) OVER (PARTITION BY Dept_No ORDER BY Dept_No, Last_Name) as "Lag Partition" FROM Employee_Table; LAST_NAME Jones Smythe Chambers Coffing Smith Larkins Harrison Reilly Strickling
DEPT_NO ? 10 100 200 200 300 400 400 400
Lag All ? ? ? 10 100 200 200 300 400
Lag Partition ? ? ? ? ? ? ? ? 400
For this example, the first two rows have a null because there is not a row two rows before these. The number of nulls will always be the same as the offset value. There is a third null because Jones Dept_No is null.
Page 381
Chapter 10
Window Functions OLAP
LAG SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - LAG(Daily_Sales, 1, 0) OVER (ORDER BY Product_ID, Sale_Date) AS Lag1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date ________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000
Daily_Sales _________
Lag1 _______
48850.40 54500.22 36000.07 40200.43 32800.50 64300.00 54553.10 41888.88 48000.00 49850.03 54850.29 36021.93 43200.18 32800.50
48850.40 5649.82 -18500.15 4200.36 -7399.93 31499.50 -9746.90 -12664.22 6111.12 1850.03 5000.26 -18828.36 7178.25 -10399.68
Above, we compute the difference between a product's Daily_Sales and that of the next Daily_Sales in the sort order (which will be the previous row's Daily_Sales, or one whose Daily_Sales is the same). The expression LAG(Daily_Sales, 1, 0) tells LAG() to evaluate the expression Daily_Sales on the row that is positioned one row before the current row. If there is no such row (as is the case on the first row of the partition or relation), then the default value of 0 is used. Page 382
Chapter 10
Window Functions OLAP
LAG with Partitioning SELECT Product_ID ,Sale_Date , Daily_Sales, Daily_Sales - LAG(Daily_Sales, 1, 0) OVER (PARTITION BY Product_ID ORDER BY Sale_Date) AS Lag1 FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date _________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000 09/28/2000 09/29/2000 09/30/2000 10/01/2000 10/02/2000 10/03/2000 10/04/2000
Daily_Sales Lag1 _________ _______ 48850.40 48850.40 54500.22 5649.82 36000.07 -18500.15 40200.43 4200.36 32800.50 -7399.93 64300.00 31499.50 54553.10 -9746.90 41888.88 41888.88 48000.00 6111.12 49850.03 1850.03 54850.29 5000.26 36021.93 -18828.36 43200.18 7178.25 32800.50 -10399.68
Above, we compute the difference between a product's Daily_Sales and that of the next Daily_Sales in the sort order (which will be the previous row's Daily_Sales, or one whose Daily_Sales is the same). The expression LAG(Daily_Sales, 1, 0) tells LAG() to evaluate the expression Daily_Sales on the row that is positioned one row before the current row. If there is no such row (as is the case on the first row of the partition or relation), then the default value of 0 is used. Page 383
Chapter 10
Window Functions OLAP
CUME_DIST SELECT Product_ID ,Sale_Date , Daily_Sales, CUME_DIST() OVER (ORDER BY Daily_Sales DESC) AS CDist FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date _________ _________ 1000 10/03/2000 2000 10/01/2000 1000 10/04/2000 1000 09/29/2000 2000 09/30/2000 1000 09/28/2000 2000 09/29/2000 2000 10/03/2000 2000 09/28/2000 1000 10/01/2000 2000 10/02/2000 1000 09/30/2000 1000 10/02/2000 2000 10/04/2000
Daily_Sales __________
64300.00 54850.29 54553.10 54500.22 49850.03 48850.40 48000.00 43200.18 41888.88 40200.43 36021.93 36000.07 32800.50 32800.50
CDist _____
0.07 0.14 0.21 0.29 0.36 0.43 0.50 0.57 0.64 0.71 0.79 0.86 1.00 1.00
The CUME_DIST() is a cumulative distribution function that assigns a relative rank to each row, based on a formula. That formula is (number of rows preceding or peer with current row) / (total rows). We order by Daily_Sales DESC, so that each row is ranked by cumulative distribution. The distribution is represented relatively, by floating point numbers, from 0 to 1. When there is only one row in a partition, it is assigned 1. When there are more than one row, they are assigned a cumulative distribution ranking, ranging from 0 to 1. Page 384
Chapter 10
Window Functions OLAP
CUME_DIST With a Partition SELECT Product_ID ,Sale_Date , Daily_Sales, CUME_DIST() OVER (PARTITION by Product_ID ORDER BY Daily_Sales DESC) AS CDist FROM Sales_Table WHERE Product_ID IN (1000, 2000) ; Product_ID Sale_Date Daily_Sales ________ _________ _________ 1000 1000 1000 1000 1000 1000 1000 2000 2000 2000 2000 2000 2000 2000
10/03/2000 10/04/2000 09/29/2000 09/28/2000 10/01/2000 09/30/2000 10/02/2000 10/01/2000 09/30/2000 09/29/2000 10/03/2000 09/28/2000 10/02/2000 10/04/2000
64300.00 54553.10 54500.22 48850.40 40200.43 36000.07 32800.50 54850.29 49850.03 48000.00 43200.18 41888.88 36021.93 32800.50
CDist _____ 0.14 0.29 0.43 0.57 0.71 0.86 1.00 0.14 0.29 0.43 0.57 0.71 0.86 1.00
The CUME_DIST() is a cumulative distribution function that assigns a relative rank to each row, based on a formula. That formula is (number of rows preceding or peer with current row) / (total rows). We Partition by Product_ID and Order By Daily_Sales DESC, so that each row is ranked by cumulative distribution within its partition. Page 385
Chapter 10
Window Functions OLAP
SUM(SUM(n)) SELECT Product_ID , SUM(Daily_Sales) as Summy, SUM(SUM(Daily_Sales)) OVER (ORDER BY Sum(Daily_Sales) ) AS Prod_Sales_Running_Sum FROM Sales_Table GROUP BY Product_ID ;
Product_ID __________ Summy _______ Prod_Sales_Running_Sum ___________________ 3000 2000 1000
224587.82 306611.81 331204.72
224587.82 531199.63 862404.35
Window functions can compute aggregates of aggregates, as in the example above.
Page 386
Chapter 11
Page 387
Working with Strings
Chapter 11
Working with Strings
Chapter 11 - Working with Strings
“It’s always been and always will be the same in the world: the horse does the work and the coachman is tipped.” - Anonymous
Page 388
Chapter 11
Working with Strings
The ASCII Function The example below shows you how to convert characters into the integer ASCII value. Syntax: ASCII (string)
SELECT ASCII('H') as AsciiH ,ASCII('o') as AsciiO ,ASCII('w') as AsciiW ,ASCII('d') as AsciiD ,ASCII('y') as AsciiY
AsciiH AsciiO ______ AsciiW _______ AsciiD ______ AsciiY ______ ______ 72
111
119
100
121
The example above shows you how to convert characters into the integer ASCII value.
Page 389
Chapter 11
Working with Strings
The CHAR Function The example below shows you how to convert the integer ASCII value into characters. Syntax: CHAR (integer)
SELECT CHAR(72) As CharH ,CHAR(111) As CharO ,CHAR(119) As CharW ,CHAR(100) As CharD ,CHAR(121) As CharY ;
CharH CharW ______ CharD ______ CharY _____ CharO _____ _____ H
o
w
d
y
The example above shows you how to convert the integer ASCII value into characters.
Page 390
Chapter 11
Working with Strings
The UNICODE Function The UNICODE function returns the Unicode integer value for the first character of the character or input expression. Syntax: UNICODE (string)
SELECT UNICODE('H') AS UniH ,UNICODE('o') AS UniO ,UNICODE('w') AS UniW ,UNICODE('d') AS UniD ,UNICODE('y') AS UniY ;
UniH _____ UniO _____
72
111
UniW _____
119
UniD _____ UniY _____
100
121
The example above shows you how to convert characters into the UNICODE value.
Page 391
Chapter 11
Working with Strings
The NCHAR Function The NCHAR function takes the integer values and converts them back into characters.
Syntax: NCHAR (Integer)
SELECT NCHAR(72) ,NCHAR(111) ,NCHAR(119) ,NCHAR(100) ,NCHAR(121)
AS NcaH AS NcaO AS NcaW AS NcaD AS NcaY ;
NcaH _____ NcaO _____
NcaW _____
NcaD _____ NcaY _____
H
w
d
o
The example above shows you how to convert integers back to characters.
Page 392
y
Chapter 11
Working with Strings
The LEN Function The LEN function returns the number of characters in an input string. (Ending spaces are automatically excluded for CHAR data types) Syntax: LEN (string) SELECT First_Name ,LEN(First_Name) AS Lnth ,Last_Name ,LEN(Last_Name) AS Lnth FROM Employee_Table First_Name __________
Lnth ____
Last_Name __________
Richard Cletus Mandee Herbert Billy John Squiggy Loraine William
7 6 6 7 5 4 7 7 7
Smythe Strickling Chambers Harrison Coffing Smith Jones Larkins Reilly
Lnth ____ 6 10 8 8 7 5 5 7 6
The LEN function returns the number of characters in the input string and not necessarily the number of bytes.
Page 393
Chapter 11
Working with Strings
The DATALENGTH Function The DATALENGTH function returns the number of characters in an input string. (Ending spaces are automatically included for CHAR data types) Syntax: DATALENGTH (string) SELECT First_Name ,DATALENGTH(First_Name) AS Lnth ,Last_Name ,DATALENGTH(Last_Name) AS Lnth FROM Employee_Table First_Name __________
Lnth ____
Last_Name __________
Lnth ____
Richard Cletus Mandee Herbert Billy John Squiggy Loraine William
7 6 6 7 5 4 7 7 7
Smythe Strickling Chambers Harrison Coffing Smith Jones Larkins Reilly
20 20 20 20 20 20 20 20 20
The DATALENGTH function returns the number of characters in the input string and not necessarily the number of bytes. The difference between the LEN and the DATALENGTH functions is that the LEN function excludes trailing spaces. The DATALENGTH function counts them. Notice that each length is 20 characters for the Last_Name lengths. Page 394
Chapter 11
Working with Strings
Concatenation
The + sign means concatenate
SELECT First_Name ,Last_Name ,First_Name A space + '' + Last_Name as Full_Name FROM Employee_Table WHERE First_Name = 'Squiggy'
First_Name _________
Last_Name Full_Name _________ ___________
Squiggy
Jones
Squiggy Jones concatenated
See those + signs? Those represent concatenation. That allows you to combine multiple columns into one column. The + in this example has combined the first name, then a single space, and then the last name to get a new column called ‘Full name’. We brought back the full name of Squiggy Jones.
Page 395
Chapter 11
Working with Strings
The RTRIM and LTRIM Command trims Spaces RTRIM Query
SELECT Last_Name ,RTRIM(Last_Name) AS Trim_Trailing_Spaces FROM Employee_Table ;
LTRIM Query SELECT Last_Name ,LTRIM(Last_Name) AS Trim_Leading_Spaces FROM Employee_Table ;
Trimming Both Leading and Trailing Spaces Query SELECT Last_Name ,LTRIM(RTRIM(Last_Name)) AS Trim_Spaces_Leading_Trailing FROM Employee_Table ; The RTRIM command trims trailing spaces from a character string. The LTRIM trims leading spaces from a character string. The LTRIM(RTRIM) combination trims both leading and trailing spaces from a character string.. Page 396
Chapter 11
Working with Strings
The SUBSTRING Command SELECT First_Name, SUBSTRING (First_Name, 2, 3) AS Quiz FROM Employee_Table ; Start in position 2
First_Name __________ Squiggy John Richard Herbert Mandee Cletus William Billy Loraine
Go for 3 positions
Quiz ______ qui ohn ich erb and let ill ill ora
This is a SUBSTRING. The substring is passed two parameters, and they are the starting position of the string and the number of positions to return (from the starting position). The above example will start in position 2 and go for 3 positions! Page 397
Chapter 11
Working with Strings
Using SUBSTRING to move Backwards SELECT First_Name, SUBSTRING (First_Name , 0 , 6) AS Before1 FROM Employee_Table ; Start in Position 0 (one space before)
First_Name Before1 __________ ________ Squiggy Squig John John Richard Richa Herbert Herbe Mandee Mande Cletus Cletu William Willi Billy Billy Loraine Lorai A starting position of zero moves one space in front of the beginning. Notice that our FOR Length is 6 so ‘Squiggy’ turns into ‘ Squig’. The point being made here is that both the starting position and ending positions can move backwards which will come in handy as you see other examples. Page 398
Chapter 11
Working with Strings
How SUBSTRING Works with a Starting Position of -1 SELECT First_Name, SUBSTRING (First_Name , -1 , 3) AS Before2 FROM Employee_Table ; Start in Position -1. This is two spaces before.
First_Name Before2 __________ ________ Squiggy S John J Richard R Herbert H Mandee M Cletus C William W Billy B Loraine L A starting position of -1 moves two spaces in front of the beginning. Notice that our FOR Length is 3, so each name delivers only the first initial. The point being made here is that both the starting position and ending positions can move backwards which will come in handy as you see other examples. Page 399
Chapter 11
Working with Strings
How SUBSTRING Works with an Ending Position of 0 SELECT First_Name, SUBSTRING (First_Name , 3 , 0) AS WhatsUp FROM Employee_Table ; Go for 0 positions
First_Name WhatsUp __________ ________ Squiggy John Richard Herbert Mandee Cletus William Billy Loraine In our example above, we start in position 3, but we go for zero positions, so nothing is delivered in the column. That is what’s up!
Page 400
Chapter 11
Working with Strings
Concatenation and SUBSTRING A Period (.) and a space
SELECT First_Name ,Last_Name ,Substring(First_Name, 1, 1) + '. ' + Last_Name as Full_Name FROM Employee_Table
First_Name _________ Last_Name ____________ Full_Name _________ Richard Smythe R. Smythe Cletus Strickling C. Strickling Mandee Chambers M. Chambers Herbert Harrison H. Harrison Billy Coffing B. Coffing John Smith J. Smith Squiggy Jones S. Jones Loraine Larkins L. Larkins William Reilly W. Reilly Of the three items being concatenated together, what is the first item of concatenation in the example above? The first initial of the First_Name. Then, we concatenated a literal space and a period. Then, we concatenated the Last_Name. Page 401
Chapter 11
Working with Strings
SUBSTRING and Different Aliasing SELECT Phone_Number ,First3digits = SUBSTRING(Phone_Number, 1, 3) ,Exchange = SUBSTRING(Phone_Number, 5,4) FROM Customer_Table WHERE Phone_Number LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
Phone_Number __________ First3digits _________ Exchange ____________ 555-1234 555 1234 555-1111 555 1111 555-1212 555 1212 347-8954 347 8954 322-1012 322 1012
Above, we are using the Substring commands to extract certain portions of the Phone_Number. Notice that the column names are materialized at the beginning of the line. This is almost like a reverse alias.
Page 402
Chapter 11
Working with Strings
The LEFT and RIGHT Functions The LEFT and RIGHT functions are abbreviations of the SUBSTRING function. They return a requested number of characters from the left or right end of the input string. Syntax: LEFT(string, n), RIGHT(string, n)
SELECT First_Name ,LEFT (First_Name , 1) AS First_Initial ,Last_Name ,Right (RTRIM(Last_name), 2) AS "Last Two Letters" FROM Employee_Table WHERE Dept_No in (400) ; First_Name __________
First_Initial __________
Last_Name Last Two Letters __________ ______________
Cletus Herbert William
C H W
Strickling Harrison Reilly
ng on ly
In our example above, our result set will have the First_Name and Last_Name coming back, but we also use the LEFT and RIGHT functions to produce the first letter of the First_Name and the last two letters of the Last_Name. We filtered the rows with an additional WHERE clause to only bring back three rows. Notice the RTRIM of Last_Name. This is necessary because the Last_Name column has a data type of Character 20. This is padded with spaces. Page 403
Chapter 11
Working with Strings
Four Concatenations Together CHAR(20)
VARCHAR(12)
SELECT First_Name ,Last_Name ,RTRIM(Last_Name) + ' ' + Substring(First_Name, 1, 1) + '.' AS Last_Name_1st FROM Employee_Table ;
First_Name Last_Name_1st __________ Last_Name _________ _____________ Richard Cletus Mandee Herbert Billy John Squiggy Loraine William
Smythe Strickling Chambers Harrison Coffing Smith Jones Larkins Reilly
Smythe R. Strickling C. Chambers M. Harrison H. Coffing B. Smith J. Jones S. Larkins L. Reilly W.
Why did we TRIM the Last_Name? To get rid of the spaces or the output would have looked odd. How many items are being concatenated in the example above? There are 4 items concatenated. We start with the Last_Name (after we trim it), then we have a single space, then we have the First Initial of the First Name, and then we have a Period. Page 404
Chapter 11
Working with Strings
The DATALENGTH Function and RTRIM The DATALENGTH function returns the number of characters in an input string. (Ending spaces are automatically included for CHAR data types) Syntax: DATALENGTH (string) SELECT First_Name ,DATALENGTH(First_Name) AS Lnth ,Last_Name ,DATALENGTH(RTRIM(Last_Name)) AS Lnth FROM Employee_Table First_Name __________
Lnth ____
Last_Name __________
Richard Cletus Mandee Herbert Billy John Squiggy Loraine William
7 6 6 7 5 4 7 7 7
Smythe Strickling Chambers Harrison Coffing Smith Jones Larkins Reilly
Lnth ____ 6 10 8 8 7 5 5 7 6
The DATALENGTH function returns the number of characters in the input string and not necessarily the number of bytes. The difference between the LEN and the DATALENGTH functions is that the LEN function excludes trailing spaces, however the DATALENGTH function counts them. Use either the LEN function or merely RTRIM with DATALENGTH. Page 405
Chapter 11
Working with Strings
A Visual of the TRIM Command Using Concatenation Concatenation without Trim and with Trim SELECT Last_Name concatenate ,First_Name ,Last_Name + First_Name as NameBackwards ,RTRIM(Last_Name) + First_Name as TrimNameBackwards FROM Employee_Table
Last_Name First_Name __________ __________ Jones Squiggy Smith John Smythe Richard Harrison Herbert Chambers Mandee Strickling Cletus Reilly William Coffing Billy Larkins Loraine
NameBackwards TrimNameBackwards ______________________ __________________ Jones Squiggy JonesSquiggy Smith John SmithJohn Smythe Richard SmytheRichard Harrison Herbert HarrisonHerbert Chambers Mandee ChambersMandee Strickling Cletus StricklingCletus Reilly William ReillyWilliam Coffing Billy CoffingBilly Larkins Loraine LarkinsLoraine
When you use the RTRIM command on a column, that column will have trailing spaces removed.
Page 406
Chapter 11
Working with Strings
CHARINDEX Function Finds a Letter(s) Position in a String Tell this function what character(s) to look for in a string, and optionally, what starting position to first start looking. If it does not find the character(s) in the string it returns a 0. It also only reports the first occurrence. Syntax: CHARINDEX(substring, string[, start_pos]) SELECT Last_Name ,CHARINDEX ('e', Last_Name) AS Find_E ,CHARINDEX ('f', Last_Name) AS Find_F ,CHARINDEX ('th', Last_Name) AS Find_TH ,CHARINDEX ('in', Last_Name, 6) AS Find_es_after_6 FROM Employee_Table WHERE Last_Name IN ('Smith', 'Smythe', 'Strickling', 'Coffing') ORDER BY 1 DESC; Last_Name _________ Strickling Smythe Smith Coffing
Find_E ______ 0 6 0 0
Find_F ______ 0 0 0 3
Find_TH ________ Find_ing_after_6 ______________ 0 4 4 0
8 0 0 0
Strickling does not have an 'e', 'f' or 'th' in it, but it does have an 'in' starting in position 8. Coffing shows only the first 'f' in position 3, but notice that Coffing also has an 'in', however we stated to start looking in position 6, thus a zero was returned to indicate it didn't find an occurrence. Smith and Smythe both have a 'th' starting in position 4. Page 407
Chapter 11
Working with Strings
The CHARINDEX Command is brilliant with SUBSTRING Starting position is a subquery. Find the first space and subtract two.
SELECT Last_Name ,SUBSTRING (Last_Name, CHARINDEX(' ', Last_name) -2 , 2) as Last_Two_Letters FROM Employee_Table; Last_Name _________
Smythe Strickling Chambers Harrison Coffing Smith Jones Larkins Reilly
Last_Two_Letters _____________
he ng rs on ng th es ns ly
What was the starting position of the Substring in the above query? It uses a subquery. Page 408
Chapter 11
Working with Strings
The CHARINDEX Command Using a Literal The phrase we are seeking to find
The 1st character of the phrase starts here
SELECT CHARINDEX('May flowers', 'April showers bring May flowers') ;
(No column name) _____________ 21
We are looking for the phrase May flowers. This starts in position 21 of the substring Page 409
Chapter 11
Working with Strings
PATINDEX Function The PATINDEX, better named "Pattern Index" will find patterns in an argument somewhat similar to the LIKE command. The following example will show how to find the first occurrence of a digit within a string. Syntax: PATINDEX(pattern, string) SELECT PATINDEX('%[0-9]%', 'July 4th Holiday') as Number_Position; Give me the position of any number between 0-9 in the string
Number_Position _______________ 6
The "Pattern Index", referred to as PATINDEX will look for a pattern in a string and give you the position of the first character in the pattern. Above, we are using the literal 'July 4th Holiday', but we could have used a column value. The number 4 is in the 6th position of the value. Page 410
Chapter 11
Working with Strings
PATINDEX Function to Find a Character Pattern The PATINDEX, better named "Pattern Index" will find patterns in an argument somewhat similar to the LIKE command. The example below will find any occurrence where the column Street has a 3 before the St.
Syntax: PATINDEX(pattern, string) SELECT Subscriber_No, Street, PATINDEX('%[3]%St%', Street) As "Street_3" FROM Addresses Subscriber_No ____________ 5555555 2222222 4444444 1111111 3333333
Street Street_3 __________________ ________ 121 Jump St. 123 Some St. 12 Jump St. 123 Any St. 2468 Appreciate Ave.
0 3 0 3 0
The "Pattern Index", referred to as PATINDEX will look for a pattern in a string and give you the position of the first character in the pattern. Above, we are using the column Street to see if there is a 3 before the St. Notice that we have two hits and they are both in the 3rd position of the column Street. Page 411
Chapter 11
Working with Strings
SOUNDEX Function to Find a Sound The SOUNDEX, better named "Sound" will display similar sounding items. The example below will find any Last_Name that sounds like 'Smith'.
Syntax: SOUNDEX(String)
SELECT DISTINCT SOUNDEX(Last_Name) SoundsLike1 ,SOUNDEX('Smith') SoundsLike2 ,Last_Name FROM Employee_Table WHERE SOUNDEX(Last_Name) = SOUNDEX('Smith') SoundsLike1 Last_Name ___________ SoundsLike2 ___________ _________ S530 S530 Smith S530 S530 Smythe
Call center employees often look up customers by last name while speaking with the customer on the phone. The employees would like to guess at the spelling of the name to narrow the search results and then work with the customer to determine the appropriate spelling. This is what the SOUNDEX function does. Above, we are looking at anyone who has a name that sounds like 'Smith'. We got two results back in 'Smith' and 'Smythe'. Page 412
Chapter 11
Working with Strings
DIFFERENCE Function to Quantile a Sound The DIFFERENCE function will display similar sounding items and give them a quantile of 4 (high similarity) to a low of 0 (low similarity).
SELECT DISTINCT SOUNDEX(Last_Name) AS Sound1 ,SOUNDEX('smith') AS Sound_Smith ,DIFFERENCE(Last_Name, 'Smith') as High4Low0 ,Last_Name FROM Employee_Table ORDER BY 3 DESC ; Sound1 Sound_Smith __________ High4Low0 __________ Last_Name ______ ___________ 4 Smith S530 S530 Sounds a lot 4 Smythe S530 S530 like 'Smith' 2 Jones J520 S530 2 Reilly R400 S530 2 Strickling S362 S530 1 Coffing C152 S530 1 Chambers C516 S530 H625 S530 Sounds nothing 1 Harrison like 'Smith' 1 Larkins L625 S530
Call center employees often look up customers by last name while speaking with the customer on the phone. The employees would like to guess at the spelling of the name to narrow the search results and then work with the customer to determine the appropriate spelling. The SOUNDEX and DIFFERENCE functions can both be used. Above, we are using the DIFFERENCE function to show how close the name 'Smith' is to other Last_Name values. Page 413
Chapter 11
Working with Strings
The REPLACE Function The REPLACE function replaces all occurrences of substring1 in the string with substring2. Syntax: REPLACE(string, substring1, substring2) SELECT Customer_Name ,REPLACE (Customer_Name, ' ', '_') AS Under_Score ,Phone_Number ,REPLACE (Phone_Number, '-', ' ') AS No_Dash FROM Customer_table Customer_Name Under_Score ________________ ________________ Phone_Number _______________ No_Dash _________ Billy's Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
Billy's_Best_Choice Acme_Products ACE_Consulting XYZ_Plumbing Databases_N-U
Replace spaces with underscores
555-1234 555-1111 555-1212 347-8954 322-1012
555 1234 555 1111 555 1212 347 8954 322 1012
Replace dashes with spaces
The RELACE function replaces a value for another in a string. Above, we have replaced the spaces in a Customer Name with underscores. In the Phone Number we have replace the dashes (-) with a space. Page 414
Chapter 11
Working with Strings
LEN and REPLACE Functions for Number of Occurrences SELECT Last_Name ,LEN(Last_Name) - LEN(REPLACE(Last_Name, 'r', '')) AS Num_of_Occur FROM Employee_Table WHERE Last_Name LIKE '%r%' Two single quotes Last_Name ____________ Num_of_Occur _______________ Strickling Chambers Harrison Larkins Reilly
1 1 2 1 1
The LEN function returns the number of characters in an input string.
Syntax: LEN (string) The REPLACE function replaces all occurrences of substring1 in the string with substring2. Syntax: REPLACE(string, substring1, substring2)
The RELACE function and LEN function can be combined to find the number of occurrences of a character. You can use the REPLACE function to count the number of occurrences of a character within a string. To do this, you replace all occurrences of the character with an empty string (zero characters) and calculate the original length of the string minus the new length. Page 415
Chapter 11
Working with Strings
REPLICATE Function The REPLICATE function replicates a string a requested number of times. Syntax: REPLICATE(string, n) SELECT Last_Name ,Class_Code ,REPLICATE(Class_Code, 3) AS Repeat_3_Times ,REPLICATE('Go Wildcats! ', 2) AS UofA FROM Student_Table Last_Name __________ Phillips Hanson Wilson Thomas Johnson McRoberts Bond Delaney Smith Larkins
Class_Code __________ SR FR SO FR ? JR JR SR SO FR
Repeat_3_Times ______________ SRSRSR FRFRFR SOSOSO FRFRFR ? JRJRJR JRJRJR SRSRSR SOSOSO FRFRFR
UofA ______________________ Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats! Go Wildcats!
The REPLICATE function replicates a string a number of times. Above, notice we replicated the class_code column 3 times. Also notice that we replicated a literal value of 'Go Wildcats! ' 2 times. Did you notice that Johnson had a null value for his Class_Code? The Null value did not replicate. Page 416
Chapter 11
Working with Strings
STUFF Function The STUFF function works on a character string and will put STUFF where you want STUFF after deleting STUFF.
Syntax: STUFF(string, pos, delete_length, insertstring)
SELECT Start in Delete Put in nd position 2 1 Character 'enior' First_Name ,Class_Code ,STUFF (Class_Code, 2, 1, 'enior') As Full_Class_Code FROM Student_Table WHERE Class_Code = 'SR' First_Name __________ Martin Danny
Class_Code __________ SR SR
Full_Class_Code _______________ Senior Senior
The STUFF function operates on an input parameter string. It deletes as many characters as the number specified in the delete_length parameter, starting at the character position specified in the pos input parameter. The function inserts the string specified in the insertstring parameter in position pos. If you decide to insert a string and not delete anything, you can specify a length of 0 as the third argument. Page 417
Chapter 11
Working with Strings
STUFF without Deleting Function The STUFF function works on a character string and will put STUFF where you want STUFF after deleting STUFF. Syntax: STUFF(string, pos, delete_length, insertstring)
Start in 1st position
Delete 0 Characters
Put in 'Course: '
SELECT Course_Name ,STUFF (Course_Name, 1, 0, 'Course: ') As Course_Added FROM Course_Table Course_Name _____________________ Advanced SQL Database Administration Introduction to SQL Physical Database Design SQL Server Concepts V2R3 SQL Features
Course_Added ____________________________ Course: Advanced SQL Course: Database Administration Course: Introduction to SQL Course: Physical Database Design Course: SQL Server Concepts Course: V2R3 SQL Features
Above, we decided not to delete anything, but to insert a string called 'Course: ', so we specified a length of 0 as the third argument. The STUFF function operates on an input parameter string. It deletes as many characters as the number specified in the delete_length parameter, starting at the character position specified in the pos input parameter. The function inserts the string specified in the insertstring parameter in position pos. Page 418
Chapter 11
Working with Strings
UPPER and lower Functions The UPPER and LOWER functions convert the input string to either all uppercase or lowercase characters. Syntax: UPPER(string), LOWER(string)
SELECT First_Name ,UPPER (First_Name) as "Upper Case" ,lower(First_Name) as "Lower Case" FROM Student_Table First_Name __________ Martin Henry Susie Wendy Stanley Richard Jimmy Danny Andy Michael
Upper Case Lower Case __________ __________ MARTIN martin HENRY henry SUSIE susie WENDY wendy STANLEY stanley RICHARD richard JIMMY jimmy DANNY danny ANDY andy MICHAEL michael
The UPPER and LOWER functions convert the input string to either all uppercase or lowercase characters. Page 419
Chapter 12
Page 420
Interrogating the Data
Chapter 12
Interrogating the Data
Chapter 12 - Interrogating the Data
"The difference between genius and stupidity is that genius has its limits" - Albert Einstein
Page 421
Chapter 12
Interrogating the Data
Quiz – What would the Answer be? Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT Class_Code ,Grade_Pt / (Grade_Pt * 2 ) as Math1 FROM Student_Table ORDER BY 1,2 ;
Can you guess what would return in the Answer Set? Using the Student_Table above, and try and predict what the answer will be if this query was running on the system. Page 422
Chapter 12
Interrogating the Data
Answer to Quiz – What would the Answer be? Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT Class_Code ,Grade_Pt / (Grade_Pt * 2 ) as Math1 FROM Student_Table ORDER BY 1,2 ; Error – Division by zero
You get an error when you DIVIDE by ZERO! Let’s turn the page and fix it!
Page 423
Chapter 12
Interrogating the Data
The NULLIF Command Student_Table Student_ID _________ 423400 231222 280023 322133 125634 333450 324652 260000 234121 123250
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Larkins Michael FR 0.00 Wilson Susie SO 3.80 McRoberts Richard JR 1.90 Bond Jimmy JR 3.95 Hanson Henry FR 2.88 Smith Andy SO 2.00 Delaney Danny SR 3.35 Johnson Stanley ? ? Thomas Wendy FR 4.00 Phillips Martin SR 3.00
SELECT Class_Code ,Grade_Pt / ( NULLIF (Grade_pt,0) * 2 ) AS Math1 FROM Student_Table; SELECT Class_Code ,Grade_Pt / ( NULLIF( (Grade_pt) * 2, 0 ) ) AS Math1 FROM Student_Table;
If you have a calculation where a ZERO could kill the operation, and you don’t want that, you can use the NULLIF command to convert any zero value to a null value. Both queries above bring back the same result. Page 424
Chapter 12
Interrogating the Data
Quiz – Fill in the Answers for the NULLIF Command Student_Table Student_ID _________ 423400 123250 234121
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Phillips Martin SR 3.00 Thomas Wendy FR 4.00
SELECT Fill in the Answer Last_Name Set below after ,NULLIF(Grade_Pt, 0) AS GP1 looking at the table ,NULLIF(Grade_Pt, 3.0) AS GP2 and the query. ,NULLIF(Grade_Pt, 4.0) AS GP3 FROM Student_Table WHERE Student_ID IN (423400, 123250, 234121) ORDER BY Last_Name ; Last_Name GP1 __________ ____ Larkins Phillips Thomas
GP2 ____
What would the above Answer Set produce from your analysis? Page 425
GP3 ____
Chapter 12
Interrogating the Data
Answer– Fill in the Answers for the NULLIF Command Student_Table Student_ID _________ 423400 123250 234121
Last_Name First_Name Grade_Pt __________ __________ Class_Code __________ ________ Larkins Michael FR 0.00 Phillips Martin SR 3.00 Thomas Wendy FR 4.00
SELECT Fill in the Answer Last_Name Set below after ,NULLIF(Grade_Pt, 0) AS GP1 looking at the table ,NULLIF(Grade_Pt, 3.0) AS GP2 and the query. ,NULLIF(Grade_Pt, 4.0) AS GP3 FROM Student_Table WHERE Student_ID IN (423400, 123250, 234121) ORDER BY Last_Name ; Last_Name GP1 GP2 __________ ____ ____ ? 0.00 Larkins 3.00 ? Phillips 4.00 4.00 Thomas
GP3 ____ 0.00 3.00 ?
Look at the answers above, and if it doesn’t make sense, go over it again until it does.
Page 426
Chapter 12
Interrogating the Data
The COALESCE Command – Fill In the Answers Student_Table Student_ID _________ 423400 260000 234121
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Larkins Michael FR 0.00 Johnson Stanley ? ? Thomas Wendy FR 4.00
SELECT Fill in the Answer Last_Name Set below after looking at the table ,Grade_Pt and the query. ,Class_Code ,COALESCE (Grade_Pt, Class_Code) as ValidStudents FROM Student_Table WHERE Last_Name IN ('Johnson', 'Larkins', 'Thomas') ORDER BY 1 ; Last_Name Grade_Pt __________ ________ Johnson Larkins Thomas
? 0.00 4.00
Class_Code __________ ValidStudents ___________ ? FR FR
Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null Page 427
Chapter 12
Interrogating the Data
The COALESCE Answer Set Student_Table Student_ID _________ 423400 260000 234121
Last_Name First_Name __________ __________ Class_Code __________ Grade_Pt ________ Larkins Michael FR 0.00 Johnson Stanley ? ? Thomas Wendy FR 4.00
SELECT Last_Name ,Grade_Pt ,Class_Code ,COALESCE (Grade_Pt, Class_Code) as ValidStudents FROM Student_Table WHERE Last_Name IN ('Johnson', 'Larkins', 'Thomas') ORDER BY 1 ;
Last_Name Grade_Pt __________ ________ Johnson Larkins Thomas
? 0.00 4.00
Class_Code __________ ValidStudents ___________ ? FR FR
? 0.00 4.00
Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null.
Page 428
Chapter 12
Interrogating the Data
COALESCE is Equivalent to This CASE Statement SELECT Last_Name ,Grade_Pt ,Class_Code ,COALESCE (Grade_Pt, Class_Code) as ValidStudents FROM Student_Table ; SELECT Last_Name ,Grade_Pt ,Class_Code , CASE WHEN Grade_Pt IS NOT NULL THEN Grade_Pt WHEN Class_Code IS NOT NULL THEN Class_Code ELSE NULL END as ValidStudents FROM Student_Table ;
Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null. Above are two queries that return the exact same answer set. These examples are designed to give you a better idea of how Coalesce works
Page 429
Chapter 12
Interrogating the Data
The Basics of CAST (Convert and Store) CAST will convert a column or value’s data type temporarily into another data type. Below is the syntax:
SELECT CAST( AS [()] ) FROM ; Convert smallint to character
Examples using CAST:
CAST ( CAST ( CAST ( CAST (
AS CHAR(5) ) AS INTEGER ) AS VARCHAR(5) ) AS FLOAT )
Truncates decimals
Data can be converted from one type to another by using the CAST function. As long as the data involved does not break any data rules (i.e. placing alphabetic or special characters into a numeric data type), the conversion works. The name of the CAST function comes from the Convert And STore operation that it performs.
Page 430
Chapter 12
Interrogating the Data
Some Great CAST (Convert and Store) Examples SELECT CAST('ABCDE' AS CHAR(1) ) AS Trunc ,CAST(128 AS CHAR(3) ) AS This_Is_OK ,CAST(127 AS INTEGER ) AS Bigger ;
_____ ____ Trunc This_Is_OK ______ A 128
Bigger ______ 127
The first CAST truncates the five characters (left to right) to form the single character ‘A’. In the second CAST, the integer 128 is converted to three characters and left justified in the output. The 127 was initially stored in a SMALLINT (5 digits - up to 32767) and then converted to an INTEGER. Hence, it uses 11 character positions for its display, ten numeric digits and a sign (positive assumed) and right justified as numeric.
Page 431
Chapter 12
Interrogating the Data
Some Great CAST (Convert and Store) Examples SELECT CAST('ABCDE' AS CHAR(1) ) AS Trunc ,CAST(128 AS CHAR(3) ) AS This_Is_OK ,CAST(127 AS INTEGER ) AS Bigger ;
_____ ____ Trunc This_Is_OK ______ A 128
Bigger ______ 127
The value of 121.53 was initially stored as a DECIMAL as 5 total digits with 2 of them to the right of the decimal point. Then, it is converted to a SMALLINT using CAST to remove the decimal positions. Therefore, it truncates data by stripping off the decimal portion. It does not round data using this data type. On the other hand, the CAST in the fifth column called Rounder is converted to a DECIMAL as 3 digits with no digits (3,0) to the right of the decimal, so it will round data values instead of truncating. Since .53 is greater than .5, it is rounded up to 122.
Page 432
Chapter 12
Interrogating the Data
A Rounding Example SELECT CAST(.014 ,CAST(.016 ,CAST(.015 ,CAST(.0150 ,CAST(.0250 ,CAST(.0159
Digit to Right of rounding digit < 5 (no change)
.014 ____ 0.01
AS Decimal(3,2)) AS Decimal(3,2)) AS Decimal(3,2)) AS Decimal(3,2)) AS Decimal(3,2)) AS Decimal(3,2))
AS ".014" AS ".016" AS ".015" AS ".0150" AS ".0250" AS ".0159"
Digit to Right of rounding digit > 5 (increase 1)
.016 ____ 0.02
.015 ____ 0.02
.0150 _____ 0.02
.0250 _____ 0.03
Above is an example of what you might expect to see in similar rounding examples.
Page 433
.0159 _____ 0.02
Chapter 12
Interrogating the Data
Quiz - CAST Examples SELECT Order_Number as OrdNo ,Customer_Number as CustNo ,Order_Date ,Order_Total ,CAST(Order_Total as integer) as Chopped ,CAST(Order_Total as Decimal(5,0)) as Rounded FROM Order_Table ORDER BY 1 ; Fill in the Answer Set below after looking at the data and the query.
OrdNo _________ CustNo Order_Date Order_Total _______ __________ __________ Chopped _______ 123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
1998-05-04 1999-01-01 1999-10-01 1999-10-10 1999-09-09
Rounded _______
12347.53 8005.91 5111.47 15231.62 23454.84
The Column Chopped takes Order_Total (a Decimal (10,2) and CASTs it as an integer which chops off the decimals. Rounded CASTs Order_Total as a Decimal (5,0), which takes the decimals and rounds up if the decimal is .50 or above. Page 434
Chapter 12
Interrogating the Data
Answer to Quiz - CAST Examples SELECT Order_Number as OrdNo ,Customer_Number as CustNo ,Order_Date ,Order_Total ,CAST(Order_Total as integer) as Chopped ,CAST(Order_Total as Decimal(5,0)) as Rounded FROM Order_Table ORDER BY 1 ;
OrdNo _________ CustNo Order_Date Order_Total _______ __________ __________ Chopped _______
123456 123512 123552 123585 123777
11111111 11111111 31323134 87323456 57896883
1998-05-04 1999-01-01 1999-10-01 1999-10-10 1999-09-09
12347.53 8005.91 5111.47 15231.62 23454.84
12347 8005 5111 15231 23454
Rounded _______
12348 8006 5111 15232 23455
The Column Chopped takes Order_Total (a Decimal (10,2) and CASTs it as an integer which chops off the decimals. Rounded CASTs Order_Total as a Decimal (5,0), which takes the decimals and rounds up if the decimal is .50 or above.
Page 435
Chapter 12
Interrogating the Data
Quiz - The Basics of the CASE Statements Course_Table Course_ID _________ 100 200 210 220 300 400
Course_Name Credits _____________________ ______ Seats _____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16
SELECT Course_Name ,CASE Credits WHEN 1 THEN 'One Credit' WHEN 2 THEN 'Two Credits' WHEN 3 THEN 'Three Credits' END AS CreditAlias FROM Course_Table WHERE Course_ID IN (220, 300) ; Course_Name ______________________ CreditAlias ____________ Physical Database Design SQL Features
This is a CASE STATEMENT which allows you to evaluate a column in your table, and from that, come up with a new answer for your report. Every CASE begins with a CASE, and they all must end with a corresponding END. What would the answer be? Page 436
Chapter 12
Interrogating the Data
Answer to Quiz - The Basics of the CASE Statements Course_Table Course_ID _________ 100 200 210 220 300 400
Course_Name Credits _____________________ ______ Seats _____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16
SELECT Course_Name ,CASE Credits WHEN 1 THEN 'One Credit' WHEN 2 THEN 'Two Credits' WHEN 3 THEN 'Three Credits' END AS CreditAlias FROM Course_Table WHERE Course_ID IN (220, 300) ; Course_Name ______________________ CreditAlias ____________ ? Physical Database Design Two Credits SQL Features
The answer for the Physical Database Design class is null. This is because it fell through the case statement. The answer for the SQL Features course is Two Credits. Once a case statement gets a match, it leaves the statement and gets the next row. Page 437
Chapter 12
Interrogating the Data
Using an ELSE in the Case Statement Course_Table Course_ID _________ 100 200 210 220 300 400
Course_Name Credits _____________________ ______ Seats _____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16
SELECT Course_Name ,CASE Credits WHEN 1 THEN 'One Credit' WHEN 2 THEN 'Two Credits' WHEN 3 THEN 'Three Credits' ELSE 'Four Credits' END AS CreditAlias FROM Course_Table WHERE Course_ID IN (220, 300) ; Course_Name ______________________ CreditAlias ____________ Four Credits Physical Database Design Two Credits SQL Features
Now that we have an ELSE in our case statement we are guaranteed that nothing will fall through.
Page 438
Chapter 12
Interrogating the Data
Using an ELSE as a Safety Net Course_Table Course_ID _________ 100 200 210 220 300 400
Course_Name Credits _____________________ ______ Seats _____ Database Concepts 3 50 Introduction to SQL 3 20 Advanced SQL 3 22 SQL Features 2 25 Physical Database Design 4 20 Database Administration 4 16
SELECT Course_Name ,CASE Credits WHEN 1 THEN 'One Credit' WHEN 2 THEN 'Two Credits' WHEN 3 THEN 'Three Credits' WHEN 4 THEN 'Four Credits' ELSE 'Do not know' END AS CreditAlias FROM Course_Table ; Now that we have an ELSE in our case statement we are guaranteed that nothing will fall through. An ELSE should be used in case you forgot a possibility and there was no match.
Page 439
Chapter 12
Interrogating the Data
Rules For a Valued Case Statement SELECT Course_Name ,CASE Credits WHEN 1 THEN 'One Credit' WHEN 2 THEN 'Two Credits' WHEN 3 THEN 'Three Credits' Else 'Credits not found' END AS CreditAlias FROM Course_Table ;
The column Credits (in blue) follows the word CASE. This is a valued case statement. The value is the column Credits.
Rules for a Valued CASE: 1. You can only check for equality 2. You can only check the value of the column Credits
There are two types of CASE statements. There is the Valued CASE and the Searched CASE. Above are the rules for the Valued CASE statement.
Page 440
Chapter 12
Interrogating the Data
Rules for a Searched Case Statement SELECT Course_Name No Value follows the ,CASE word CASE. This is WHEN Credits = 1030 is in page 3. Page 465
Chapter 13
Table Create and Data Types
The Building of a B-Tree For a Clustered Index (3 of 3) 1001
Intermediate Node 1001
Header
1030
Header
2000
Header
3000
6000
Intermediate Node 3000
Header
4000
Header
5000
Header
Root Node
Intermediate Node 6000
Header
7000
Header
8000
Header
Leaf Pages containing the actual data rows
Let's look at this B-Tree starting at the leaf level. Each leaf is an 8 K page that contains data rows. Each data row has a RowID containing the FileID:PageNo:RowNum, which takes up 8 bytes. The rows are sorted in each page by Employee_No. Each Intermediate node has a pointer to the first RowID and Employee_No for every leaf it is responsible for. The Root node has a pointer to the first RowID and Employee_No for each Intermediate node. As a leaf adds rows and expands past 8 K it splits. As an Intermediate node adds leafs and expands past 8 K it splits into two more Intermediate nodes. As a Root node continues to add more Intermediate node pointers and expands past 8 K it splits into two Root nodes. The reason they call it a B-Tree (Balanced Tree) is because every row can be retrieved at the exact same speed. Page 466
Chapter 13
Table Create and Data Types
The Row Offset Array is the Guidance System For Every Row Previous Page# - 1
PAGE 2
Next Page# - 3
1000 2 1 1001 100 Rafael Minal 90000
1000 2 2 1004 400 Kyle
Stover 60000
1000 2 3 1007 200 Sushma Davis 50000
1000 2 4 1020 200 May
Jones
60000
1000 2 5 1030 500 Dawn Wilson 50000 1000 2 6 1040 300 Red
Saylor 40000
1000 2 7 1050 300 Rex
Mason 60000
1000 2 8 1060 400 Kit
Wagner 50000
Row Offset Array (ROA)
The Row Offset Array guides every search. It holds the starting position of every row within the page. It is always in perfect descending order. (The first row (yellow) on the right represents the starting position of the first row on the page (also in yellow).
2 Bytes for each ROA slot
798 698 598 498 398 298 198 98
When a page of data is moved from disk into memory it is ready to be searched to produce an answer set. Every read of every row will first go through the Row Offset Array. The page above holds eight rows. In each case, the Row Offset Array will be read and that will guide SQL Server directly to the offset of the row. For example, to read the first row on the page SQL Server will go to the last slot in the Row Offset Array (in yellow) and it will know that the first row starts in byte 98. It will then go to byte 98 and read the row. Page 467
Chapter 13
Table Create and Data Types
The Row Offset Array Provides Two Search Options (1 of 2) Previous Page# - 1
PAGE 2
Next Page# - 3
1000 2 1 1001 100 Rafael Minal 90000 1000 2 2 1004 400 Kyle
Stover 60000
1 The first search option, which is the slowest is a sequential search.
1000 2 3 1007 200 Sushma Davis 50000 1000 2 4 1020 200 May
Jones
60000
1000 2 5 1030 500 Dawn Wilson 50000
1000 2 6 1040 300 Red
Saylor 40000
1000 2 7 1050 300 Rex
Mason 60000
1000 2 8 1060 400 Kit
Wagner 50000
Row Offset Array (ROA)
2 Bytes for each ROA slot
Each row will be read starting from the first row to the last. This is done when a query does not use an index. All Full Table Scans are sequential searches.
798 698 598 498 398 298 198 98
When a page of data is moved from disk into memory it is ready to be searched to produce an answer set. Every read of every row will first go through the Row Offset Array. The slowest search happens when there is no index being used. This is often called a Full Table Scan. There are eight rows in the above example. The Row Offset Array will be used with each read. SQL Server will read the last offset first (yellow color) and then read the first row in the page starting at byte 98. SQL Server will then read the second offset (pink color) and then read the second row in offset 198, and so on. Stay with me because there are two more reasons this design is always used. Page 468
Chapter 13
Table Create and Data Types
The Row Offset Array Provides Two Search Options (2 of 2) Previous Page# - 1
PAGE 2
Next Page# - 3
1000 2 1 1001 100 Rafael Minal 90000
1000 2 2 1004 400 Kyle
Jones
60000
1000 2 5 1030 500 Dawn Wilson 50000 1000 2 6 1040 300 Red
Saylor 40000
1000 2 7 1050 300 Rex
Mason 60000
1000 2 8 1060 400 Kit
Wagner 50000
Row Offset Array (ROA)
2
Stover 60000
1000 2 3 1007 200 Sushma Davis 50000
1000 2 4 1020 200 May
The second search option, which is the fastest is a Binary search.
2 Bytes for each ROA slot
798 698 598 498 398 298 198 98
This search uses an index and it is like using a phone book.
The first row read will be in the middle of the page. SQL Server will then know whether to move up or down because the rows are sorted.
When a page of data is moved from disk into memory it is ready to be searched to produce an answer set. Every read of every row will first go through the Row Offset Array. When the data on the page is sorted using a clustered index a binary search is fast. SQL Server reads the Row Offset Array to find the row in the middle. It can then move up or down depending on if it is too high or too low. It always cuts the remaining search in half. Imagine if we the query was searching for Employee 1050. The Row Offset Array would first go to the middle are read the row for employee 1020 (red arrow). It would then realize it was too low. It would then read the Row Offset Array to move to employee 1040. It would still be too low and then use the Row Offset Array to continue cutting the remaining rows in half, and next choose the row for Employee 1050. Found it! A binary search can be used on any query that takes advantage of an index on a page that is sorted. Page 469
Chapter 13
Table Create and Data Types
The Row Offset Array Helps With Inserts Previous Page# - 1
PAGE 2
Next Page# - 3
1000 2 1 1001 100 Rafael Minal 90000 1000 2 2 1004 400 Kyle
Stover 60000
1000 2 3 1007 200 Sushma Davis 50000 1000 2 4 1020 200 May
Jones
60000
1000 2 5 1030 500 Dawn Wilson 50000
Row Offset Array (ROA)
1000 2 6 1040 300 Red
Saylor 40000
1000 2 7 1050 300 Rex
Mason 60000
1000 2 8 1060 400 Kit
Wagner 50000
1000 2 9 1002 100 Bill
Mason 75000
2 Bytes for each ROA slot
798 698 598 498 398 298 198
The new row just inserted logically sorts as row 2, but SQL Server places it at the end of the page, but logically places it second in the Row Offset Array.
The row for Employee_No 1002 has just been inserted.
898 98
When a page is sorted using a clustered index the rows are sorted physically and logically. Let me explain. The Row Offset Array is always in perfect descending order. Above, you can see that each row is sorted physically by Employee_No. In a perfect world the Row Offset Array logically lists the rows on the page in perfect descending order and the rows are physically in perfect order within the page. However, when SQL is used for an insert statement, SQL Server will often write the row physically as the last row on the page (for speed), but it will still list the row in the Row Offset Array in perfect logical order. Notice we added a new row (in black) at the end of the page. Since this table is sorted with a clustered index on Employee_No you should notice that the row in black has an Employee_No of 1002. It should physically be the second row on the page. It is the second row according the Row Offset Array. Any sequential search will read the black row second. Page 470
Chapter 13
Table Create and Data Types
Adding an Index CREATE TABLE Emp_Intl ( Employee_No INTEGER ,Dept_No SMALLINT ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) ;
1
CREATE UNIQUE CLUSTERED INDEX Idx1 ON Emp_Intl (Employee_No);
2
CREATE INDEX Idx2 ON Emp_Intl (Dept_No);
Above, we have created a table called Emp_Intl. Each row in the table will contain a RowID. The RowID is 8bytes in size and contain FileID, PageID, SlotNo. A table can only have one clustered index. A clustered index sorts the rows of the table by the clustered key column value. In this example the rows will be sorted in ascending order by Employee_No. If the clustered index is unique then no duplicate Employee_No values will be permitted. A table can have numerous NON-CLUSTERED INDEXES. Think of them as pointers to data. They are implemented as B-TREES. More about B_TREES on the next slide. Page 471
Chapter 13
Table Create and Data Types
When Do I Create a Non Clustered Index? 1. Utilize columns that only contain a large number of distinct values. This might be a combination of last name and first name or a social security number. If there are many duplicates then SQL Server will perform a sequential scan instead. 2. When queries do not return large result sets. This goes back to having many distinct values. 3. Utilize on columns that are frequently involved in the WHERE clause that utilize equality searches. 4. Utilize these not on OLTP applications, but when you have large Decision-support-system (DSS) applications. DSS systems for when joins and grouping are frequently required. A best practice is to create multiple nonclustered indexes on columns involved in join and grouping operations, and a clustered index on the foreign keys. 5. In Cover query situations. A Cover query uses only the non clustered index to retrieve the data to satisfy the query instead of utilizing the table for a query. The answer set is said to be covered by the index.
Following the do's and don'ts on this page can enhance performance and prevent difficulties. Page 472
Chapter 13
Table Create and Data Types
B-Tree For Non Clustered Index on a Clustered Table (1 of 2) Previous Page# - null
Root
Next Page# - 2,3
Dept_No Employee_No
Non Clustered Index
100 200 300 400 500
1001 1007 1020 1040 1050 1004 1060 1030
Clustered Index Values
Leaf Page Previous Page# - 1
Leaf Page PAGE 2
Next Page# - 3
Previous Page# - 2
PAGE 3
Next Page# - Null
1000 2 1 1001 100 Rafael Minal 90000
1000 3 1 1030 500 Dawn Wilson 50000
1000 2 2 1004 400 Kyle
Stover 60000
1000 3 2 1040 300 Red
Saylor 40000
1000 2 3 1007 200 Sushma Davis 50000
1000 3 3 1050 300 Rex
Mason 60000
1000 2 4 1020 200 May
1000 3 4 1060 400 Kit
Wagner 50000
Jones
60000
398 298 198 98
398 298 198 98
A non clustered index will utilize a B-Tree node and it will always have a root node. A non clustered index will store the index value in order within the index node. When a non clustered index is created on a table that has a clustered index, then the index node will contain two values: index value and clustered index value(s). Above, we created a non clustered index on the column Dept_No. Since the base table also had a clustered index on Employee_No then the Employee_No values are also included. So, if your query wanted to retrieve all rows WHERE the Dept_No was equal to 400 then SQL Server would look in the non clustered index and see that there were two rows and they were Employee_No 1004 and 1060. Then, the system would use the clustered index to find them. Page 473
Chapter 13
Table Create and Data Types
B-Tree For Non Clustered Index on a Clustered Table (2 of 2) 100
Intermediate Node 100
Header
Header
500
Intermediate Node 300
200
Header
300
Header
Root Node
Intermediate Node
400
Header
500
Header
Header
Header
Header
Leaf Pages containing the actual data rows
We created a non clustered index on the column Dept_No on a table with a clustered index on Employee_No. A non clustered index in this example will sort by Dept_No and point to the row(s) Employee_No. A page always allocates 8 K for both disk and memory use. If a leaf, intermediate or even root node reaches the 8 K limit it will split into two nodes. Page 474
Chapter 13
Table Create and Data Types
Adding A Non Clustered Index To A Heap CREATE TABLE Emp_Intl ( Employee_No INTEGER ,Dept_No SMALLINT ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) ;
1
CREATE INDEX Idxlast ON Emp_Intl (Last_Name);
Above, we have created a table called Emp_Intl and it does not have a clustered index. This means that the rows are unordered and stored in a heap. Each row in the table will contain a RowID. The RowID is 8-bytes in size and contain FileID, PageID, SlotNo. This table could have many non clustered indexes, but we only created one on Last_Name. The next pages will show the B-Tree for the newly created non clustered index. Page 475
Chapter 13
Table Create and Data Types
B-Tree For Non Clustered Index on a Heap Table (1 of 2) Previous Page# - null
Next Page# - 2,3
Root
Last_Name RowID Davis Jones Minal Stover
Non Clustered Index
1000:1:2 1000:1:3 1000:1:1 1000:1:4
Row ID
Leaf Page FileID PageNum SlotNum
Physical Rows
1000
1
1
1001 100 Rafael Minal 90000
1000
1
2
1007 200 Sushma Davis 50000
1000
1
3
1020 200 May
Jones
1000
1
4
1004 400 Kyle
Stover 60000
60000
In a heap rows are not sorted
Row Identifier FREE SPACE
Row ID
A non clustered index will utilize a B-Tree node and it will always have a root node. A non clustered index will store the index value in order within the index node. When a non clustered index is created on a table that is a heap, then the index node will contain two values: index value and RowID(s). Above, we created a non clustered index on Last_Name. The index will contain every Last_Name sorted and the RowID. Page 476
Chapter 13
Table Create and Data Types
B-Tree for a Non Clustered Index on a Heap Table (2 of 2) Adams
Intermediate Node Adams
Header
Indy
Header
Header
Jones
Sims
Intermediate Node Jones
Header
Header
Root Node
Intermediate Node
Tan
Sims
Header
Header
Zin
Header
Header
Leaf Pages containing the actual data rows We created a non clustered index on the column Last_Name on a table that had no clustered index, which is considered an unordered heap of rows. A non clustered index in this example will sort by Last_Name and point to the RowID(s) on the leaf page. A page always allocates 8 K for both disk and memory use. If a leaf page, intermediate node or even root node reaches the 8 K limit it will split into two leafs or nodes. Page 477
Chapter 13
Table Create and Data Types
SELECT INTO DDL - The Original Order_Table
CREATE TABLE Order_Table (Order_Number INTEGER ,Customer_Number INTEGER ,Order_Date DATE ,Order_Total DECIMAL(10,2)) ; Using SELECT INTO
SELECT * INTO New_Order_Table FROM Order_Table ; DDL - The New_Order_Table
CREATE TABLE New_Order_Table (Order_Number INTEGER ,Customer_Number INTEGER ,Order_Date DATE ,Order_Total DECIMAL(10,2)) ; SELECT INTO allows for the quick creation of one table from another. Page 478
Chapter 13
Table Create and Data Types
SELECT INTO Using SELECT INTO
SELECT Order_Number ,Customer_Number ,Order_Date ,Order_Total INTO New_Order_Table1 FROM Order_Table ;
Using SELECT INTO with a Join
SELECT A.Order_Number ,A.Customer_Number ,B.Customer_Name ,A.Order_Date ,A.Order_Total INTO New_Order_Table2 FROM Order_Table AS A INNER JOIN Customer_Table AS B ON A.Customer_Number = B.Customer_Number ; These also work. SELECT INTO makes you so powerful. Page 479
Chapter 13
Table Create and Data Types
A Primary Key Constraint CREATE TABLE Emp_Intl (Employee_No INTEGER NOT NULL PRIMARY KEY ,Dept_No SMALLINT ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) ;
Leaf Page
Previous Page# - 1
PAGE 2
Next Page# - 3
1000 2 1 1001 100 Rafael Minal 90000 1000 2 2 1004 400 Kyle
Stover 60000
1000 2 3 1007 200 Sushma Davis 50000 1000 2 4 1020 200 May The data will be sorted by the Primary Key and no duplicates or Null values will be allowed
Jones
60000
398 298 198 98
When you want to uniquely identify a row then a primary key or a unique constraint is the way it is done. Whether or not you implement a primary key or a unique constraint, SQL Server will create a unique clustered index by default. Most of the time, creating a primary key automatically creates a unique clustered index, but it can optionally create a unique nonclustered index instead. A unique index always ensures data to being unique. This is enforced through the B-Tree index. Page 480
Chapter 13
Table Create and Data Types
The Difference between a Primary Key vs. Unique Constraint CREATE TABLE Emp_Intl (Employee_No INTEGER NOT NULL PRIMARY KEY ,Dept_No SMALLINT ,First_Name VARCHAR(12) ,Last_Name CHAR(20) Primary key ,Salary DECIMAL(8,2) );
CREATE TABLE Emp_Intl2 (Employee_No INTEGER ,Dept_No SMALLINT ,First_Name VARCHAR(12) Unique Constraint ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ,CONSTRAINT Emp_Constr UNIQUE (Employee_No) ); The only difference between a unique constraint or a unique clustered index and a primary key is that a primary key cannot have null values, and a unique constraint or unique clustered index can permit a single null value. Page 481
Chapter 13
Table Create and Data Types
Primary Key Foreign Key Constraints CREATE TABLE Dept_Intl (Dept_No INTEGER NOT NULL PRIMARY KEY ,Dept_Name CHAR (20) ,Mgr_No INTEGER Primary key ,Budget DECIMAL (10,2)); CREATE TABLE Emp_Intl Foreign key (Employee_No INTEGER ,Dept_No INTEGER FOREIGN KEY REFERENCES Dept_Intl(Dept_No) ON DELETE NO ACTION ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) ; INSERT INTO Dept_Intl Values (100, 'Sales', 2000000, 500000); INSERT INTO Emp_Intl Values (2000000, 200, 'Squiggy', 'Jones', 75000.00) ERROR: The INSERT statement conflicted with the FOREIGN Key constraint" FK__Emp_Intl__Dept___7E37BEF6".
In the example above we created a Primary Key on the Dept_No of Dept_Intl and we created a Foreign Key on the Dept_No column of the Emp_Intl table. When we tried to Insert an Employee that had an invalid Dept_No it was rejected. Page 482
Chapter 13
Table Create and Data Types
More Information about Foreign Key Constraints CREATE TABLE Emp_Intl Foreign key (Employee_No INTEGER ,Dept_No INTEGER FOREIGN KEY REFERENCES Dept_Intl(Dept_No) ON DELETE NO ACTION ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) ; The ON DELETE clause has the following options: NO ACTION – This specifies that the deletion fails if there is an error. CASCADE – This specifies that all the rows with foreign keys pointing to the deleted row should also be deleted. SET NULL - This specifies that all rows with foreign keys pointing to the deleted row need to be set to a NULL value. SET DEFAULT - This specifies that all rows with foreign keys pointing to the deleted row are to be set to their exact default value.
You cannot insert a row with a foreign key value, except NULL, if there is no candidate key with that value. The ON DELETE clause controls what actions are taken when you try to delete a row to which existing foreign keys point. The ON UPDATE clause will also define the actions that are to be taken if there is an attempt to update a candidate key value to which existing foreign keys point. These options also include NO ACTION, CASCADE, SET NULL and SET DEFAULT. Page 483
Chapter 13
Table Create and Data Types
Check Constraint CREATE TABLE Emp_Intl Not Null Constraint (Employee_No INTEGER NOT NULL ,Dept_No SMALLINT CHECT Constraint ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ,CONSTRAINT CheckDept CHECK (Dept_No Between 100 and 500) );
If you want to limit the values that can be input into a column you can use a CHECK constraint. A CHECK constraint specifies a Boolean, which will evaluate to either a TRUE, FALSE, or unknown value). All data input values into the column with the CHECK constraint that evaluate to FALSE are rejected. You can specify a CHECK constraint for every column if you desire, and you can even utilize multiple CHECK constraints for any particular column. Page 484
Chapter 13
Table Create and Data Types
Default Values CREATE TABLE Emp_Intl (Employee_No INTEGER NOT NULL DEFAULT ('') ,Dept_No SMALLINT ,First_Name VARCHAR(12) Above, we have directed ,Last_Name CHAR(20) SQL Server to put in an ,Salary DECIMAL(8,2) empty string (two single ); quotation marks with no space between them) as the default value.
If you don’t desire a NULL in a particular column, you can alternatively utilize a default value to indicate that the column has not yet been populated. All you need to do is specify a DEFAULT constraint by adding the DEFAULT clause right after saying NOT NULL.
Page 485
Chapter 13
Table Create and Data Types
Identity Columns CREATE TABLE Emp_Intl (Employee_No INTEGER IDENTITY (1,1) NOT NULL ,Dept_No SMALLINT ,First_Name VARCHAR(12) An Identity column will have two ,Last_Name CHAR(20) values: ,Salary DECIMAL(8,2) 1. Seed value ); 2. Increment amount A seed value is the value to start with (we chose 1) and then an increment amount, which is the amount to increment each new sequence number by. (we chose 1)
An Identity column can be assigned to a column to automatically generate a sequence of numbers. This is an excellent choice for columns that are system generated, such as an Employee_No. You can use it for only one column in a table, and you must specify both the seed (starting number) and increment values (how much to increment each sequence) for the number sequence generated. Page 486
Chapter 13
Table Create and Data Types
Computed Columns CREATE TABLE Product_Transactions (Product_ID INTEGER NOT NULL ,Sale_Date Date ,Price Decimal(10,2) The column TotalPrice ,Qty INTEGER is actually computed ,TotalPrice AS Price * Qty from two other ); columns (Price * Qty).
INSERT INTO Product_Transactions Values (1000, '2015-06-30', 100.25, 2)
SELECT * FROM Product_Transactions
Product_ID Sale_Date ______ Price ____ Qty _________ TotalPrice __________ _________
1000 06/30/2015 100.25
2
200.50
SQL Server allows you to define columns as computed values. Above, we have created a column called TotalPrice that computes this column from two other columns (Price * Qty). Page 487
Chapter 13
Table Create and Data Types
Compression CREATE TABLE Emp_Intl (Employee_No INTEGER NOT NULL ,Dept_No SMALLINT This table has ,First_Name VARCHAR(12) Row-level compression ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) WITH (DATA_COMPRESSION = ROW) ;
ALTER TABLE Emp_Intl REBUILD WITH (DATA_COMPRESSION = PAGE);
CREATE TABLE Emp_Intl (Employee_No INTEGER NOT NULL ,Dept_No SMALLINT ,First_Name VARCHAR(12) ,Last_Name CHAR(20) ,Salary DECIMAL(8,2) ) WITH (DATA_COMPRESSION = PAGE) ;
We have altered the table to Page-level compression
This is how you create a table with Page-level compression
Table compression has two levels. They are row-level and page-level. Row-level compression applies a more compact storage format for each row in the table. Page-level compression will include the row-level compression, but it will also utilize additional compression algorithms that will be performed at the page level. Page 488
Chapter 13
Table Create and Data Types
ROWVERSION Every time an INSERT or UPDATE is performed against a row that has a column with a rowversion datatype, the value of the rowversion column in increased.
CREATE TABLE Employee_Table ( Employee_No INTEGER ,Version_Col ROWVERSION) ;
Employee_No ___________ 1 2
Version_Col _______________ 00000000000007D1 00000000000007D2
UPDATE Employee_Table SET Employee_No = 7 WHERE Employee_No = 1 Employee_No ___________ 7 2
Version_Col _______________ 00000000000007D3 00000000000007D2
ROWVERSION columns are helpful for row versioning. Page 489
Chapter 14
Page 490
View Functions
Chapter 14
View Functions
Chapter 14 – View Functions
"Be the change that you want to see in the world." - Mahatma Gandhi
Page 491
Chapter 14
View Functions
The Fundamentals of Views View Fundamentals A view is a virtual table. A view may define a subset of columns A view can even define a subset of rows if it has a WHERE clause A view never duplicates data or stores the data separately Views provide security
View Advantages
An additional level of security is provided. Helps the business user not miss join conditions. Help control read and update privileges. Unaffected when new columns are added to a table. Unaffected when a column is dropped unless its referenced in the view. View Recommendations
The above is designed to introduce View fundamentals and View advantages. Page 492
Chapter 14
View Functions
Creating a Simple View to Restrict Sensitive Columns Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name __________ Jones Squiggy Smythe Richard Chambers Mandee Coffing Billy Smith John Larkins Loraine Strickling Cletus Reilly William Harrison Herbert
Salary _______ 32800.50 64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 36000.00 54500.00
CREATE View Employee_V AS SELECT Employee_No ,First_Name ,Last_Name ,Dept_No FROM Employee_Table ; The purposes of views is to restrict access to certain columns, derive columns or Join Tables, and to restrict access to certain rows (if a WHERE clause is used). This view does not allow the user to see the column salary.
Page 493
Chapter 14
View Functions
Creating a Simple View to Restrict Rows Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name __________ Jones Squiggy Smythe Richard Chambers Mandee Coffing Billy Smith John Larkins Loraine Strickling Cletus Reilly William Harrison Herbert
Salary _______ 32800.50 64300.00 48850.00 41888.88 48000.00 40200.00 54500.00 36000.00 54500.00
CREATE VIEW Employee_View AS SELECT First_Name ,Last_Name ,Dept_No ,Salary FROM Employee_Table WHERE Dept_No IN (300, 400) ;
The purposes of views is to restrict access to certain columns, derive columns or Join Tables, and to restrict access to certain rows (if a WHERE clause is used). This view does not allow the user to see information about rows unless the rows have a Dept_No of either 300 or 400. Page 494
Chapter 14
View Functions
Basic Rules for Views No ORDER BY inside the View CREATE (exceptions exist) All Aggregation needs to have an ALIAS Any Derived columns (such as Math) needs an ALIAS
Why do these two columns need aliases?
CREATE View DeptSal_V AS SELECT Dept_No ,SUM(Salary) as SumSal , SUM(Salary) / 12 as MonthSal FROM Employee_Table You don't put an GROUP BY Dept_No; Order By in the view creation.
So we can bring them back in the SELECT query
SELECT Dept_No ,SumSal FROM DeptSal_V Order By 1 ;
Above are the basic rules of Views with excellent examples.
Page 495
Users put the Order By when selecting from the view
Chapter 14
View Functions
How to Modify a View
1
2
3
SELECT OBJECT_DEFINITION (OBJECT_ID (‘Employee_V’))
CREATE View Employee_V SELECT Employee_No ,First_Name ,Last_Name ,Dept_No ,Salary FROM Employee_Table ;
AS
Run the OBJECT_DEFINITION function
The CREATE statement returns in the answer set. Copy and paste it in your query window.
ALTER View Employee_V AS SELECT Employee_No Change the word ,First_Name CREATE to ,Last_Name ALTER, make ,Dept_No your changes and hit execute. FROM Employee_Table ;
Notice that the OBJECT_DEFINITION function takes the OBJECT_ID of the view as input. When a view is altered, the OBJECT_ID of the view stays the same.
Page 496
Chapter 14
View Functions
Why Bother To ALTER A View So that stored procedures using the view will continue to work
So that other views using the view will continue to work So that permissions stay intact SELECT OBJECT_ID (‘Employee_V’); 965578478
ALTER View Employee_V AS SELECT Employee_No ,First_Name ,Last_Name ,Dept_No FROM Employee_Table ;
SELECT OBJECT_ID (‘Employee_V’); 965578478
When a view is altered as opposed to dropped and recreated, the view’s OBJECT_ID remains the same. This means that references to the view continue to work. Page 497
Chapter 14
View Functions
Two Exceptions to the ORDER BY Rule inside a View CREATE VIEW Top_Sal_V AS SELECT TOP 3 * The TOP command FROM Employee_Table goes with Order By ORDER BY Salary DESC; like bread goes with butter.
Create view Sales_Olap_V AS SELECT Product_ID, Sale_Date, Daily_Sales ,Sum(Daily_Sales) OVER (ORDER BY Daily_Sales Rows Unbounded Preceding) as "CSUM" FROM Sales_Table ; Every ANSI Ordered Analytic has an Order By in it naturally
There are EXCEPTIONS to the ORDER BY rule. The TOP command allows a view to work with an ORDER BY inside. ANSI OLAP statements also work inside a View. Page 498
Chapter 14
View Functions
How to Get HELP with a View EXEC sp_helptext ‘Employee_V’
Running the System Stored Procedure sp_helptext on a view name, returns the CREATE statement for the view.
Page 499
Chapter 14
View Functions
How to Get HELP with a View SELECT OBJECT_DEFINITION (OBJECT_ID (‘Employee_V’)
Isn’t it nice to know that there are multiple ways to do the same thing in SQL Server.
Page 500
Chapter 14
View Functions
Views sometimes CREATED for Formatting or Row Security CREATE VIEW empl_200_v AS SELECT Employee_No AS Emp_No ,Last_Name AS Last ,FORMAT(salary/12, 'C') AS Mnth_Sal FROM Employee_Table WHERE Dept_No = 200 ;
SELECT * FROM Empl_200_v ORDER BY Mnth_Sal ;
Emp_No _______
Last_Name _________
Mnth_Sal ________
1324657
Coffing
$3,490.74
1333454
Smith
$4,000.00
Views are designed to do many things. In the example above, this view formats and derives data, limits columns, and also limits the rows coming back with a WHERE. Page 501
Chapter 14
View Functions
Creating a View to Join Tables Together
This view is designed to join two tables together. By creating a view, we have now made it easier for the user community to join these tables by merely selecting the columns you want from the view. The view exists now in the database sql_views and accesses the tables in sql_class.
Page 502
Chapter 14
View Functions
You Select From a View
Once the view is created, then users can query them with a SELECT statement. Above, we have queried the view we created to join the employee_table to the department_table (created on previous page). Users can select all columns with an asterisk, or they can choose individual columns (separated by a comma). Above, we selected all columns from the view.
Page 503
Chapter 14
View Functions
Another Way to Alias Columns in a View CREATE CREATE VIEW E_View (Emp_Nbr, Last, Mnth_Sal) AS SELECT Employee_No ,Last_Name Option 1: Aliases ,Salary/12 FROM Employee_Table can be here WHERE Dept_No = 200 ;
SELECT * FROM E_View ORDER BY Mnth_Sal ;
Emp_No _______
Last_Name _________
Mnth_Sal _________
1324657
Coffing
3490.74
1333454
Smith
4000.00
Will this View CREATE work or will it error? It works fine because it’s aliased above!
Page 504
Chapter 14
View Functions
The Standard Way Most Aliasing is done CREATE VIEW emp_v2 AS SELECT Employee_No ,Last_Name ,Salary/12 as Sal_Monthly FROM Employee_Table Option 2: WHERE Dept_No = 200 ; The most popular form of aliasing
SELECT * FROM Emp_v2 ORDER BY Sal_Monthly ;
Emp_No _______ 1324657 1333454
Last_Name _________ Coffing Smith
Sal_Monthly ___________ 3490.74 4000.00
The ALIAS for Salary / 12 that’ll be used in this example is Sal_Monthly and this form of aliasing is most often used.
Page 505
Chapter 14
View Functions
What Happens When Both Aliasing Options Are Present CREATE VIEW emp_v3 (Emp_Nbr, Last, Mnth_Sal) AS Once you alias here SELECT Employee_No that is the alias ,Last_Name ,Salary/12 as Sal_Mnth FROM Employee_Table This alias is not recognized WHERE Dept_No = 200 ; SELECT * FROM Emp_v3 ORDER BY 3 ;
Emp_No _______ 1324657 1333454
Last_Name _________ Coffing Smith
Mnth_Sal _________ $3,490.74 $4,000.00
The ALIAS for Salary / 12 that’ll be used in this example is Mnth_Sal. It came first at the top, even though it is aliased in the SELECT list also.
Page 506
Chapter 14
View Functions
Resolving Aliasing Problems in a View CREATE CREATE VIEW emp_v3 (Emp_Nbr, Last, Mnth_Sal) AS Once you alias here SELECT Employee_No that is the alias ,Last_Name ,Salary/12 as Sal_Mnth FROM Employee_Table This alias is not recognized WHERE Dept_No = 200 ;
SELECT * FROM Emp_v3 ORDER BY Sal_Mnth ; What happens when this query runs?
What will happen in the above query? Page 507
Chapter 14
View Functions
Answer to Resolving Aliasing Problems in a View CREATE CREATE VIEW emp_v3 (Emp_Nbr, Last, Mnth_Sal) AS Once you alias here SELECT Employee_No that is the alias ,Last_Name ,Salary/12 as Sal_Mnth FROM Employee_Table This alias is not recognized WHERE Dept_No = 200 ; SELECT * FROM Emp_v3 ORDER BY Sal_Mnth ; What happens when this query runs?
Error – Sal_Mnth is unrecognized The query above errors because Sal_Mnth is an unrecognized alias. That is because we did our aliasing at the top so this makes the alias right after Salary/12 non-valid for use when querying the view.
Page 508
Chapter 14
View Functions
Aggregates on View Aggregates CREATE VIEW Aggreg_Order_v AS SELECT Customer_Number ,COUNT(Order_Total) AS Order_Cnt ,SUM(Order_Total) AS Order_Sum ,AVG(Order_Total) AS Order_Avg FROM Order_Table GROUP BY Customer_Number, Yr_Mth_Orders ;
SELECT Customer_Number ,Order_Sum FROM Aggreg_Order_v ; Customer_Number Order_Sum _______________ __________ 31323134 5111.47 87323456 15231.62 11111111 8005.91 11111111 12347.53 57896883 23454.84
SELECT SUM (Order_Sum) FROM Aggreg_Order_v ;
SUM(Order_Sum) _______________
The examples above show how we put a SUM on the aggregate Order_Sum Page 509
64151.37
Chapter 14
View Functions
Altering A Table CREATE VIEW Emp_HR_v AS SELECT Employee_No ,Dept_No ,Last_Name ,First_Name FROM Employee_Table ; Altering the actual Table
ALTER TABLE Employee_Table ADD Mgr_No INTEGER ; Will the View STILL run?
SELECT * FROM Emp_HR_v;
YES! This view will run after the table has added an additional column!
Page 510
Chapter 14
View Functions
Altering a Table after a View has been Created CREATE VIEW Emp_HR_v4 AS SELECT * FROM Employee_Table4 ; Altering the actual Table
ALTER TABLE Employee_Table4 ADD Mgr_No INTEGER ; Will the View STILL run?
SELECT * FROM Emp_HR_v4;
YES!
Only columns present when the view was created will be visible.
This view runs after the table has added an additional column, but it won’t include Mgr_No in the view results even though there is a SELECT * in the view. The View includes only the columns present when the view was CREATED. Page 511
Chapter 14
View Functions
A View that Errors After An ALTER CREATE VIEW Emp_HR_v5 AS SELECT Employee_No ,Dept_No ,Last_Name ,First_Name FROM Employee_Table5 ; Altering the actual Table
ALTER TABLE Employee_Table5 DROP COLUMN Dept_No; Will the View STILL run?
SELECT * FROM Emp_HR_v5;
ERROR
This view will NOT run after the table has dropped a column referenced in the view. Page 512
Chapter 14
View Functions
Troubleshooting a View CREATE VIEW Emp_HR_v6 AS SELECT * FROM Employee_Table6 ; Altering the actual Table
ALTER TABLE Employee_Table6 DROP COLUMN Dept_No ; Will the View STILL run?
SELECT * FROM Emp_HR_v6;
Error
This view will NOT run after the table has dropped a column referenced in the view even though the View was CREATED with a SELECT *. At View CREATE Time, the columns present were the only ones the view considered responsible for, and Dept_No was one of those columns. Once Dept_No was dropped, the view no longer works.
Page 513
Chapter 14
View Functions
Updating Data in a Table through a View CREATE VIEW Emp_HR_v8 AS SELECT * FROM Employee_Table8; Updating the table through the View
UPDATE Emp_HR_V8 SET Salary = 88888.88 WHERE Employee_No = 2000000; Will the View still run?
SELECT * FROM Employee_Table8 WHERE Employee_No = 2000000; Employee_No Dept_No __________ Last_Name ____________ ________ 2000000
?
Jones
First_Name __________
Salary _______
Squiggy
88888.88
You can UPDATE a table through a View if you have the RIGHTS to do so.
Page 514
Chapter 14
View Functions
Loading Data through a View
New row Inserted
You can actually utilize a view to load data. Page 515
Chapter 14
View Functions
Maintenance Restrictions on a Table through a View Any INSERT, UPDATE, or DELETE statement on a view must apply to the columns and rows on only one table.
A view cannot be used for maintenance if it:
Derives data – because it does not undo the math or calculation Performs aggregation – because this eliminates detail data
Uses OLAP functions – because OLAP data is calculated Uses a DISTINCT or GROUP BY – eliminates duplicate rows Uses TOP and WITH CHECK OPTION together - because we cannot be sure that all data would stay visible using the view after the maintenance
Perform maintenance on a table through a view, but see the restrictions above first.
Page 516
Chapter 15
Page 517
Data Manipulation Language (DML)
Chapter 15
Data Manipulation Language (DML)
Chapter 15 – Data Manipulation Language (DML)
“I tried to draw people more realistically, but the figure I neglected to update was myself.” - Joe Sacco
Page 518
Chapter 15
Data Manipulation Language (DML)
INSERT Syntax # 1 The following syntax of the INSERT does not use the column names as part of the command. Therefore, it requires that the VALUES portion of the INSERT match each and every column in the table with a data value or a NULL.
INSERT [ INTO ] VALUES ( [ ..., ] ;
The INSERT statement is used to put new row(s) into a table. A status is the only returned value from the database; no rows are returned to the user. This INSERT syntax requires either a data value or a NULL for all the columns in a table. When executed this code places a single new row into a table.
Page 519
Chapter 15
Data Manipulation Language (DML)
INSERT Example with Syntax 1 INSERT INTO Employee_Table VALUES ( 20, 5, 'Jones', NULL , 45000, 15 ) ;
20
5
Jones NULL 45000
15
The Employee_Table was created with these columns in this order: Employee_No ,Dept_No, Last_Name, First_Name, Salary, Mgr_No
After the execution of the above INSERT, there is a new row with the integer value of 1 going into Column1, the integer value of 5 going into Column2, the character value of Jones going into Column3, a NULL value going into Column4 , and an integer value of 15 going into Column5. The NULL expressed in the VALUES list is the literal representation for no data.
Page 520
Chapter 15
Data Manipulation Language (DML)
INSERT Syntax #2 The syntax of the second type of INSERT follows:
INSERT [ INTO ] ( [..., ] VALUES ( [..., ] ;
This is another form of the INSERT statement that can be used when some of the data is not available. It allows for the missing values (NULL) to be eliminated from the list in the VALUES clause. It is also the best format when the data is arranged in a different sequence than the CREATE TABLE. Page 521
Chapter 15
Data Manipulation Language (DML)
INSERT Example with Syntax 2 INSERT INTO Employee_Table (Employee_No ,Dept_No, First_Name, Last_Name, Mgr_No) VALUES( 23, 5,'Srini', 'Akari', 15 ) ;
23
5
Srini Akari NULL
15
Notice that only five columns were inserted and that there are six columns in the row. The system filled the empty columns with Null.
The above statement incorporates both of the reasons to use this syntax. First, notice that the column names , Last_Name and First_Name have been switched, to match the data values. Also, notice that Salary does not appear in the column list, therefore it is assumed to be NULL. This is a good format to use when the data is coming from a file and does not match the order of the table columns. Page 522
Chapter 15
Data Manipulation Language (DML)
INSERT Example with Syntax 3 INSERT INTO Employee_Table VALUES ( 25, 6, 'Jones', NULL , 45000, 15 ) , ( 26, 6, 'Smith', 'Roopa' , 45000, 15 ) , ( 27, 6, NULL, 'Ricardo' , 45000, 15 ) ;
25
6
Jones NULL
45000 15
26
6
Smith Roopa 45000 15
27
6
NULL Ricardo 45000 15
The third form of the INSERT statement can be used to insert numerous rows at the same time. Page 523
Chapter 15
Data Manipulation Language (DML)
INSERT/SELECT Command The syntax of the INSERT / SELECT is:
INSERT [ INTO ] SELECT [..., ] FROM ;
Although the INSERT is great for adding a single row not currently present in the system, an INSERT/SELECT is even better when the data already exists within SQL Server. In this case, the INSERT is combined with a SELECT. However, no rows are returned to the user. Instead, they go into the table as new rows. The SELECT reads the data values from the one or more columns in one or more tables and uses them as the values to INSERT into another table. Simply put, the SELECT takes the place of the VALUES portion of the INSERT. This is a common technique for building data marts, interim tables and temporary tables. It is normally a better and much faster alternative than extracting the rows to a data file, then reading the data file and inserting the rows using a utility. Page 524
Chapter 15
Data Manipulation Language (DML)
INSERT/SELECT Example using All Columns (*) When all columns are desired to make an exact copy of the second table and both tables have the exact same number of columns in the exact same order with the exact same data types; an * may be used in the SELECT to read all columns without a WHERE clause, as in the example below:
INSERT INTO Employee_Table_New SELECT * FROM Employee_Table_Original ;
Like all SELECT operations without a WHERE clause, a full table scan occurs and all the rows of the second table are inserted into My_New_Table, using only the data values from the columns listed. Page 525
Chapter 15
Data Manipulation Language (DML)
INSERT/SELECT Example with Less Columns When fewer than all the columns are desired, either of the following INSERT / SELECT statements will do the job:
INSERT INTO Employee_Table_New (Employee_No, Last_Name, First_Name, Hire_Dt) SELECT Employee_No, Last_Name, First_Name, '2013/01/15' FROM Employee_Table_Original ;
INSERT INTO Employee_Table_Newl (Employee_No, First_Name, Last_Name, Hire_Dt) SELECT Employee_No, First_Name, Last_Name, GETDATE() FROM Employee_Table_Original ; In both of the above examples, only the specified columns are populated in Employee_Table_New. In the first INSERT, the data is a literal date. The second INSERT uses the GETDATE() function. Both are acceptable, depending on what is needed. Working with the same concept of a normal INSERT, when using the column names the only data values needed are for these columns and they must be in the same sequence as the column list, not the CREATE TABLE. Therefore, omitted data values or column names become a NULL data value. Page 526
Chapter 15
Data Manipulation Language (DML)
The UPDATE Command Basic Syntax UPDATE [ FROM [AS ] ] SET = { | } [..., = | ] [ WHERE ] [ AND … ] [ OR … ] ;
The UPDATE statement is used to modify data values in one or more columns of one or more existing rows. A status is the only returned value from the database; no rows are returned to the user. When business requirements call for a change to be made in the existing data, then the UPDATE is the SQL statement to use. In order for the UPDATE to work, it must know a few things about the data row(s) involved. Like all SQL, it must know which table to use for making the change, which column or columns to change and the change to make within the data.
Page 527
Chapter 15
Data Manipulation Language (DML)
Two UPDATE Examples UPDATE Employee_Table_New SET Last_Name = 'Singh' ,First_Name = 'Anita' ,Hire_DT = '2015/04/10' WHERE Employee_No = 20 ;
UPDATE Employee_Table_New SET Hire_DT = '2016/04/10' WHERE Last_Name = 'Singh' AND First_Name = 'Anita' ;
The first UPDATE command modifies all rows for Employee_No 20. It changes the values in three columns with new data values provided after the equal sign (=): The next UPDATE uses the same table as the above statement. The UPDATE determines which row(s) to modify with compound conditions written in the WHERE clause based on values stored in other columns. Page 528
Chapter 15
Data Manipulation Language (DML)
Subquery UPDATE Command Syntax UPDATE [ FROM [AS ] ] SET = { | } [..., = | ] WHERE [..., ] IN ( SELECT [..., ] FROM [ WHERE … ] ) ;
Sometimes it is necessary to update rows in a table when they match rows in another table. To accomplish this, the tables must have one or more columns in the same domain. The matching process then involves either a subquery or join processing. Page 529
Chapter 15
Data Manipulation Language (DML)
Example of Subquery UPDATE Command Employee_Table1 can be changed based on Employee_Table2. The following UPDATE uses a subquery operation to accomplish the operation:
UPDATE Employee_Table1 SET Salary = 50000 WHERE Employee_No IN (SELECT Employee_no FROM Employee_Table2 WHERE Salary > 60000) ;
Sometimes it is necessary to update rows in a table when they match rows in another table. To accomplish this, the tables must have one or more columns in the same domain. The matching process then involves either a subquery or join processing.
Page 530
Chapter 15
Data Manipulation Language (DML)
Join UPDATE Command Syntax UPDATE SET = { | } [..., = | ] [ FROM [ AS ] ] WHERE [.] = [.] [ AND ] [ OR ] ;
When adding an alias to the UPDATE, the alias becomes the table name and MUST be used in the WHERE clause when qualifying columns.
Page 531
Chapter 15
Data Manipulation Language (DML)
Example of an UPDATE Join Command Employee_Table1 can be changed based on Employee_Table2. The following UPDATE uses a subquery operation to accomplish the operation:
UPDATE Employee_Table1 SET Salary = 50000 FROM Employee_Table2 AS Emp_Orig WHERE Employee_Table1.Employee_No = Emp_Orig.Employee_No ;
Sometimes it is necessary to update rows in a table when they match rows in another table. To accomplish this, the tables must have one or more columns in the same domain. The matching process then involves either a subquery or join processing. Page 532
Chapter 15
Data Manipulation Language (DML)
Fast UPDATE The following SELECT INTO “updates” the values in Last_Name and Hire_Dt columns in every row of Employee_Table_New, using Employee_Table_Original
SELECT Employee_No ,CONCAT(Last_Name,', ') AS Last_Name ,First_Name , '2013/01/15' AS Hire_Dt INTO Employee_Table_New FROM Employee_Table_Original ;
When the above command finishes, there is a new table and it has the updated information. This can often be a more efficient way to UPDATE data than UPDATING the data in an existing table. Page 533
Chapter 15
Data Manipulation Language (DML)
The DELETE Command Basic Syntax DELETE [ FROM ] [ AS ] [ WHERE condition ] ;
The DELETE statement has one function and that is to remove rows from a table. A status is the only returned value from the database; no rows are returned to the user. One of the fastest things that SQL Server does is to remove ALL rows from a table. Be Very CAREFUL with DELETE. It can come back to bite you if your not careful. Page 534
Chapter 15
Data Manipulation Language (DML)
Two DELETE Examples to DELETE ALL Rows in a Table DELETE FROM Employee_Table1 ;
DELETE Employee_Table1 ;
Both examples will delete all the rows in the table. Since the FROM is optional, the second example still removes all rows from a table and executes exactly the same as the above statement. Page 535
Chapter 15
Data Manipulation Language (DML)
To DELETE or to TRUNCATE To DELETE or to TRUNCATE? That is the question. The answer is: TRUNCATE if you can.
TRUNCATE TABLE Employee_Table1 ;
TRUNCATING uses fewer system resources, it does not require row logging, and it is faster. So TRUNCATE if you can. Page 536
Chapter 15
Data Manipulation Language (DML)
TRUNCATE is Different from DELETE TRUNCATE deallocates a tables data and index pages. The deallocation of pages is logged.
DELETE removes rows from a table. The removal of each row is logged.
A table that is to be TRUNCATED cannot : A table that is to be DELETED can : Have a foreign key reference from another table Be part of an indexed view. Be published using merge or transactional replication
So TRUNCATE if you can. Page 537
Chapter 15
Data Manipulation Language (DML)
A DELETE Example Deleting only Some of the Rows
DELETE FROM Employee_Table1 WHERE Employee_No = 1121334 ;
The DELETE example above only removes the row that contained Employee_No of 1121334 and leaves all other rows in the table. Page 538
Chapter 15
Data Manipulation Language (DML)
Want to know How Many Rows were just changed SELECT @@ROWCOUNT ;
IF @@ROWCOUNT = 0 PRINT 'Warning: No rows were updated‘ ;
@@ROWCOUNT will return how many rows were affected by the last statement. Consider using it after INSERT, DELETE, UPDATE, and MERGE processing. Page 539
Chapter 15
Data Manipulation Language (DML)
Subquery and Join DELETE Command Syntax The subquery syntax for the DELETE statement follows:
DELETE WHERE [..., ] IN ( SELECT [..., ] FROM [ AS ] [ WHERE condition … ] ) ;
The join syntax for the DELETE statement follows:
DELETE [ FROM [ AS ] ] WHERE .=. [ AND ] [ OR ] ;
You may be asked to delete rows in one table based on data from a different table. Sometimes it is desirable to delete rows from one table based on their existence in or by matching a value stored in another table. To access these rows from another table for comparison, a subquery or a join operation can be used. Page 540
Chapter 15
Data Manipulation Language (DML)
Example of Subquery DELETE Command The DELETE below is to remove rows from Employee_Table_Original for employees with unknown salaries in Employee_Table_New. This DELETE uses a subquery operation to accomplish the DELETE.
DELETE FROM Employee_Table_Original WHERE Employee_No IN ( SELECT Employee_No FROM Employee_Table_New WHERE Salary IS NULL ) ;
The above uses a Subquery and the DELETE command.
Page 541
Chapter 15
Data Manipulation Language (DML)
Example of Join DELETE Command The DELETE below is to remove rows from Employee_Table_Original for employees with unknown salaries in Employee_Table_New. This DELETE uses a join operation to accomplish the DELETE.
DELETE Employee_Table_Original FROM Employee_Table_New AS New WHERE Employee_Table_Original.Employee_No = New.Employee_No AND New.Salary IS NULL ; The previous example could also be written using the format below. In this example an alias can be used for the table being DELETED.
DELETE Employee_Table_Original FROM Employee_Table_Original AS Orig INNER JOIN Employee_Table_New AS New ON Orig.Employee_No = New.Employee_No WHERE Salary IS NULL ‘ ;
The above examples use Joins in the DELETE statement and are equivalent to the subquery example we saw on the previous page. Page 542
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Want to synchronize data in two different tables?
MERGE INTO can be used to get your tables in synch.
MERGE INTO involves performing an UPDATE, INSERT, or DELETE on a target table based on data in a source table. The target and source tables are joined on common column(s) or key(s). A target table is modified to reflect the data in a source table.
MERGE merges a source row set into a target table based on whether there is a MATCH or whether there is NOT a MATCH. If there is a MATCH and data changed an UPDATE can be made, but if there is a NOT a MATCH an INSERT or DELETE can be made.
Page 543
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Want to synchronize data in two different tables? MERGE INTO can be used to get your tables in synch.
SELECT Employee_No, Dept_No, Last_Name, First_Name, Salary FROM Employee_Table_Original;
SELECT Employee_No, Dept_No, Last_Name, First_Name, Salary FROM Employee_Table_New;
We are going to update Employee_Table_Original based on data in Employee_Table_New using a MERGE INTO. Page 544
Chapter 15
Data Manipulation Language (DML)
MERGE INTO MERGE INTO Employee_Table_Original AS target USING (SELECT Employee_No, Dept_No, Last_Name, First_Name, Salary FROM Employee_Table_New) AS source (Employee_No, Dept_No, Last_Name, First_Name, Salary) ON (target.Employee_No = source.Employee_No)
WHEN MATCHED AND source.Salary = 0 THEN DELETE WHEN MATCHED THEN UPDATE SET target.Employee_No = source.Employee_No ,target.Dept_No = source.Dept_No ,target.Last_Name = source.Last_Name ,target.First_Name = source.First_Name ,target.Salary = source.Salary WHEN NOT MATCHED THEN INSERT (Employee_No, Dept_No, Last_Name, First_Name, Salary) VALUES (source.Employee_No, source.Dept_No, source.Last_Name, source.First_Name, Source.Salary) ;
MERGE INTO can be very powerful when you need to perform DELETES, UPDATES, and INSERTS to one table based on values in another table.
Page 545
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Take A Look At This!
SELECT Employee_No, Dept_No, Last_Name, First_Name, Salary FROM Employee_Table_Original;
John Smith who had a salary of 0 in the New table was DELETED. Mandee Chanbers who had a change in salary amout was UPDATED. Billy Coffing who existed in the New table but not the Original table was INSERTED.
Is this amazing or what? Page 546
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Can also be used to perform UPDATES or INSERTS based on supplied values.
USING (VALUES (3000000, 400, 'Coffing', 'TeraTom', 75000.00)) AS Source (Emp, Dept, Las, Fir, Sal)
See next page for complete syntax.
Matches operations can be based on another table or on values supplied. Page 547
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Example that Matches Squiggy Jones is in the Employee_Table with a salary of 32500.00.
Employee_No First_Name ________ Salary ____________ Dept_No ________ Last_Name _________ __________ 2000000
?
Jones
Squiggy
32500.00
MERGE INTO Employee_Table AS Target USING (VALUES (2000000, NULL, 'Jones', 'Squiggy', 40000.00)) AS Source (Emp, Dept, Las, Fir, Sal) I will now perform a ON Target.Employee_No = Source.Emp MERGE that will WHEN MATCHED THEN have a MATCH. UPDATE SET Salary = Source.Sal WHEN NOT MATCHED BY TARGET THEN INSERT (Employee_No, Dept_No, Last_Name, First_Name, Salary ) VALUES (Emp, Dept, Las, Fir, Sal); SELECT * FROM Employee_Table WHERE Employee_No = 2000000; Employee_No First_Name ________ Salary ____________ Dept_No ________ Last_Name _________ __________ 2000000
Page 548
?
Jones
Squiggy
40000.00
Chapter 15
Data Manipulation Language (DML)
MERGE INTO Example that does NOT Match I will now perform a MERGE that will have a NOT MATCH situation because Employee_No 3000000 does not exist in the Employee_Table.
MERGE INTO Employee_Table AS Target USING (VALUES (3000000, 400, 'Coffing', 'TeraTom', 75000.00)) AS Source (Emp, Dept, Las, Fir, Sal) ON Target.Employee_No = Source.Emp WHEN MATCHED THEN UPDATE SET Salary = Source.Sal WHEN NOT MATCHED BY TARGET THEN INSERT (Employee_No, Dept_No, Last_Name, First_Name, Salary ) VALUES (Emp, Dept, Las, Fir, Sal) ; SELECT * FROM Employee_Table WHERE Employee_No = 3000000; Employee_No First_Name ____________ Dept_No ________ Last_Name _________ __________ 3000000
400
Coffing
TeraTom
Salary ________ 75000.00
There is no employee 3000000 that exists before the MERGE INTO statement, but once the MERGE INTO statement runs (and doesn’t find a Match) it INSERTS employee 3000000 into the table.
Page 549
Chapter 15
Data Manipulation Language (DML)
User Defined Functions (UDFs) UDFs can be of the following types: • Scalar • Table • System
Users create Scalar and Table Functions. SQL Server creates and provides System Functions.
You have likely used System Functions in the past. Have you ever used: SELECT GETDATE( ) SELECT CURRENT_TIMESTAMP or @@TOTAL_ERROR
If so, you have used System Functions. User Defined Functions (UDFs) accept input, perform operations and return output. UDFs can be of the following types: Scalar, Table, or System. Scalar Functions return one data value. Table Functions return a table.
Page 550
Chapter 15
Data Manipulation Language (DML)
User Defined Functions (UDFs) Scalar and Table User Defined Functions (UDFs) can be created using: SQL Server or Microsoft .NET Framework Common Language Runtime(CLR). Consider developing CLR functions for manipulating strings, performing calculations, and encapsulating business logic.
Possibly : Visual Basic .NET Or Visual C#
On the next page we will see an example of a UDF created using SQL Server. Page 551
Chapter 15
Data Manipulation Language (DML)
User Defined Function Example CREATE FUNCTION ufnGetDeptSalary(@DeptNo int)
RETURNS Decimal(8,2) AS -- Returns the total annual salary amount for a department BEGIN DECLARE @ret Decimal(8,2); SELECT @ret = SUM(Emp.Salary) FROM Employee_Table Emp WHERE Emp.Dept_No = @DeptNo
The function is now created
IF (@ret IS NULL) SET @ret = 0; RETURN @ret; END ; SELECT Dept_No, dbo.ufnGetDeptSalary(Dept_No)AS TotalAnnualSalary FROM Employee_Table And can be used GROUP BY Dept_No ; in a query
The example above shows how to create a UDF in SQL Server and how to run a query that uses it. Page 552
Chapter 15
Data Manipulation Language (DML)
Replace Replace will replace characters
So if col1 contains '123*45*67*89' and you: Replace (col1,'*',‘-') the result is 123-45-67-89
The Replace function replaces certain characters with others
Page 553
Chapter 15
Data Manipulation Language (DML)
Replace Replace will replace characters or eliminate them
Just give the replace string as a zero length string and it removes the character. So if col1 contains '123*45*67*89*' and you:
Replace (col1,'*','') the result is 123456789
The Replace function replace certain characters with others or eliminates them.
Page 554
Chapter 15
Data Manipulation Language (DML)
User Defined Types (UDTs) UDTs allow for the creation of custom data types that model the structure/ behavior of data in a specific application(s). UDTs offer data types that have capabilities not offered by SQL Server (built-in) data types.
UDTs might be used for : • Date, Time, Currency, or Extended Numeric Types • Geospatial Data • Encrypted Data Possibly : Visual Basic .NET Or Visual C#
To use a Microsoft .NET Framework Common Language Runtime(CLR) UDT: • Code the UDT in a language uspported by using the Microsoft .NET Framework Common Language Runtime(CLR) • Registered the UDT with the database • Create the UDT using a CREATE TYPE statement • Use the UDT in a table, variable, or parameter definition If the current build-in SQL Server data types won't work for a specific column then you might consider creating a UDT. Page 555
Chapter 16
Page 556
Set Operators Functions
Chapter 16
Set Operators Functions
Chapter 16 – Set Operators Functions
"The man who doesn't read good books has no advantage over the man who can't read them." - Mark Twain
Page 557
Chapter 16
Set Operators Functions
Rules of Set Operators 1.
Each query will have at least two SELECT Statements separated by a SET Operator
2.
SET Operators are UNION, UNION ALL, INTERSECT and EXCEPT
3.
Must specify the same number of columns from the same domain (data type/range)
4.
If using Aggregates, both SELECTs much have their own GROUP BY
5.
Both SELECTS must have a FROM Clause
6.
The First SELECT is used for all ALIAS and FORMAT Statements
7.
The Second SELECT will have the ORDER BY statement which must be a number
8.
When multiple operators the order of precedence is INTERSECT, UNION, and EXCEPT
9.
Parentheses can change the order of Precedence
10. Duplicate rows are eliminated in the spool unless the ALLkeyword is used 11. ◦Set operators consider two NULLs as equal for the purpose of comparison.
Page 558
Chapter 16
Set Operators Functions
INTERSECT Explained Logically
Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red INTERSECT SELECT * FROM Table_Blue ; In this example, what numbers in the answer set would come from the query above? Page 559
Chapter 16
Set Operators Functions
INTERSECT Explained Logically Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red INTERSECT SELECT * FROM Table_Blue ;
3 In this example, only the number 3 was in both tables so they INTERSECT. Page 560
Chapter 16
Set Operators Functions
UNION Explained Logically
Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red UNION SELECT * FROM Table_Blue ;
In this example, what numbers in the answer set would come from the query above? Page 561
Chapter 16
Set Operators Functions
UNION Explained Logically Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red UNION SELECT * FROM Table_Blue ;
1 2 3 4 5 Both top and bottom queries run simultaneously, then the two different spools files are merged to eliminate duplicates and place the remaining numbers in the answer set.
Page 562
Chapter 16
Set Operators Functions
UNION ALL Explained Logically
Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red UNION ALL SELECT * FROM Table_Blue ;
In this example, what numbers in the answer set would come from the query above? Page 563
Chapter 16
Set Operators Functions
UNION ALL Explained Logically Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red UNION ALL SELECT * FROM Table_Blue ;
1 2 3 3 4 5 Both top and bottom queries run simultaneously, then the two different spools files are merged together to build the answer set. The ALL prevents eliminating Duplicates.
Page 564
Chapter 16
Set Operators Functions
EXCEPT Explained Logically
Table_Red
Table_Blue
1 2 3
3 4 5
EXCEPT never adds additional rows, but only takes rows away!
SELECT * FROM Table_Red EXCEPT SELECT * FROM Table_Blue ;
In this example, what numbers in the answer set would come from the query above? Page 565
Chapter 16
Set Operators Functions
EXCEPT Explained Logically Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Red EXCEPT SELECT * FROM Table_Blue ;
1 2 The Top query SELECTED 1, 2, 3 from Table_Red. From that point on, only 1, 2, 3 at most could come back. The bottom query is run on Table_Blue, and if there are any matches, they are not ADDED to the 1, 2, 3 but instead take away either the 1, 2, or 3.
Page 566
Chapter 16
Set Operators Functions
Another EXCEPT Example
Table_Red
Table_Blue
1 2 3
3 4 5
EXCEPT never adds additional rows, but only takes rows away!
SELECT * FROM Table_Blue EXCEPT SELECT * FROM Table_Red ;
In this example, what numbers in the answer set would come from the query above?
Page 567
Chapter 16
Set Operators Functions
EXCEPT Explained Logically in Reverse Order Table_Red
Table_Blue
1 2 3
3 4 5
SELECT * FROM Table_Blue EXCEPT SELECT * FROM Table_Red ;
4 5 The Top query SELECTED 3, 4, 5 from Table_Blue. From that point on, only 3, 4, 5 at most could come back. The bottom query is run on Table_Red, and if there are any matches, they are not ADDED to the 3, 4, 5 but instead take away either the 3, 4, or 5. Page 568
Chapter 16
Set Operators Functions
An Equal Amount of Columns in both SELECT List SELECT Dept_No ,Employee_No FROM Employee_Table INTERSECT SELECT Dept_No ,Mgr_No FROM Department_Table;
Both queries have the same number of columns in the SELECT list.
Dept_No _______
Employee_No ____________
400
1256349
Rule 1
You must have an equal amount of columns in both SELECT lists. This is because data is compared from the two spool files, and duplicates are eliminated. So, for comparison purposes, there must be an equal amount of columns in both queries. Page 569
Chapter 16
Set Operators Functions
Columns in the SELECT list should be from the same Domain SELECT First_Name FROM Employee_Table INTERSECT SELECT Department_Name FROM Department_Table;
First_Name __________
You can’t compare First_Name with Department_Name! Different Domains!
Rule 2
Answer set
No rows returned
The above query works without error, but no data is returned. There are no First Names that are the same as Department Names. This is like comparing Apples to Oranges. That means they are NOT in the same Domain.
Page 570
Chapter 16
Set Operators Functions
The Top Query handles all Aliases SELECT Dept_No as Depty ,Employee_No as "The Mgr" FROM Employee_Table INTERSECT Top query is responsible for SELECT Dept_No the column ,Mgr_No ALIAS, Title FROM Department_Table;
and Formatting.
Depty _____
The Mgr ________
400
1256349
The Top Query is responsible for ALIASING.
Page 571
Answer set
Rule 3
Chapter 16
Set Operators Functions
The Bottom Query does the ORDER BY SELECT Dept_No as Depty ,Employee_No as "The Mgr" FROM Employee_Table Bottom INTERSECT query is SELECT Dept_No responsible ,Mgr_No for the Sort FROM Department_Table with an ORDER BY 1 ; ORDER BY SELECT Dept_No as Depty ,Employee_No as "The Mgr" FROM Employee_Table Bottom query INTERSECT is SELECT Dept_No can use the ,Mgr_No number, FROM Department_Table column name ORDER BY Depty ; or alias
Rule 4
Rule 5
The Bottom Query is responsible for sorting. Above, we have both examples referencing the ORDER BY column as either the number 1 (column 1) or Depty. We could have also used Dept_No or even "The Mgr", but the ORDER BY statement must come from referencing column names, aliases or the number representing the column from the top query. Page 572
Chapter 16
Set Operators Functions
Great Trick: Place your Set Operator in a Derived Table SELECT Employee_No AS MANAGER ,RTRIM(Last_Name) + ', ' + First_Name as "Name" FROM Employee_Table INNER JOIN (SELECT Employee_No FROM Employee_Table INTERSECT SELECT Mgr_No FROM Department_Table) AS TeraTom (empno) ON Employee_No = empno ORDER BY "Name" __________ MANAGER 1256349 1333454 1000234 1121334
_______________ Name Harrison, Herbert Smith, John Smythe, Richard Strickling, Cletus
The Derived Table gave us the empno for all managers, and we were able to join it. Page 573
Chapter 16
Set Operators Functions
UNION Vs UNION ALL SELECT Department_Name, Dept_No FROM Department_Table UNION ALL SELECT Department_Name, Dept_No FROM Department_Table ORDER BY 1; UNION ALL Answer Set
UNION Answer Set _____________________ Department_Name ________ Dept_No
_____________________ Department_Name Dept_No ________
Customer Support Human Resources Marketing Research and Development Sales
Customer Support Customer Support Human Resources Human Resources Marketing Marketing Research and Development Research and Development Sales Sales
400 500 100 200 300
400 400 500 500 100 100 200 200 300 300
UNION eliminates duplicates, but UNION ALL does not. If you know that a set operator query does not have any duplicates you are still better to use UNION ALL. This does not check for duplicates so it is faster in performance. Page 574
Chapter 16
Set Operators Functions
Using UNION ALL and Literals SELECT Dept_No AS Dept ,'Employee ' as "Title" ,First_Name + ' ' + Last_Name as "Name" Dept _________ Title FROM Employee_Table ____ UNION ALL ? Employee SELECT Dept_No 10 Employee 100 Department ,'Department' 100 Employee ,Department_Name 200 Department FROM Department_Table 200 Employee ORDER BY 1, 2 ;
Name ______________
Squiggy Jones Richard Smythe Marketing Mandee Chambers Research and Develop Billy Coffing 200 Employee John Smith 300 Department Sales 300 Employee Loraine Larkins 400 Department Customer Support 400 Employee Cletus Strickling 400 Employee Herbert Harrison 400 Employee William Reilly 500 Department Human Resources
Notice the 2nd SELECT column in that it is a literal ‘Employee ‘ (with two spaces) and the other Literal is ‘Department’. These literals match up because now they are both 10 characters long exactly. The UNION ALL brings back all Employees and all Departments and shows the employees in each valid department. Page 575
Chapter 16
Set Operators Functions
A Great Example of how EXCEPT works Employee_Table Employee_No ________ Dept_No ____________ 2000000 ? 1000234 10 1232578 100 1324657 200 1333454 200 2312225 300 1121334 400 2341218 400 1256349 400
Last_Name __________ First_Name _______ Salary __________ 32800.50 Jones Squiggy 64300.00 Smythe Richard 48850.00 Chambers Mandee 41888.88 Coffing Billy 48000.00 Smith John 40200.00 Larkins Loraine 54500.00 Strickling Cletus 36000.00 Reilly William 54500.00 Harrison Herbert
Department_Table Dept_No ________________ Department_Name ________
SELECT Dept_No as Department_Number FROM Department_Table EXCEPT SELECT Dept_No FROM Employee_Table ORDER BY 1 ; _________________ Department_Number 500
This query brought back all Departments without any employees. Page 576
100 200 300 400 500
Marketing Research and Dev Sales Customer Support Human Resources
Chapter 16
Set Operators Functions
USING Multiple SET Operators in a Single Request SELECT Dept_No , Employee_No empno FROM Employee_Table UNION ALL SELECT Dept_No, Employee_No FROM Employee_Table Dept_No ________ INTERSECT ? SELECT Dept_No, Mgr_No 10 FROM Department_Table 100 EXCEPT 200 SELECT Dept_No, Mgr_No 200 FROM Department_Table 300 WHERE Department_Name 400 LIKE '%Sales%' 400 ORDER BY 1, 2; 400
Empno ________ 2000000 1000234 1232578 1324657 1333454 2312225 1121334 1256349 2341218
Above we use multiple SET Operators. They follow the natural Order of Precedence in that UNION is evaluated first, then INTERSECT, and finally EXCEPT. Page 577
Chapter 16
Set Operators Functions
Changing the Order of Precedence with Parentheses SELECT Dept_No , Employee_No empno FROM Employee_Table UNION ALL Dept_No (SELECT Dept_No, Employee_No _______ FROM Employee_Table ? INTERSECT (SELECT Dept_No, 10 Mgr_No 100 FROM Department_Table 200 EXCEPT 200 SELECT Dept_No, Mgr_No 300 FROM Department_Table 400 WHERE Department_Name 400 LIKE '%Sales%')) 400 ORDER BY 1, 2; 400
Empno _______ 2000000 1000234 1232578 1324657 1333454 2312225 1121334 1256349 1256349 2341218
Above we use multiple SET Operators and Parentheses to change the order of precedence. Above the EXCEPT runs first, then the INTERSECT and lastly, the UNION. The natural Order of Precedence without parentheses is UNION, INTERSECT, and finally EXCEPT. Page 578
Chapter 16
Set Operators Functions
Building Grouping Sets Using UNION SELECT NULL AS "Year", Product_ID, SUM(Daily_Sales) TotalSales FROM Sales_Table soh GROUP BY Product_ID UNION SELECT Year(Sale_Date) "Year", NULL Product_ID, SUM(Daily_Sales) TotalSales FROM Sales_Table soh GROUP BY Year(Sale_Date)
Year Product_ID TotalSales ____ _________ _________ ? ? ? 2000
1000 2000 3000 ?
331204.72 306611.81 224587.82 862404.35
The example above shows us that we made $862404.35 in the year 2,000. It also shows us what we made for Product_ID 1000, 2000 and 3000. If you totaled up the TotalSales 1000, 2000 and 3000 it would equal $862404.35.
Page 579
Chapter 16
Set Operators Functions
Three Grouping Sets Using a UNION SELECT NULL AS "Yr_Mnth", Product_ID, SUM(Daily_Sales) TotalSales FROM Sales_Table soh GROUP BY Product_ID UNION SELECT Year(Sale_Date) "Yr_Mnth", NULL Product_ID, SUM(Daily_Sales) TotalSales FROM Sales_Table soh GROUP BY Year(Sale_Date) UNION SELECT Month(Sale_Date) "Yr_Mnth", NULL Product_ID, SUM(Daily_Sales) TotalSales FROM Sales_Table soh GROUP BY Month(Sale_Date)
Yr_Mnth Product_ID TotalSales ________ _________ _________ ? ? ? 9 10 2000
1000 2000 3000 ? ? ?
331204.72 306611.81 224587.82 418769.36 443634.99 862404.35
The example above shows us that we made $862404.35 in the year 2,000. It also shows us what we made for Product_ID 1000, 2000 and 3000. If you totaled up the TotalSales 1000, 2000 and 3000 it would equal $862404.35. It also shows what we did in the months of September and October. If you total those months up it would also equal $862404.35. Page 580
Chapter 17
Page 581
Stored Procedures
Chapter 17
Stored Procedures
Chapter 17 – Stored Procedures
“Freedom from effort in the present merely means that there has been effort stored up in the past.” - Theodore Roosevelt
Page 582
Chapter 17
Stored Procedures
Creating a Stored Procedure Schema DBO "Database Owner"
The name of the Stored Procedure
CREATE PROCEDURE dbo.ListStudents AS SELECT Student_ID ,Last_Name ,First_Name ,Class_Code ,Grade_Pt FROM Student_Table ORDER BY Class_Code ; The CREATE PROCEDURE command will create a stored procedure. The above procedure will return information about students from the Student_Table. The answer set is sorted by freshman, sophomore, junior and then senior. This procedure is created in the dbo schema. The letters dbo stand for “database owner.” The dbo schema is one that is always present in every database, and it is an excellent standard repository for stored procedures. Page 583
Chapter 17
Stored Procedures
Executing a Stored Procedure The name of the Stored Procedure
EXEC dbo.ListStudents ;
Student_ID Last_Name __________ First_Name __________ __________
260000 125634 234121 423400 280023 322133 231222 333450 123250 324652
Johnson Hanson Thomas Larkins McRoberts Bond Wilson Smith Phillips Delaney
Stanley Henry Wendy Michael Richard Jimmy Susie Andy Martin Danny
Class_Code __________ Grade_Pt _________
? FR FR FR JR JR SO SO SR SR
? 2.88 4.00 0.00 1.90 3.95 3.80 2.00 3.00 3.35
You SELECT from a table. You SELECT from a view. You EXECUTE a Stored Procedure with an EXEC statement. This stored procedure queries the contents of a single table, returning a result set. A stored procedure works much like a view, but the query plan will actually be cached once it is executed for the first time. After the first time, the execution time will be consistent in consecutive executions. One reason to use Stored Procedures is consistency in executions. Page 584
Chapter 17
Stored Procedures
There are Three Ways to Execute a Stored Procedure 1
ListStudents ;
2
EXECUTE ListStudents ;
3
EXEC ListStudents ;
Student_ID Last_Name __________ First_Name __________ __________ 260000 125634 234121 423400 280023 322133 231222 333450 123250 324652
Johnson Hanson Thomas Larkins McRoberts Bond Wilson Smith Phillips Delaney
Stanley Henry Wendy Michael Richard Jimmy Susie Andy Martin Danny
Class_Code __________ Grade_Pt _________ ? FR FR FR JR JR SO SO SR SR
? 2.88 4.00 0.00 1.90 3.95 3.80 2.00 3.00 3.35
You SELECT from a table. You SELECT from a view. You EXECUTE a Stored Procedure with an EXEC statement. Above, you can see the three ways to execute a Stored Procedure. The procedure name is ListStudents. You can merely type in ListStudents or EXECUTE ListStudents or EXEC ListStudents. Page 585
Chapter 17
Stored Procedures
Dropping a Stored Procedure DROP PROCEDURE dbo.ListStudents ;
Dropping a stored procedure is easy. Just use the DROP PROCEDURE command. Above, we have qualified the schema and tablename in order to not accidentally drop another procedure named ListStudents in another schema. We could have also just typed, "DROP PROCEDURE ListStudents".
Page 586
Chapter 17
Stored Procedures
Passing an Input Parameter to a Stored Procedure
Passing parameters is an integral part of why stored procedures are important. Using parameters, you can pass information into the body of a procedure in order to control how the procedure operates, and dramatically reduce complexity. The example above has a single input parameter. It is an Employee_Number. When the stored procedure is executed you merely supply the Employee_Number you are trying to find and the result is a single employee. In this case it is Squiggy Jones. Page 587
Chapter 17
Stored Procedures
Executing With Positional Parameters vs. Named Parameters CREATE PROCEDURE dbo.Employee_Find (@Employee_Number INTEGER ) AS SELECT * FROM Employee_Table WHERE Employee_No = @Employee_Number ;
This is how you execute using a positional parameter: EXEC SQL_Class.DBO.Employee_Find 2000000 ;
Employee_Number is equal to 2000000
This is how you execute using a named parameter: EXEC SQL_Class.DBO.Employee_Find @Employee_Number = 2000000 ;
The example above has a single input parameter. It is an Employee_Number. In our examples above we have fully qualified the execute statement with the database.schema.storedprocedurename. In our first example we use a positional parameter, thus the stored procedure assumes that 2000000 is the @Employee_Number value. In the second example we specify this by using the @Employee_Number = 2000000. Page 588
Chapter 17
Stored Procedures
Passing an Output Parameter to a Stored Procedure CREATE PROCEDURE dbo.Student_Count @Class_Code CHAR(2), @Class_Count INT OUTPUT AS SELECT Class_Code FROM Student_Table WHERE Class_Code = @Class_Code; SELECT @Class_Count = @@ROWCOUNT ; DECLARE @Class_Count INT; EXEC dbo.Student_Count 'Fr', @Class_Count OUTPUT; PRINT @Class_Count ;
Class_Code __________ FR FR FR
The stored procedure above begins with a defined output parameter called @Class_Count. Notice it has a data type of INTEGER and also specifies the keyword OUTPUT. When executed the query stores the row count in the parameter. The code that invokes the stored procedure creates the variable to pass as the output parameter (DECLARE @Class_Count INT). The EXEC statement will also specify that the parameter is OUTPUT. Page 589
Chapter 17
Stored Procedures
Changing a Stored Procedure with an ALTER The name of the Stored Procedure
ALTER PROCEDURE dbo.ListStudents AS SELECT Student_ID ,Last_Name ,First_Name ,Class_Code ,Grade_Pt FROM Student_Table ORDER BY CASE Class_Code WHEN 'Fr' THEN 1 WHEN 'So' THEN 2 WHEN 'Jr' THEN 3 WHEN 'Sr' THEN 4 ELSE 5 END, Grade_Pt DESC ;
The ALTER PROCEDURE command can be used to change a stored procedure. Now the above procedure will return information about students from the Student_Table. The answer set is sorted by freshman, sophomore, junior and then senior. Then, the minor sort is Grade_Pt DESC. This procedure is created in the dbo schema. The letters dbo stand for “database owner.” The dbo schema is one that is always present in every database, and it is an excellent standard repository for stored procedures. Page 590
Chapter 17
Stored Procedures
Answer Set for the Altered Stored Procedure The name of the Stored Procedure
EXEC dbo.ListStudents
Student_ID Last_Name __________ First_Name __________ __________
234121 125634 423400 231222 333450 322133 280023 324652 123250 260000
Thomas Hanson Larkins Wilson Smith Bond McRoberts Delaney Phillips Johnson
Wendy Henry Michael Susie Andy Jimmy Richard Danny Martin Stanley
Class_Code __________ Grade_Pt _________
FR FR FR SO SO JR JR SR SR ?
4.00 2.88 0.00 3.80 2.00 3.95 1.90 3.35 3.00 ?
We have altered the stored procedure to sort by Fr, So, Jr, Sr and then the null value. The minor sort is Grade_Pt DESC. It is very easy to create, execute and alter a stored procedure. Page 591
Chapter 17
Stored Procedures
Displaying a Stored Procedure’s Definition
The name of the Stored Procedure
Above we have just displayed the definition for the stored procedure named Employee_Find. This is a great way to alter a stored procedure. First, you get the definition. Then, you copy the definition. And finally, you then alter the definition.
Page 592
Chapter 17
Stored Procedures
Encrypting a Stored Procedure’s Definition
ENCRYPTION
The EXEC SP_Helptext command no longer Displays the Stored Procedure
If you encrypt a stored procedure then others cannot see the definition.
Page 593
Chapter 17
Stored Procedures
Using a Stored Procedure to Delete a Row SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust _________ 11111111 31313131 31323134 57896883 87323456
Name ________________ Billy's Best Choice Acme Products ACE Consulting XYZ Plumbing Databases N-U
Phone _________ 555-1234 555-1111 555-1212 347-8954 322-1012
CREATE PROCEDURE Del_Cust AS DECLARE @Cust_No INT; SET @Cust_No = 31313131; DELETE FROM SQL_Class.dbo.Customer_Table WHERE Customer_Number = @Cust_No ; Exec Del_Cust ; SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust _________ 11111111 31323134 57896883 87323456
Name ________________ Billy's Best Choice ACE Consulting XYZ Plumbing Databases N-U
Phone _________ 555-1234 555-1212 347-8954 322-1012
The example above demonstrates how to delete a row from a table using a stored procedure. Page 594
Chapter 17
Stored Procedures
A Different Method to Delete a Row SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust _________ 11111111 31323134 57896883 87323456
Name ________________ Billy's Best Choice ACE Consulting XYZ Plumbing Databases N-U
Phone _________ 555-1234 555-1212 347-8954 322-1012
CREATE PROCEDURE Del_Cust2 AS DECLARE @Cust_No INT = 87323456; DELETE FROM SQL_Class.dbo.Customer_Table WHERE Customer_Number = @Cust_No;
Exec Del_Cust2 ; SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust _________ 11111111 31323134 57896883
Name ________________ Billy's Best Choice ACE Consulting XYZ Plumbing
Phone _________ 555-1234 555-1212 347-8954
The example above demonstrates another way to declare a variable and to delete a row from a table using a stored procedure. Page 595
Chapter 17
Stored Procedures
Deleting a Row Using an Input Parameter SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust _________ 11111111 31323134 57896883
Name ________________ Billy's Best Choice ACE Consulting XYZ Plumbing
Phone _________ 555-1234 555-1212 347-8954
CREATE PROCEDURE Del_Cust_Parm @Cust_No INT AS DELETE FROM SQL_Class.dbo.Customer_Table WHERE Customer_Number = @Cust_No;
Del_Cust_Parm 31323134;
SELECT Customer_Number as Cust ,Customer_Name as Name ,Phone_Number as Phone FROM Customer_Table;
Cust Name Phone _________ ________________ _________ 11111111 Billy's Best Choice 555-1234 57896883 XYZ Plumbing 347-8954
The example above demonstrates how to delete a row from a table using a stored procedure via an input parameter. This is the preferred method because you can use this stored procedure over and over again. You just supply a different customer_number each time you execute it. Page 596
Chapter 17
Stored Procedures
Using Loops in Stored Procedures
1
2
CREATE Table My_Tbl_XYZ ( Cntr Integer ,TheTime Datetime );
Use your initials for the XYZ piece of the table
CREATE PROCEDURE Inserter_Five AS DECLARE @Cntr INTEGER = 0; WHILE @Cntr < 5 BEGIN; SET @Cntr = @Cntr + 1; INSERT INTO My_Tbl_XYZ VALUES (@Cntr, Current_Timestamp) ; END;
3
Inserter_Five ;
There are now Five rows the table
We created a table called My_Tbl_XYZ. Then, we inserted five rows inside the table using a WHILE loop. The WHILE loop did a loop five times. Page 597
Chapter 17
Stored Procedures
Stored Procedure Workshop Create the table below but substitute the XYZ with you initials
CREATE Table Table_XYZ ( Col1 INTEGER ,Col2 INTEGER) ;
Create the procedure but substitute the XYZ with you initials
Now, create a stored procedure called Final_XYZ that places 1,000 rows inside the table:
Col1 should have 1000 unique values Col2 should have 250 different values Your mission is to create the table above and then create a stored procedure that will insert 1,000 rows. The tricky part is that col1 should have 1,000 unique values, but col2 should have only 250 different values. Page 598
Chapter 17
Stored Procedures
Looping with a WHILE Statement CREATE Table SQL_Class.dbo.Table_XYZ ( Col1 INTEGER ,Col2 INTEGER) ; CREATE Procedure Final_XYZ AS DECLARE @Cntr INT = 0; DECLARE @Cntr2 INT = 0; WHILE @Cntr < 1000 BEGIN; SET @Cntr = @Cntr + 1; SET @Cntr2 = @Cntr2 + 1; If @Cntr2 = 251 BEGIN; SET @Cntr2 = 1; END; INSERT INTO SQL_Class.dbo.Table_XYZ Values (@Cntr, @Cntr2); END; Exec Final_XYZ;
The assignment is to create a table called Table_XYZ. It has two columns (Col1 and Col2). Their data types are integer. The next part of the assignment is to insert 1,000 rows inside the table. The first column (Col1) will have 1,000 unique values. The second column (Col2) will have only 250 different values. Above is how it is done. Page 599
Chapter 18
Page 600
Statistical Aggregate Functions
Chapter 18
Statistical Aggregate Functions
Chapter 18 – Statistical Aggregate Functions
"You can make more friends in two months by becoming interested in other people than you will in two years by trying to get other people interested in you." - Dale Carnegie
Page 601
Chapter 18
Statistical Aggregate Functions
The Stats Table Col1 Col3 ____ Col4 _____ Col5 _____ Col6 ____ Col2 ____ ____ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 1 3 3 3 4 5 5 5 5 7 7 9 9 9 9 10 10 10 10 10 10 13 13 13 14 15 15 16 16
1 1 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 40 40 50 50 60
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 22 12 13 14 15 14 13 12 11 9 8 7 6 5 4 3 2 1 1 1
0 5 10 15 20 30 30 30 35 35 40 40 45 45 50 55 55 60 60 65 65 65 70 70 80 85 90 90 95 100
Above is the Stats_Table data in which we will use in our statistical examples.
Page 602
Chapter 18
Statistical Aggregate Functions
The VAR and VARP Functions SELECT VAR(col1) AS Variance_Example, VARP(col1) AS Var_EntirePopulation FROM Stats_Table ;
Variance_Example _______________ Var_EntirePopulation _________________ 77.5
74.92
The VAR and VARP functions return the statistical variance of all the values in the specified expression. The VAR uses a sample of the data population to return a value. The VARP returns the value based upon the entire data population. The expression parameter must be one of the exact or approximate numeric data types, except for the bit data type.
Page 603
Chapter 18
Statistical Aggregate Functions
A VAR Example Col1 Col3 ____ Col4 _____ Col5 _____ Col6 ____ Col2 ____ ____ 1 1 1 30 1 0 2 1 1 29 2 5 3 3 10 28 3 10 4 3 10 27 4 15 5 3 10 26 5 20 6 4 10 25 6 30 7 5 10 24 7 30 8 5 10 23 8 30 9 5 10 22 9 35 10 5 20 21 10 35 11 7 20 20 22 40 12 7 20 19 12 40 13 9 20 18 13 45 14 9 20 17 14 45 15 9 20 16 15 50 16 9 20 15 14 55 17 10 20 14 13 55 18 10 20 13 12 60 19 10 20 12 11 60 20 10 20 11 9 65 21 10 20 10 8 65 22 10 20 9 7 65 23 13 20 8 6 70 24 13 30 7 5 70 25 13 30 6 4 80 26 14 40 5 3 85 27 15 40 4 2 90 28 15 50 3 1 90 29 16 50 2 1 95 30 16 60 1 1 100
Page 604
SELECT VAR(col1) AS Col1 ,VAR(col2) AS Col2 ,VAR(col3) AS Col3 ,VAR(col4) AS Col4 ,VAR(col5) AS Col5 ,VAR(col6) AS Col6 FROM Stats_Table;
____ _____ Col1 Col2 ____ Col3 ____ Col4 _____ Col5 Col6 _____ 77.5 19.95 197.65 77.5 20.25 747.73
The VAR function returns the statistical variance of all the values in the specified expression. The VAR uses a sample of the data population to return a value.
Chapter 18
Statistical Aggregate Functions
A VARP Example Col1 Col3 ____ Col4 _____ Col5 _____ Col6 ____ Col2 ____ ____ 1 1 1 30 1 0 2 1 1 29 2 5 3 3 10 28 3 10 4 3 10 27 4 15 5 3 10 26 5 20 6 4 10 25 6 30 7 5 10 24 7 30 8 5 10 23 8 30 9 5 10 22 9 35 10 5 20 21 10 35 11 7 20 20 22 40 12 7 20 19 12 40 13 9 20 18 13 45 14 9 20 17 14 45 15 9 20 16 15 50 16 9 20 15 14 55 17 10 20 14 13 55 18 10 20 13 12 60 19 10 20 12 11 60 20 10 20 11 9 65 21 10 20 10 8 65 22 10 20 9 7 65 23 13 20 8 6 70 24 13 30 7 5 70 25 13 30 6 4 80 26 14 40 5 3 85 27 15 40 4 2 90 28 15 50 3 1 90 29 16 50 2 1 95 30 16 60 1 1 100
Page 605
SELECT VARP(col1) ,VARP(col2) ,VARP(col3) ,VARP(col4) ,VARP(col5) ,VARP(col6) FROM Stats_Table;
AS Col1 AS Col2 AS Col3 AS Col4 AS Col5 AS Col6
____ _____ Col1 Col2 ____ Col3 ____ Col4 _____ Col5 Col6 _____ 74.92 19.29 191.06 74.92 19.58 722.81
The VARP function returns the statistical variance of all the values in the specified expression. The VARP returns the value based upon the entire data population.
Chapter 18
Statistical Aggregate Functions
The STDEV and STDEVP Functions SELECT STDEV(Col1) AS StandDev ,STDEVP(Col1) AS StandDevPop FROM Stats_Table
StandDev ________ 8.8 The STDEV function returns the standard deviation, but only uses a sample of the data population
StandDevPop ___________ 8.66 The STDEVPOP function returns the standard deviation based on the entire population
The STDEV function returns the standard deviation of all the values in the specified expression, but only uses a sample of the data population to return a value. The STDEVP returns the standard deviation value based upon the entire data population. The expression parameter must be one of the exact or approximate numeric data types, except for the bit data type. Page 606
Chapter 18
Statistical Aggregate Functions
A STDEV Example Col1 Col3 ____ Col4 _____ Col5 _____ Col6 ____ Col2 ____ ____ 1 1 1 30 1 0 2 1 1 29 2 5 3 3 10 28 3 10 4 3 10 27 4 15 5 3 10 26 5 20 6 4 10 25 6 30 7 5 10 24 7 30 8 5 10 23 8 30 9 5 10 22 9 35 10 5 20 21 10 35 11 7 20 20 22 40 12 7 20 19 12 40 13 9 20 18 13 45 14 9 20 17 14 45 15 9 20 16 15 50 16 9 20 15 14 55 17 10 20 14 13 55 18 10 20 13 12 60 19 10 20 12 11 60 20 10 20 11 9 65 21 10 20 10 8 65 22 10 20 9 7 65 23 13 20 8 6 70 24 13 30 7 5 70 25 13 30 6 4 80 26 14 40 5 3 85 27 15 40 4 2 90 28 15 50 3 1 90 29 16 50 2 1 95 30 16 60 1 1 100
Page 607
SELECT STDEV(col1) ,STDEV(col2) ,STDEV(col3) ,STDEV(col4) ,STDEV(col5) ,STDEV(col6) FROM Stats_Table;
AS Col1 AS Col2 AS Col3 AS Col4 AS Col5 AS Col6
____ _____ Col1 Col2 ____ Col3 ____ Col4 _____ Col5 Col6 _____ 8.8 4.47 14.06 8.8 4.5 27.34
The STDEV function returns the standard deviation, but only uses a sample of the data population
Chapter 18
Statistical Aggregate Functions
A STDEVP Example Col1 Col3 ____ Col4 _____ Col5 _____ Col6 ____ Col2 ____ ____ 1 1 1 30 1 0 2 1 1 29 2 5 3 3 10 28 3 10 4 3 10 27 4 15 5 3 10 26 5 20 6 4 10 25 6 30 7 5 10 24 7 30 8 5 10 23 8 30 9 5 10 22 9 35 10 5 20 21 10 35 11 7 20 20 22 40 12 7 20 19 12 40 13 9 20 18 13 45 14 9 20 17 14 45 15 9 20 16 15 50 16 9 20 15 14 55 17 10 20 14 13 55 18 10 20 13 12 60 19 10 20 12 11 60 20 10 20 11 9 65 21 10 20 10 8 65 22 10 20 9 7 65 23 13 20 8 6 70 24 13 30 7 5 70 25 13 30 6 4 80 26 14 40 5 3 85 27 15 40 4 2 90 28 15 50 3 1 90 29 16 50 2 1 95 30 16 60 1 1 100
Page 608
SELECT STDEVP(col1) , STDEVP(col2) , STDEVP(col3) , STDEVP(col4) , STDEVP(col5) , STDEVP(col6) FROM Stats_Table;
____ _____ Col1 Col2 ____ Col3 8.66 4.39 13.82
AS Col1 AS Col2 AS Col3 AS Col4 AS Col5 AS Col6
____ Col4 _____ Col5 Col6 _____ 8.66 4.42 26.89
The STDEVPOP function returns the standard deviation based on the entire population
Chapter 19
Page 609
Nexus
Chapter 19
Nexus
Chapter 19 – Nexus
“I might be just a little bit biased, but because of our long term vision and incredible determination, the Nexus has become what some consider the greatest BI tool on planet Earth!” - Tera-Tom Coffing
Page 610
Chapter 19
Nexus
Nexus is Now Available on the Microsoft Azure Cloud
Why the Nexus Chameleon should be your query tool of choice: 1) Queries every major system 2) Provides visualization and automatically writes the SQL 3) Can perform cross-system joins with a few clicks of the mouse 4) Converts table structures and moves the table and data between systems 5) Compares and synchronizes databases 6) Can move an entire database of tables or views between systems 7) Has the "Garden of Analysis" to re-query answer sets inside your PC 8) Provides a dashboard of graphs and charts for answer sets Download the Nexus for a free trial at www.CoffingDW.com and use Nexus in-house or on the Microsoft Azure cloud. Page 611
Chapter 19
Nexus
Nexus Queries Every Major System
Nexus is connected to each of the systems above
Priority number one for us was to build the best BI tool and then get it working on every major platform.
Page 612
Chapter 19
Nexus
Setup of Nexus is as easy as pie
To add a system just right click on the Systems Tree and choose Add data source connection
Some of the reasons Nexus is so popular on cloud platforms are because Nexus queries every major platform and it is so easy to setup. Just right click on the systems tree and choose "Add data source connection". You can then add all of your systems one by one and before you know it you are ready to query them all.
Page 613
Chapter 19
Nexus
Setup of Nexus is a Easy as 1, 2, 3
1 Choose your system type from the drop down menu
2 Hit the Add New Button
3 Pick your driver. The Nexus Chameleon drivers are already installed for you.
Once you have right clicked on the Systems Tree and selected "Add new data source", you will come to the Data Source Connection page (see above). First, choose your Source Type from the drop down menu. Hit the Add New button and choose your driver from the System DSN tab (The Nexus Chameleon drivers are outstanding). Then, hit the CONFIGURE button and put in your IP address, login and password. You are ready to begin querying.
Page 614
Chapter 19
Nexus
Nexus Data Visualization
“It never made sense to me that the data scientist and the business user couldn't work together on the same playing field. We developed a way for them to work together, by building the Super Join Builder.” - Tera-Tom Coffing
Page 615
Chapter 19
Nexus
Nexus Data Visualization
1
Right Click on any table and choose Super Join Builder
You can write the SQL yourself and Nexus will bring back an answer set, but why not let Nexus write the SQL for you? The Nexus has the best data visualization and it took years of work and millions of lines of code. Just right click on a table in any of your systems trees (above we chose the Addresses table) and then choose SUPER JOIN BUILDER. The table will appear visually and in color. It will show the table name, the columns and their data types. Just check the columns you want on your report and Nexus will build the SQL for you automatically!
Page 616
Chapter 19
Nexus
Nexus Data Visualization Shows What Tables Can Be Joined
Left Click on the Add Join drop down
The menu shows what tables can be joined together.
1
2
Once you see your first table in the Super Join Builder the fun is just beginning. Left Click on the top right of the visible table and select the drop down menu where it says "Add Join". Nexus will show you what tables can be joined. The Addresses table above can be joined to the Subscribers table. The Subscribers table can be joined to the Claims table. The Claims table can be joined to the Providers and Services tables. Be prepared to be amazed at the next page!
Page 617
Chapter 19
Nexus
Nexus is doing a Five-Table Join
The "Add Join" button showed us the tables that could be joined and we chose them all. Notice that we can now see each table visually (and in color) and their respective columns and data types. Also, notice that we have checked the columns we want on our report. The Nexus has already built the SQL instantly and automatically for you and it does so perfectly. This technology puts the business user on the same level with the data scientist. The next page will show the SQL generated!
Page 618
Chapter 19
Nexus
Nexus Generates the SQL Automatically
1 By clicking on the SQL tab you can see the SQL that Nexus has generated automatically
This is the SQL that was built automatically from the previous page. Since we are querying an Azure SQL Data Warehouse system, the Nexus built T-SQL to satisfy the query. It does not matter whether the system you are querying is Hadoop, Oracle, SQL Server, Teradata or any other system, the Nexus will build the SQL perfectly for that system. All you have to do now is hit the EXECUTE button and you will receive your answer set.
Page 619
Chapter 19
Nexus
Nexus Delivers the Report 1
By hitting the EXECUTE button the report was delivered in the Answer Set tab
When you hit the EXECUTE button Nexus executes the query and delivers the report.
Page 620
Chapter 19
Nexus
Cross-System Joins from Teradata, Oracle and SQL Server
The three top tables are from Teradata and the bottom tables are from Oracle and SQL Server.
The three tables at the top are from Teradata, but the tables at the bottom are from Oracle and SQL Server. When you hit EXECUTE, the Nexus will deliver the report. Nexus not only builds the SQL needed, but the table conversions and data movement to make it happen. Nexus does all of the difficult things for you. You just point and click on the columns you want from the tables and Nexus does the rest. Is that amazing or what?
Page 621
Chapter 19
Nexus
The Tab of the Super Join Builder
“The 9 tabs of the Super Join Builder are each dedicated to a single query, but each provides a different function. This makes the automatic writing of the SQL so easy, intuitive, quick and yet powerful.” - Tera-Tom Coffing .
Page 622
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Objects Tab 1
The Objects tab is the first screen you see whenever you right click on any table and choose Super Join Builder. The Objects tab (in red) shows the table, columns and their data types. The Objects tab also allows you to left click on the right corner of any table on the ADD JOIN dropdown to see what other tables are joinable. If you click on a joinable table in the ADD JOIN menu then that table will appear in the Objects tab as well. If you check mark any of the columns from any tables in the Objects tab the SQL will be built and include those selected columns in the report. Since we have not selected any columns yet the SQL has not been built. Once we begin to checkmark columns the SQL will be built. Above, we first entered the Super Join Builder by right clicking on the Addresses table in our systems tree and we chose Super Join Builder. We then left clicked on the right corner of the Addresses table on the ADD JOIN drop down and we selected Subscribers. Both tables then appeared in the Object tab. We then left clicked on the Subscribers table on the ADD JOIN drop down and we can see that Claims joins to subscribers. Page 623
Chapter 19
Nexus
Selecting Columns in the Objects Tab
The Objects tab is the first screen you see whenever you right click on any table and choose Super Join Builder. The Objects tab (in red) shows the table, columns and their data types, and allows you to left click on the right corner of any table on the ADD JOIN dropdown to see what other tables are joinable. We have chosen a two table join between the Addresses table and the Subscribers table. Notice that we clicked on the checkbox on the columns Street, City and State of the Addresses table, and also notice that we clicked on the SELECT * of the Subscribers table which auto-clicked all columns. Our answer set (as of now) will come back with 9 columns (Street, City, State, Last_Name, First_Name, Gender, SSN, Member_No and Subscriber_No). The SQL has automatically been generated with each check of a column. Page 624
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Columns Tab 2
These are the columns that will be on the report. This is because you checked them in the Objects Tab. You can rearrange these columns like a Rubik's cube (click and drag) and the SQL will reflect any and all changes.
These are the columns that you did not check in the Object Tab. You can drag them up if you decide you want any of them on the report.
The columns tab displays the columns that you selected in the Object tab that will be on the report (at the top). Notice the colors correspond to their respective tables. It is here that you can change the order of the columns by dragging them to the order that you prefer. Notice at the bottom are the columns that you did not select in the Objects tab. These will not be on the report. You can however drag these up to the top and then they will be on the report. The columns at the top are on the report and the columns at the bottom are not, but you can rearrange these columns until the report is exactly what you want.
Page 625
Chapter 19
Nexus
Removing Columns from the Report in the Columns Tab
Drag any column to the trashcan (blue arrow) and it is no longer on the report.
You can remove a series of columns also. Above, we did a CTRL click on Gender and a SHIFT Click on Member_No and all columns between highlight. Keep the SHIFT key down and move them all to the trashcan.
If you want to delete a column from the report, just drag it to the trashcan and it will appear at the bottom in the list in the area of non-selected columns. You can also remove a series of columns. Above, we did a CTRL click on the Gender column and then did a SHIFT click on the Member_No column. If we keep the SHIFT key down, and drag them all together to the trash can, then all three of these columns are removed from the report.
Page 626
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Sorting Tab 3
The Sorting tab allows you to sort the answer set by simply double clicking on a column or by dragging it up. The columns are listed near the bottom (in color) and the columns at the very bottom were not selected to be on the report, but you can still sort by them. In our example above, we chose the Column State to be the major sort key. The Column State was selected previously to be part of the report. The column Zip is the minor sort key, but as you can see it was not previously selected to be on the report. Now, the Nexus will automatically place an ORDER BY statement at the end of the query. That ORDER BY statement will be ORDER BY State, Zip DESC.
Page 627
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Joins Tab 4
The drop down box allows you to change the join from an INNER JOIN to a:
LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
The Nexus defaults all joins to INNER JOIN, but the Joins tab will allow you to change any of the joins from an INNER JOIN to any OUTER JOIN. Just hit the drop down box (red arrow) and your outer join options await you.
Page 628
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Where Tab 5
Columns on the report Indexed columns Columns not on the report
The WHERE tab is designed to do two things. First, it shows you the indexes of the tables even if you are using views. This allows users to click on indexed columns and utilize an additional WHERE clause. Secondly, it shows all of the columns already on the report and those that are not on the report. Either way, you can double click on any column and write the WHERE or AND clause. I will demonstrate that on the next page.
Page 629
Chapter 19
Nexus
Using the WHERE Tab For Additional WHERE or AND
By double clicking on the Subscriber_No, that column is placed down below. I entered the Subscriber_No = 123456778. SELECT "Add".Street, "Add".City, "Add"."State", SUB.Last_Name, SUB.First_Name, SUB.Subscriber_No FROM SQL_CLASS.ADDRESSES "Add" INNER JOIN SQL_CLASS.SUBSCRIBERS SUB ON "Add".Subscriber_No = SUB.Subscriber_No WHERE "Add".Subscriber_No = 123456778 ORDER BY "Add"."State" ASC, "Add".Zip DESC;
That will automatically be placed in the SQL. Since that column is an index the system will retrieve the answer set faster.
If you went to the SQL tab you would see this SQL. Notice it reflects everything we have done over the past several examples.
The example above shows us double clicking on the Subscriber_No column. Notice (follow the arrow) that an additional WHERE clause was added. The Subscriber_No = awaited us to place a Subscriber_No in and we typed 123456778.
Page 630
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – SQL Tab 6
The SQL tab shows the SQL that Nexus has automatically generated. Every click from every tab can cause a change to the SQL. We first went to the Object Tab where we chose the Addresses table and the Subscribers table. We chose our columns in the Objects tab, but we then went to the Columns tab and deleted some of the columns. We then went to the Sorting tab and chose our ORDER BY keys of State and Zip DESC. We then went to the WHERE tab and added an additional WHERE clause choosing the column Subscriber_No and then we typed in = 123456778. The SQL reflects everything we did. Page 631
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Answer Set Tab 7 1
By hitting the EXECUTE button the report was delivered in the Answer Set tab
When you hit EXECUTE, the SQL generated is run on your system and you receive an answer set. The above example is a different example than our previous examples. This reflects just a two-table join.
Page 632
Chapter 19
Nexus
The 9 Tabs of the Super Join Builder – Analytics Tab 9
Select all of the columns and then click on the Analytics tab
The Analytics tab is used for Rank, OLAP, and for Group by Grouping Sets, Group by Rollup and Group by Cube queries. It is usually used with a single table. Above, we have right clicked on the Sales_Table and chosen Super Join Builder. We will next click on the Analytics tab to show you how to generate analytics quickly. Turn the page and let's get started. Page 633
Chapter 19
Nexus
Analytics Tab Your three Analytics options are OLAP, Rank, Grouping Sets
We are in the OLAP tab.
The Analytics tab is used for Rank, OLAP, and for Group by Grouping Sets, Group by Rollup and Group by Cube queries. It is usually used with a single table. Above, we have right clicked on the Sales_Table and chosen Super Join Builder. We will next click on the Analytics tab to show you how to generate analytics quickly. Turn the page and let's get started.
Page 634
Chapter 19
Nexus
Analytics Tab – OLAP Example
This report will generate an OLAP report (Online Analytic Processing) such as a Cumulative Sum, Moving Sum, etc. We dragged the Daily_Sales column from the bottom to the OLAP column (top left). We dragged the Product_ID and Sale_Date columns to the sorting area. We dragged the Product_ID column to the Partitioning area and we changed our moving window to a 3. We then checked all of the OLAP functions on the top right, including the OLAP with Partitioning. The next slide will show the SQL automatically generated in the SQL tab.
Page 635
Chapter 19
Nexus
Analytics Tab – OLAP Example of SQL Generated
SELECT Sal.Product_ID, Sal.Sale_Date, Sal.Daily_Sales, SUM(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS UNBOUNDED PRECEDING) , SUM(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS 2 PRECEDING) , AVG(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS 2 PRECEDING) , Sal.Daily_Sales - SUM(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING) , COUNT(*) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS UNBOUNDED PRECEDING) , MIN(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ) , MAX(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ) , SUM(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS UNBOUNDED PRECEDING) , SUM(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS 2 PRECEDING) , AVG(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS 2 PRECEDING) , AVG(Sal.Daily_Sales) OVER ( ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS UNBOUNDED PRECEDING) , Sal.Daily_Sales - SUM(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING) , COUNT(*) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ROWS UNBOUNDED PRECEDING) , MIN(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ) , MAX(Sal.Daily_Sales) OVER (PARTITION BY Sal.Product_ID ORDER BY Sal.Product_ID ASC, Sal.Sale_Date ASC ) FROM SQL_CLASS.Sales_table Sal;
The SQL above might take hours to write, but with the Nexus it can be generated in 30 seconds.
Page 636
Chapter 19
Nexus
Analytics Tab – Grouping Sets Example
This report will generate Grouping Sets that also include Rollup and Cube. Notice that we dragged the Product_ID to the Product. We dragged the Sale_Date column to the Date Column and we dragged the Daily_Sales column to the Sum. We then checked the Grouping Sets, Rollup and Cube on the top right. The report is now ready to be executed. Page 637
Chapter 19
Nexus
Analytics Tab – Grouping Sets Answer Set
Notice now that there are three Result Sets. The picture above shows Result Set 3 which is the Group by Grouping Sets. The Result 1 tab will show the Group by Rollup and the Result 2 tab will show the Group by Cube.
Page 638
Chapter 19
Nexus
Nexus Data Movement
“If you have ever had to build a load script or convert table structures between different systems, you have experienced the impossible. We spent 7 years to make sure our users could do it with a single click of a button.” - Tera-Tom Coffing
Page 639
Chapter 19
Nexus
Moving a Single Table To a Different System
Just Right Click on a table and choose "Move Data".
Just right click on any single table and select Move Data. The data movement screen will appear. Check out the next slide.
Page 640
Chapter 19
Nexus
The Single Table Data Movement Screen Lite Speed is for smaller tables, but Warp Speed is for large table.
This button will show the size and number of rows of the source table
Choose your Target system and put in your login information once and Nexus will remember the next time. Above, we are moving the Addresses table from SQL Server to Teradata. When the EXECUTE button is hit, the table is converted automatically and moved. Simple and easy! Wait until you see the Database Mover! It is next.
Page 641
Chapter 19
Nexus
Moving an Entire Database To a Different System
Just Right Click on a Database and choose "Move Data".
Just right click on any database and select Move Data. The database mover screen will appear. Check out the next slide.
Page 642
Chapter 19
Nexus
The Database Mover Screen Lite Speed is for small tables. Warp Speed is for large table. Auto chooses Lite or Warp based on size parameters in the Options tab
Check the tables or move through the Views and then press the blue button.
Select all the tables, a single table, some of the tables or choose to move through the views (bottom left),and then press the blue arrow button. We are moving 19 tables from SQL Server to Teradata. Hit EXECUTE and all of the tables move. Don't forget though to check out the Options tab. You can set your parameters there. The next slide will show the Options Tab. Page 643
Chapter 19
Nexus
The Database Mover Options Tab
The Row Count and Table size parameters take effect for Lite or Warp Speed if you select AUTO on the previous screen.
The Options tab allows you to set more detailed parameters. Once you set them the first time, the Nexus Chameleon will remember them the next time (as defaults). You can change them as you see fit.
Page 644
Chapter 19
Nexus
Converting DDL Table Structures
1. Right Click on a Database and a menu appears. 2. Choose CONVERT TABLE STRUCTURES. 3. Pick the Database you want to convert to.
Right Click on a Database
We right clicked on a Teradata database and chose Convert Table Structures and then chose to convert the Teradata tables to Hadoop. Check out the next couple of screens on the following pages.
Page 645
Chapter 19
Nexus
Converting DDL Table Structures
Check the tables you want converted and press the big blue button.
You can click on the table’s box (red arrow) and it checks all the tables. You can uncheck any table you don't want, but once you have the tables you want converted checked, then you just press the big blue arrow. The tables will move over to the right in the To Be Converted area. Just hit Execute (at the top) and the table structures (DDL) will be converted. This example has converted 19 Teradata tables to Hadoop table structures. Check out the DDL Nexus creates on the next page.
Page 646
Chapter 19
Nexus
Converting DDL Table Structures
Nexus converts and creates the new DDL. You can logion to the system and paste in the table structures. This complex and difficult project sometimes takes a month, but with the Nexus it takes a minute.
Page 647
Chapter 19
Nexus
Hound Dog Compression
“Using Multi-Value compression on a Teradata table is a win-win. Large tables are about 35% smaller, 35% faster and take up about 35% less network traffic. The only negative is you have to figure out the correct algorithm and write the DDL. We spent a long time making this all happen automatically.” - Tera-Tom Coffing
Page 648
Chapter 19
Nexus
Hound Dog Compression on Teradata
Right Click on a Teradata Database
Save yourself an enormous amount of money by using the Hound Dog Compression tool. It is as easy as a right click on a Teradata database and then choosing Hound Dog Compress Database. Check out the next screen to see how easily it is done.
Page 649
Chapter 19
Nexus
Hound Dog Compression on Teradata
Check the tables and press the blue button.
You can click on the table’s box (red arrow) and it checks all the tables. You can uncheck any table you don't want, but once you have the tables you want to be compressed just press the big blue arrow. The tables will move over to the right in the Table to Compress area. Just hit Execute (at the top) and the tables will be compressed. You can then look at the dashboard tab (top right) and see the compression savings for every table.
Page 650