Using Structured Query Language (SQL) (continued)

Slides:



Advertisements
Similar presentations
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 10 – Designing.
Advertisements

Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Chapter 11 Group Functions
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Exercises: Organizing, Loading, and Managing Point Observations Using HydroServer Support EAR CUAHSI HIS Sharing hydrologic data
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Data Model / Database Implementation (continued) Jeffery S. Horsburgh Hydroinformatics Fall 2014 This work was funded by National Science Foundation Grants.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
Lecture 9 Using Structured Query Language (SQL) Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant.
Lecture 10 Aggregating & Pivoting Data in SQL and Excel David E. Rosenberg CEE 6930 – Hydroinformatics Fall 2012 This work was funded by National Science.
Lecture 5 Data Model Design Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant EPS
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Using Python to Retrieve Data from the CUAHSI HIS Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2015 This work was funded by National Science.
Using Structured Query Language (SQL) Jeffery S. Horsburgh Hydroinformatics Fall 2014 This work was funded by National Science Foundation Grants EPS
1 of 42 Lecture 5 Data Model Design Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National Science Foundation Grants EPS
Jeffery S. Horsburgh Hydroinformatics Fall 2014
SQL Query Getting to the data ……..
In this session, you will learn to:
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Lecture 8 Database Implementation
The Database Exercises Fall, 2009.
SQL FUNDAMENTALS CDSE Days 2018.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
CS 405G: Introduction to Database Systems
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
GROUP BY & Subset Data Analysis
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Instructor: Mohamed Eltabakh
SQL – Entire Select.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Access: SQL Participation Project
Creating Noninput Items
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Chapter 8 Advanced SQL.
Section 4 - Sorting/Functions
Database Systems: Design, Implementation, and Management Tenth Edition
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Group Operations Part IV.
Presentation transcript:

Using Structured Query Language (SQL) (continued) Jeffery S. Horsburgh Hydroinformatics Fall 2014 This work was funded by National Science Foundation Grants EPS 1135482 and EPS 1208732

Objectives Retrieve and use data from data models used in Hydrology such as the Observations Data Model (ODM) Introduce the syntax of Structured Query Language (SQL) for common query types Construct SQL queries to retrieve data

Quick Review What we covered last time Basic query structure – SELECT FROM WHERE Ordering results – ORDER BY Select distinct values – DISTINCT Selecting from more than one table – JOIN

Aggregate Functions Compute against a column of numeric data MIN – Returns the smallest value in a given selection MAX – Returns the largest value in a given selection SUM – Returns the sum of numeric values in a given selection AVG – Returns the average of numeric values in a given selection COUNT – Returns the total number of values in a given selection COUNT(*) – Returns the number of records in a table

Aggregate Function Example ValueID VariableName DateTime DataValue 1 Temperature 1/1/2013 10:30 8 2 1/1/2013 11:30 9 3 1/1/2013 12:30 7 4 1/1/2013 1:30 5 1/1/2013 2:30 10 6 1/1/2013 3:30 12 1/1/2013 4:30 13 1/1/2013 5:30 16 1/1/2013 6:30 1/1/2013 7:30 VariableName AverageValue Temperature 10.6 Calculate a single average value from a time series of values.

Aggregate Functions and NULL Values All aggregation functions except COUNT(*) ignore NULL values in the input set If the input set is empty, NULL is returned

Aggregation Example (1) Example: “Give me the number of observations and the minimum, maximum, and average quality controlled (QualityControlLevelID = 1) turbidity (VariableID = 6) value in the Little Bear River at Mendon Road (SiteID = 1). First make sure you have the right set of DataValues: SELECT * FROM DataValues WHERE SiteID = 1 AND VariableID = 6 AND QualityControlLevelID = 1;

Aggregation Example (2) Example: “Give me the number of observations and the minimum, maximum, and average quality controlled (QualityControlLevelID = 1) turbidity (VariableID = 6) value in the Little Bear River at Mendon Road (SiteID = 1). Now modify the SELECT statement to add the aggregation functions: SELECT COUNT(DataValue) AS Count, MIN(DataValue) AS Minimum, MAX(DataValue) AS Maximum, AVG(DataValue) AS Average FROM DataValues WHERE SiteID = 1 AND VariableID = 6 AND QualityControlLevelID = 1;

GROUP BY Clause Used with Aggregate functions Groups records into sets for which the aggregate function should be evaluated When using aggregate functions, every selected field must be part of either an aggregate function or a “GROUP BY” clause

Aggregation Example with GROUP BY SELECT SiteID, VariableID, AVG(DataValue) AS AvgDataValue FROM DataValues GROUP BY SiteID, VariableID; DataValues ValueID SiteID VariableID DateTime DataValue 1 3 1/1/2012 8 2 1/2/2012 9 10 4 12 SiteID VariableID AvgDataValue 1 3 8.5 2 11 Result

Example GROUP BY Clause “Give me the minimum, maximum, and average value of quality controlled (QualityControlLevelID=1) turbidity (VariableID=6) for each Site.” SELECT SiteID, MIN(DataValue) AS Minimum, MAX(DataValue) AS Maximum, AVG(DataValue) AS Average FROM DataValues WHERE VariableID = 6 AND QualityControlLevelID = 1 GROUP BY SiteID; The “GROUP BY” clause ensures that the query calculates values for each unique SiteID.

Arithmetic Functions Computed attributes Example: “Add a constant value to water level measurements to convert from gage height to water surface elevation.” SELECT LocalDateTime, DataValue + 4380 AS Elevation FROM DataValues WHERE SiteID = 1 AND VariableID = 13 AND QualityControlLevelID = 1 ORDER BY LocalDateTime ASC

Date/Time Functions Example: “Give me the average water temperature (VariableID = 36) in the Little Bear River at Mendon Road (SiteID = 1) for each day of the year.” Use the MONTH() and DAY() functions: SELECT MONTH(LocalDateTime) AS theMonth, DAY(LocalDateTime) AS theDay, AVG(DataValue) AS AvgTemp FROM DataValues WHERE SiteID = 1 AND VariableID = 36 AND QualityControlLevelID = 1 AND DataValue <> -9999 GROUP BY MONTH(LocalDateTime), DAY(LocalDateTime) ORDER BY theMonth, theDay

Challenge Query 1 “How many observations of quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) are there in the Little Bear River at Mendon Road (SiteID = 1)?”

Challenge Query 1 - Solution “How many observations of quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) are there in the Little Bear River at Mendon Road (SiteID = 1)?” SELECT COUNT(*) FROM DataValues WHERE SiteID = 1 AND VariableID = 36 AND QualityControlLevelID = 1;

Challenge Query 2 “What are the maximum and minimum values of quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) in the Little Bear River at Mendon Road (SiteID = 1)?”

Challenge Query 2 - Solution “What are the maximum and minimum values of quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) in the Little Bear River at Mendon Road (SiteID = 1)?” SELECT MAX(DataValue) AS MaxTemp, MIN(DataValue) AS MinTemp FROM DataValues WHERE SiteID = 1 AND VariableID = 36 AND QualityControlLevelID = 1 AND DataValue <> -9999;

Assignment 4 Perform exploratory data analysis using the water temperature datasets in the Little Bear River ODM database Compare water temperature data to the state of Utah water temperature numeric criterion value for streams designated as cold water fisheries Perform analyses that may identify potential water temperature impairment

Water Temperature in LBR at Mendon Road

Assignment 4 Queries A table listing the period of record for water temperature measurements (e.g., begin and end date), the number of observations, and the overall minimum, maximum, and average values for each site at which quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) data have been collected. A table listing the total number of temperature observations, the number of observations greater than the water quality criterion value (i.e., 20 degrees C), and the overall percent exceedence of the water quality criterion value for each site at which quality controlled water temperature data have been collected. 

Assignment 4 Queries A table for the Little Bear River at Mendon Road (SiteID = 1) listing the percent exceedence of the water quality standard for each month of the year. A table listing the percent exceedence of the water quality standard for each site during the month of July, which is generally a critical period with low flows and elevated temperatures.

Advanced SQL Functionality

Mathematical Functions ABS DEGREES RAND ACOS EXP ROUND ASIN FLOOR SIGN ATAN LOG SIN ATN2 LOG10 SQRT CEILING PI SQUARE COS POWER TAN COT RADIANS  

Subqueries A subquery is a SELECT FROM WHERE expression that is nested within another query Many SQL queries that include subqueries can be alternatively formulated as joins

Subqueries in the WHERE Clause Example: “Do we have any Variables in the database for which there are no DataValues?” SELECT VariableID, VariableName FROM Variables WHERE VariableID NOT IN (SELECT DISTINCT VariableID FROM DataValues);

Nested Subqueries Example: “Give me all quality controlled (QualityControlLevelID = 1) water temperature (VariableID = 36) observations in the Little Bear River at Mendon Road (SiteID = 1) that are greater than the average temperature value.” SELECT * FROM DataValues WHERE SiteID = 1 AND VariableID = 36 AND QualityControlLevelID = 1 AND DataValue > (SELECT AVG(DataValue) FROM DataValues WHERE SiteID = 1 AND VariableID = 36 AND QualityControlLevelID = 1)

Subqueries in the FROM Clause A name has to be given to the derived table in the subquery Example: “What is the maximum water temperature at the site that has the highest maximum temperature.” SELECT MAX(maxTemperature) AS OverallMax FROM (SELECT SiteID, MAX(DataValue) AS maxTemperature FROM DataValues WHERE VariableID = 36 AND QualityControlLevelID = 1 GROUP BY SiteID) AS MaxTempValues

PIVOT Convert data from a serial format to a cross-tabulated format Uses a field’s values as column headers Field 1 Field 2 Field 3 1 A B 2 3 C 4 Cross tab Field 1 A B C 1 2 3 4

PIVOT Example: “Give me a table with a single LocalDateTime column with time-matched temperature (VariableID = 36) and dissolved oxygen (VariableID = 32) values in additional columns for the Little Bear River at Mendon Road (SiteID = 1).”

PIVOT SELECT SiteID, LocalDateTime, [36] AS Temperature_C, [32] AS DO_mgL FROM (SELECT SiteID, LocalDateTime, DataValue, VariableID FROM DataValues WHERE SiteID = 1 AND QualityControlLevelID = 1 AND VariableID IN (32,36)) dv PIVOT(SUM(DataValue) FOR VariableID IN ([32],[36])) AS pvt ORDER BY LocalDateTime

Steps in PIVOTing (1) Write the base query Only include columns needed in the final results Assign an alias to the virtual table created by the base query Columns in base query not pivoted or aggregated will cause extra grouping levels and unexpected results SELECT SiteID, LocalDateTime, [36] AS Temperature_C, [32] AS DO_mgL FROM (SELECT SiteID, LocalDateTime, DataValue, VariableID FROM DataValues WHERE SiteID = 1 AND QualityControlLevelID = 1 AND VariableID IN (32,36)) dv PIVOT(SUM(DataValue) FOR VariableID IN ([32],[36])) AS pvt ORDER BY LocalDateTime

Steps in PIVOTing (2) Create the PIVOT Expression Select an aggregate function (SUM,MIN, MAX, AVG, etc.) for the column that will be used as values in the resulting table Include the keyword FOR and the name of the pivoted column Provide a list of values for column names Provide an alias for the PIVOT expression SELECT SiteID, LocalDateTime, [36] AS Temperature_C, [32] AS DO_mgL FROM (SELECT SiteID, LocalDateTime, DataValue, VariableID FROM DataValues WHERE SiteID = 1 AND QualityControlLevelID = 1 AND VariableID IN (32,36)) dv PIVOT(SUM(DataValue) FOR VariableID IN ([32],[36])) AS pvt ORDER BY LocalDateTime

Steps in PIVOTing (3) Add column names to the SELECT list Pivoted columns will display in the order listed in the SELECT clause Do not list the aggregated column in the SELECT statement SELECT SiteID, LocalDateTime, [36] AS Temperature_C, [32] AS DO_mgL FROM (SELECT SiteID, LocalDateTime, DataValue, VariableID FROM DataValues WHERE SiteID = 1 AND QualityControlLevelID = 1 AND VariableID IN (32,36)) dv PIVOT(SUM(DataValue) FOR VariableID IN ([32],[36])) AS pvt ORDER BY LocalDateTime

Other Things You can do with SQL Create databases Create tables Insert data into tables Update exiting records Delete records, tables, databases Create users and permissions …

Summary Aggregate functions provide a powerful way to summarize data Subqueries provide a convenient way to “materialize” a virtual table and then make selections from it Pivoting and unpivoting enable you to reorganize your data in crosstab or serial format SQL supports a suite of mathematical, date/time manipulation, and other functions