Aggregate Functions and Collations

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Working with cursors in Python GISDE Python Workshop Qiao Li.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Python Let’s get started!.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Querying Single Tables with SQL.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Web Database Programming Using PHP
SQL Query Getting to the data ……..
SQL – Python and Databases
Adapters and Converters
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Data Virtualization Community Edition
Python Let’s get started!.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Indices.
Web Database Programming Using PHP
CS-104 Final Exam Review Victor Norman.
Case Statements and Functions
Introduction To Codeigniter
Arrays and files BIS1523 – Lecture 15.
SQL – Dates and Times.
Object Oriented Programming
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
SQL – Parameterized Queries
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
SQL Text Manipulation Farrokh Alemi, Ph.D.
SQL – Python and Databases (Continued)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Binary Search Trees < > =
Introduction To Python
The compareTo interface
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction to Computer Science
Module 8 – Searching & Sorting Algorithms
MapInfo SQL String Functions
Text Manipulation Chapter 7 Attaway MATLAB 5E.
CISC101 Reminders Assignment 3 due today.
Trainer: Bach Ngoc Toan– TEDU Website:
Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Aggregate Functions and Collations

Aggregate Functions Aggregators (aggregate functions) take 0 or more values and return some form of summary or those values. When seen many before: count(x) - returns the number of items in x max(x) - returns the largest value in x ... You can also create a custom aggregator with the Python sqlite3 module Lets say we wanted an aggregate function called char_length that tallied the number of characters in the strings passed to it Example Usage: CREATE TABLE x (col TEXT); INSERT INTO x VALUES ('hi'), ('bye'); SELECT char_length(col) FROM x; -- returns 5

Custom Aggregator class CharCounter: def __init__(self): self.char_count = 0 def step(self, value): self.char_count += len(value) def finalize(self): return self.char_count conn.create_aggregate("char_length", 1, CharCounter)

create_aggregate Just like create_function, the create_aggregate method of the SQLite3 Connection class has three arguments: name - the SQL name to call the aggregate function by num_params - the number of parameters for the aggregate function aggregate_class - the Python class to pass data to The aggregate class must have three methods: __init__(self) - the init method that initializes the class (usually an attribute to 0) step(self, value) - a method that is called for every values to be aggregated finalize(self) - a method that returns the value for the aggregate function/class

How is an aggregate function different from a normal function? Aggregates get called multiple times Aggregates can have multiple parameters Aggregates can store state and return summaries Aggregates are more complicated

Collations Collations are the sort order and equality for a data type. SQLite has 3 built in collating functions (a.k.a. collating sequences): BINARY - Compares the binary representation of the two data types (default) NOCASE - Same as above except the 26 upper case letters are treated like lower case RTRIM - Same as BINARY except trailing whitespace is ignored You can specific that you want this sort order in two places: CREATE TABLE x (col TEXT COLLATE NOCASE); Or: SELECT * FROM x ORDER BY col COLLATE NOCASE;

Custom Collation Lets say we want a collation (named "LTRIM") that doesn't care about leading white space. " dog" == "dog" " cat" < "\n dog" We can define a collation function that does that: def collate_ltrim(left, right): left, right = left.lstrip(), right.lstrip() if left == right: return 0 if left < right: return -1 else: return 1 conn.create_collation("LTRIM", collate_ltrim) conn.execute("SELECT * FROM students ORDER BY name COLLATE LTRIM;")

create_collation The create_collation method of the Connection class takes two arguments: name - the name the collation is called in SQL queries callable - a function that is called to figure out if two values are greater then, less than, or equal to each other. The callable needs to take two values: If the values are equal, return 0 If the first value is smaller than the second, return -1 If the first value is greater than the second, return 1 If you need to remove a collation, just replace the callable with None: conn.create_collation("LTRIM", None)

Why would you want to define a collation as a column attribute instead of an ORDER BY clause? Makes no difference So that future ORDER BYs don't need to specify it explicitly If we want to change behavior of comparison operators (> < = ...) What's a collation?