Ashley Ohmann June 20, 2013. * What is Custom SQL? * What can I do with it? * Join conditions * Unions and Self Joins * Ranks * Derived Tables.

Slides:



Advertisements
Similar presentations
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Advertisements

Name: Tatiana “Tania” Harrison
Jeopardy Objects Navigation Buttons True/False Parts of a Report Vocabulary Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final.
Chapter 5 Creating, Sorting, and Querying a Table
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Chapter 2 Querying a Database
Concepts of Database Management Sixth Edition
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Microsoft Access 2010 Chapter 7 Using SQL.
Access Tutorial 3 Maintaining and Querying a Database
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
Managing Business Data Lecture 8. Summary of Previous Lecture File Systems  Purpose and Limitations Database systems  Definition, advantages over file.
ODBC : What is it and how does it work with MDS ?.
Miscellaneous Excel Combining Excel and Access. – Importing, exporting and linking Parsing and manipulating data. 1.
Microsoft Access 2010 Building and Using Queries.
Analyzing Data For Effective Decision Making Chapter 3.
Inner Join vs. Outer Join
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
Microsoft Access 2007 Tutorial (Part II) CIS*1000*DE.
1 What to do before class starts??? Download the sample database from the k: drive to the u: drive or to your flash drive. The database is named “FormBelmont.accdb”
Views Lesson 7.
Tutorial 9 Using Action Queries and Advanced Table Relationships.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
® Microsoft Access 2010 Tutorial 3 Maintaining and Querying a Database.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 3 Analyzing Tables and Creating.
Chapter 6 Creating, Sorting, and Querying a Table
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
Normalizing Database Files Professor Ralph Westfall May, 2011.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Chapter 10: Working with Large Data Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Thinking in Sets and SQL Query Logical Processing.
There’s a particular style to it… Rob Hatton
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Database (Microsoft Access). Database A database is an organized collection of related data about a specific topic or purpose. Examples of databases include:
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
TDA Direct Certification
Objectives Create an action query to create a table
Dynamic Input with SQL Queries
Building and Using Queries
MODULE 7 Microsoft Access 2010
JOINS (Joinining multiple tables)
SQL Fundamentals in Three Hours
Access: Access Basics Participation Project
Navya Thum January 30, 2013 Day 5: MICROSOFT EXCEL Navya Thum January 30, 2013.
Access: Queries II Participation Project
Guidelines for Microsoft® Office 2013
Using Complex Formulas, Functions, and Tables
Pivot tables and charts
Microsoft Excel Basics: Pivot Tables
JOINS (Joinining multiple tables)
Shelly Cashman: Microsoft Access 2016
Getting Started with Data
New Perspectives on Microsoft
Microsoft Office Illustrated Fundamentals
Presentation transcript:

Ashley Ohmann June 20, 2013

* What is Custom SQL? * What can I do with it? * Join conditions * Unions and Self Joins * Ranks * Derived Tables

* Tableau queries each data source using SQL thats specific to the data type * The differences querying different data source types are in * Syntax * Functions * Field length limitations * Tableau allows the SQL used to query a data source to be customized (relatively easily) in order to manipulate the joins, filters, and field lengths and types to produce a more accurate output

* Every data connection, whether its to one or multiple tables, has custom SQL underpinning it. * The syntax between different database types variesfor instance, in most flat file and server-based data sources, table and field names are identified by brackets. * With Hive, the bracket has been replaced by ` * The functions in a query must be functions that the database can understand, which is a particular issue with SQL queries that aggregate * For the most part, all databases use some version of SQL, but there are minor differences * For instance, flat Microsoft-based sources, like Access, Excel, and flat files, use the conventions of Microsofts JET driver * Another example: the ISNULL function, which is used to replace nulls in SQL Server data sources and is a corollary to IFNULL in Tableau, is not read by Teradataone would use COALESCE instead.

* Join conditions tell the database how the fields in different tables are related to each other * A table is a queried set of data, not necessarily all of the rows and columns in a pre-defined table in a database * Custom SQL is a good tool for using joins to transform a data source * Inner join: keeps records common to both tables; this join type usually has the least number of null values, provided that the join condition is the right one * Left join: keeps all records from the first table and the matching records from the second table * Right join: keeps all records from the second table and the matching records from the first table * Outer/full join: keeps all records from both tables * Self join: refers to the same table in the inner and outer query. A union can be a self join. * Union: stacks tables on top of each other.

* Unions are particularly useful for transforming a data source, either because it was not set-up properly, or because one would like to create a visualization that needs for it to be set-up in a certain way * The Region Sales example data source presents the current and previous years sales by Division and Region for two different battery types. * The format is similar to that of a pivot table, and the problem with analyzing it is that the same measure is duplicated according to a qualitative attribute. * In order to analyze this data source and to create metrics based on the measures, it needs to be restructured: * We need to add a field that identifies the battery type as an attribute * We need to put the sales for this year and last year, respective, in one column

* Connect to the Union Data.xls data source, and select the Region Sales sheet. * Click the Custom SQL option under Step 2 of the data connection * Click the expansion button to see the whole Custom SQL window * Re-order the fields so that Division comes first, then Region * Highlight and copy all of the text, then underneath it, type UNION on a new row, then create another two carriage returns, and paste the code * In the outer query, create a new field that identifies the battery type as an attribute * Alias the battery types sales this year with a generic field name, and do the same for last years sales. Then, delete the fields that reference the other battery type * In the bottom query, paste the field that identifies the battery type attribute, but change the value to Energizer * Alias the sales fields for that battery type so that they have the same names as the sales fields in the outer query * Make sure that the commas are in the right places * Click OK, and then give the data source a good name

* Duplicate the data source, and call it Union with Calculated Fields * Well use the existing join conditions to add in some other calculations * Custom SQL can be used to apply conditions to different groups of data based on an attribute of the group * It might be useful to add a quota, and in this case, the quote will be the previous years sales multiplied by 1.15 for Energizer and 1.10 for Duracell * Edit the Custom SQL to multiple the previous years sales by the factors listed above, and call the new field [Quota] * This data is useful for making a nice little bullet chart. Add a calculated field in Tableau identifying whether or not the sales exceeded the quota, and put it on the color shelf.

* One of the shortcomings of Tableau is that it doesnt have a real ranking function * RAWSQL can be used to rank, but for a large data set with ranking conditions on several fields, its best to edit the SQL and include a ranking function * It doesnt look like an Excel data connection supports this, so one would need to pre-rank the data in Excel * For SQL Server or Oracle data connections, RANK and DENSE_RANK functions are appropriate * Syntax: RANK ( ) OVER ( [ partition_by_clause ] order_by_clause ) * Looks like: RANK() OVER (PARTITION BY i.LocationID ORDER BY i.Quantity DESC) AS Rank * I have added the sample data to my SQL Server and written a ranking querysee the next slide.

SELECT [Region Sales].[Division] AS [Division], [Region Sales].[Region] AS [Region], 'E' as [Battery Type], [Region Sales].[Energizer] as [TY Sales], DENSE_RANK() OVER (ORDER BY [Region Sales].[Energizer] DESC) AS TY_Rank FROM [dbo].[Region Sales] [Region Sales] Union SELECT [Region Sales].[Division] AS [Division], [Region Sales].[Region] AS [Region], 'D' as [Battery Type], [Region Sales].[Duracell] AS [TY Sales], DENSE_RANK() OVER (ORDER BY [Region Sales].[Duracell] DESC) AS Rank FROM [dbo].[Region Sales] [Region Sales]

* Derived tables in a query are very useful for transforming data as well as aggregating at different dimension levels * Well use a different version of our previous data set to create a derived table * The YOY Sales sheet in our data source shows, for each Division, Region, Battery Type, and Year, the Sales. * We want to have this years sales and last years sales as separate columns, though. * Unfortunately, we cant use a derived table with an Excel data connection, so the example on the next slide is for code from a SQL Server.

SELECT [TY].[Battery Type] AS [Battery Type], [TY].[Division] AS [Division], [TY].[Region] AS [Region], [TY].[Sales] AS [TY Sales], [LY].[LY Sales] AS [LY Sales] FROM [YOY Sales] [TY] Left Join (SELECT [YOY Sales].[Battery Type] AS [Battery Type], [YOY Sales].[Division] AS [Division], [YOY Sales].[Region] AS [Region], [YOY Sales].[Sales] AS [LY Sales] FROM [YOY Sales] where [YOY Sales].[Year] = 2012) as [LY] on [TY].[Battery Type] = [LY].[Battery Type] and [TY].[Division] = [LY].[Division] and [LY].[Region] = [LY].[Region] where [TY].[Year] = 2013