Top Banner
SQL’99 and some techniques of data mining Kiselyov Alexey
24

Sql 99 and_some_techniques

Jan 15, 2015

Download

Technology

Alexey Kiselyov

SQL 99 and some techniques
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sql 99 and_some_techniques

SQL’99 and some techniquesof data mining

Kiselyov Alexey

Page 2: Sql 99 and_some_techniques

Plan● Place of data mining methods in MVC model ● Data mining alternatives

○ Benefits and restrictions● Focus on SQL

○ Some "innovations" SQL99○ examples

Page 3: Sql 99 and_some_techniques

MVC

Trygve Reenskaug (1979)

passive active

Page 4: Sql 99 and_some_techniques

Methods to obtaining data

● DB native API & native SQL● SQL

○ SQL92○ SQL99○ SQL2003 ○ SQL2008

● Object-relational mapping (ORM)

Page 5: Sql 99 and_some_techniques

DB native API & native SQL

Benefits● Quick● Fine request tuning● Compact data transfer

Losses● Specificity● difficulty or inability to

transfer to another DB

Page 6: Sql 99 and_some_techniques

ORM layer

Benefits● Simple work with entities ● Misunderstanding data

extracting processes● Portability between any

DB (RDBMS and noSQL DBMS)

Losses● Misunderstanding data

extracting processes● Can't be used for

recursive data connections

Page 7: Sql 99 and_some_techniques

SQL

Benefits● Portability between other

RDBMS● Data extracting processes

is a clear● All thirteen from twelve

Codd's rules are rules!!! :)

Losses● No portability to noSQL

DBMS

Page 8: Sql 99 and_some_techniques

"Innovations" in examplesfrom simple to complex

and again to simple

Page 9: Sql 99 and_some_techniques

just "SELECT" them!!!

...but not only!

Page 10: Sql 99 and_some_techniques

Client (C): Please, give me all products from my storage where price greater then $100...Programmer (P): it's easy!!! > SELECT title, price FROM products WHERE price > 100

All are happy!!!

Page 11: Sql 99 and_some_techniques

C: Please, give me all products from my storage where price greater then $100 and corresponds to "tools" categoryP: (ha! it's easy too!!!) > SELECT p.title, p.price FROM products p INNER JOIN category c ON p.categoryId = c.idWHERE price > 100 and c.title = 'tool'

All are happy!!!

Page 12: Sql 99 and_some_techniques

C: Great! And now, please, give me all products from my storage where price greater then $100 and corresponds to "tools" category and I want to get all product with price greater then $100 that delivered from China!P: it's easy too... (but) > SELECT p.title, p.price FROM products p INNER JOIN category c ON p.categoryId = c.idWHERE price > 100 and c.title = 'tool'UNION ALLSELECT p.title, p.price FROM products p INNER JOIN countries cs ON p.deliveryId = cs.idWHERE price > 100 and cs.title = 'China'

Сustomer is happy... maybe

Page 13: Sql 99 and_some_techniques

C: Please, give me all products from my storage where price greater then $100 and corresponds to "tools" category, and I want to get all product with price greater then $100 that delivered from China! And I want to exclude from previous collection products with price greater than $150 from China produced before 01.01.2000. P: mmmm.... mama... :-(> SELECT x.title, x.price FROM ( SELECT p.title, p.price, p.produceDate, cs.title FROM products p INNER JOIN category c ON p.categoryId = c.id LEFT JOIN countries cs ON p.deliveryId = cs.id WHERE price > 100 and c.title = 'tool'UNION DISTINCT SELECT p.title, p.price, p.produceDate, cs.title FROM products p INNER JOIN countries cs ON p.deliveryId = cs.id WHERE price > 100 and cs.title = 'China') x) AS xWHERE !(x.price > 150 and x.produceDate > '01.01.2000' and x.title = 'China')

Page 14: Sql 99 and_some_techniques

WITH p100 as ( SELECT deliveryId, title, price, produceDate FROM products p WHERE price > 100)SELECT title, price FROM p100 INNER JOIN category c ON p100.categoryId = c.id WHERE c.title = 'tool'UNION DISTINCT SELECT title, price FROM p100 INNER JOIN countries cs ON p100.deliveryId = cs.id WHERE cs.title = 'China' and (p100.price > 150 and p100.produceDate > '01.01.2000')

Page 15: Sql 99 and_some_techniques

What more?

RECURSIVE QUERY

Page 16: Sql 99 and_some_techniques

Simple hierarchy

ID PARENT_ID CONTENT1 R State2 1 City 13 1 City 24 2 Сity block 415 4 Street 5416 4 Street 641

... ... ...

CREATE TABLE T2 (id VARCHAR(50), parent_id VARCHAR(20), content VARCHAR(20));

INSERT INTO T2 VALUES ('1', 'R', 'State');INSERT INTO T2 VALUES ('2', '1', 'City 1');INSERT INTO T2 VALUES ('3', '1', 'City 2');INSERT INTO T2 VALUES ('4', '2', 'Сity block 41');INSERT INTO T2 VALUES ('5', '4', 'Street 541');INSERT INTO T2 VALUES ('6', '4', 'Street 641');

Page 17: Sql 99 and_some_techniques

DB2, MS SQL

WITH TEMP(ID, PARENT_ID) AS( SELECT ID, PARENT_ID FROM T2 WHERE ID = '6' UNION ALL SELECT T2.ID, T2.PARENT_ID FROM TEMP, T2 WHERE TEMP.PARENT_ID = T2.ID ) SELECT * FROM TEMP

Page 18: Sql 99 and_some_techniques

Oracle

SELECT child FROM T2 WHERE id = '6'START WITH id = 'R' CONNECT BY PRIOR parent_id = id; Display the full tree (indenting child items) SELECT child FROM T2 START WITH id = 'R' CONNECT BY PRIOR parent_id = id;

Page 19: Sql 99 and_some_techniques

MySQL

Page 20: Sql 99 and_some_techniques

Infinitive loop

Page 21: Sql 99 and_some_techniques

DB2

WITH list(k, leaf, path) AS ( SELECT DISTINCT 1, id, parent_id||', '||id FROM T2 WHERE parent_id = 'R' UNION ALL SELECT k + 1, id, path || ', ' || id FROM list AS tt, T2 AS subroot WHERE k < 5 AND tt.leaf = subroot.parent_id AND LOCATE(subroot.id, tt.path) = 0)SELECT path FROM list; R, 1R, 1, 2R, 1, 3R, 1, 2, 4R, 1, 2, 4, 5R, 1, 2, 4, 6

Page 22: Sql 99 and_some_techniques

Oracle

1. How to exclude cycleSELECT child FROM T2 START WITH id IS NULL CONNECT BY NOCYCLE PRIOR parent_id = id;

2. How to restrict amount of hierarchy level SELECT child FROM T2 START WITH id IS NULL CONNECT BY PRIOR parent_id = id AND LEVEL < 5;

Page 23: Sql 99 and_some_techniques

Useful recursive SQL

WITH ALL_DAYS(DT) AS ( VALUES (DATE('2011-10-01')) UNION ALL SELECT DT + 1 DAY FROM ALL_DAYS WHERE DT < '2012-01-01' )SELECT DT FROM ALL_DAYS;

Page 24: Sql 99 and_some_techniques

I think it enough

Thank you for attention!!!