CS240B, Winter 2017 Task 2.1:  Using a syntax based on that of notes and the two references above, express a user-defined aggregate d_count to perform.

Slides:



Advertisements
Similar presentations
1 Quick recap of the SQL & the GUI way in Management Studio The Adwentureworks database from the book A script to create tables & insert data for the Amazon.
Advertisements

1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Unary Query Processing Operators Not in the Textbook!
1 Views. 2 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view.
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
Copyright: Silberschatz, Korth and Sudarshan 1 OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
ATLaS: A Complete Database Language for Streams Carlo Zaniolo, Haixun Wang Richard Luo,Jan-Nei Law et al. Documentation and software downloads:
Microsoft Access 2010 Chapter 7 Using SQL.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Natural vs. Generated Keys. Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Web Programming MySql JDBC Web Programming.
Blocking, Monotonicity, and Turing Completeness in a Database Language for Sequences and Streams Yan-Nei Law, Haixun Wang, Carlo Zaniolo 12/06/2002.
Sofia, Bulgaria | 9-10 October SQL Querying Tips & Techniques Richard Campbell.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.
SQL and Query Execution for Aggregation. Example Instances Reserves Sailors Boats.
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
Chapter 02 (Part II) Introduction to C++ Programming.
SQL and Relational Algebra Edel Sherratt Nigel Hardy Horst Holstein.
Session 1 Retrieving Data From a Single Table
Web Systems & Technologies
Unary Query Processing Operators
Tuning Transact-SQL Queries
MySQL Subquery Source: Dev.MySql.com
Databases We are particularly interested in relational databases
CPSC-310 Database Systems
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
Views, Stored Procedures, Functions, and Triggers
Chapter 5: Advanced SQL Database System concepts,6th Ed.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
CPSC-608 Database Systems
SQL Views CS542.
CSCI 2141 – Intro to Database Systems
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL Views and Updates cs3431.
Chapter 4 Summary Query.
Access: SQL Participation Project
SQL: Structured Query Language
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
Probabilistic Databases
Information Management
CS240B: Assignment1 Winter 2016.
UCLA, Fall CS240B Midterm Your Name: and your ID:
CS240B—Fall 2018 Task 4.1.  Express the Flajolet-Martin's distinct_count sketch as a user-defined aggregate mamed dcount_sketch, to be called in the same.
CS240B, Spring 2014 Task 2.2:  Using a syntax based on that of notes and reference 3 above, express a user-defined aggregate d_count to perform the exact.
CS240B Midterm: Winter 2017 Your Name: and your ID:
CPSC-608 Database Systems
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Shelly Cashman: Microsoft Access 2016
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

CS240B, Winter 2017 Task 2.1:  Using a syntax based on that of notes and the two references above, express a user-defined aggregate d_count to perform the exact count of distinct values in a window on a data stream. Your window aggregate could, e.g., be called as follows: SELECT col_name1, d_count(col_name2)OVER (ROWS 99999 PRECEDING) FROM my_stream;

d_count exact count: optimize inwindow Increase the count only if this is not a duplicate; to avoid duplicates in invindow delete the old occurrences WINDOW AGGREGATE d_count(next Real) : Real { TABLE state(cnt Int); TABLE inwindow(wnext Real); INITIALIZE : {INSERT INTO state VALUES (1)} ITERATE : {/*the system inserts the new tuple in invindow at the end of iterate*/ UPDATE state SET cnt=cnt+1 WHERE next NOT IN (SELECT wnext FROM inwindow); DELETE FROM inwindow WHERE Wnext=next; INSERT INTO RETURN SELECT cnt FROM state} EXPIRE: { /* this is processed before ITERATE for each expired tuple */ UPDATE state SET cnt= cnt-1} }

d_count exact count of distinct values in a window If the value is added to inwindow at the beginning of the iterate, then we have a problem and we must have an additional table to memorize counts. WINDOW AGGREGATE d_count(next Real) : Real { TABLE dc(cnt Int); TABLE freq(Val Real, Cfreq Int); INITIALIZE : {INSERT INTO dc VALUES (1); INSERT INTO freq VALUES (next, 1)} ITERATE : {UPDATE dc SET cnt=cnt+1 WHERE next NOT IN (SELECT Val FROM freq WHERE Cfreq>0); INSERT INTO freq VALUES (next, 1) WHERE next NOT IN (SELECT Val FROM freq); UPDATE freq SET Cfreq = Cfre+1 WHERE next IN (SELECT Val FROM freq ); INSERT INTO RETURN SELECT cnt FROM cd} EXPIRE: { /* this is processed before ITERATE for each expired tuple */ UPDATE dc SET cnt= cnt-1 /* decrease dcount if freq=1*/ WHERE SELECT Cfreq FOM freq WHERE Val= oldest() AND Cfreq=1; UPDATE freq SET Cfreq= Cfreq-1 WHERE Val= oldest() }

Ranking Name Marks Rank DenseRank Tom 8 1 1 Jeff 7 2 2 Mary 7 2 2 Ranking is done in conjunction with an order by specification. Suppose we are given a relation student-marks(Name, marks) which stores the marks obtained by each student. The following query gives the rank of each student. Select Name, rank ( ) (order by (marks) desc) as s-rank, dense_rank ( ) (order by (marks) desc) as d-rank from student-marks order by s-rank Name Marks Rank DenseRank Tom 8 1 1 Jeff 7 2 2 Mary 7 2 2 Alex 6 4 3

Rank: unlimited preceding and Window If the value remains the same as the old one, so does the rank Otherwise the rank of the new value is the current count WINDOW AGGREGATE dense_rank(marks Real) : Real { TABLE state(lastrank Int, lastval Real); TABLE rcount(tcount), TABLE inwindow(wnext Real); INITIALIZE : {INSERT INTO state VALUES (marks, 1); INSERT INTO RETURN Values (marks, 1)} ITERATE : {UPDATE rcount SET tcount=tcount+1; UPDATE state SET Lastrank= (SELECT tcount FROM rcount), lastval=marks WHERE lastval<>marks INSERT INTO RETURN lastval, lastrank FROM state} EXPIRE: {UPDATE rcount SET tcnt= tcnt-1; UPDATE state SET Lastrank=Lastrank-1} /* this will produce the correct ranking for new tuples by revising the last count*/ }

Dense Rank If the value remains the same as the old one, so does the rank Otherwise the rank of the new value is the current d-count WINDOW AGGREGATE d_rank(marks Real) : Real {TABLE state(lastrank Int, lastval Real); TABLE rcnt(tcnt Int); TABLE inwindow(wnext Real); INITIALIZE : {INSERT INTO rcnt VALUES (marks, 1 ); INSERT INTO RETURN Values (marks, 1)} ITERATE : {UPDATE rcnt SET cnt=cnt+1 WHERE next NOT IN (SELECT wnext FROM inwindow); DELETE FROM inwindow WHERE Wnext=next; UPDATE state SET Lastrank= (SELECT tcount FROM rcount), lastval=marks WHERE lastval<>marks INSERT INTO RETURN lastval, lastrank FROM state} EXPIRE: {UPDATE rcount SET tcnt= tcnt-1; UPDATE state SET Lastrank=Lastrank-1}