SQL’99 and some techniques      of data mining       Kiselyov Alexey
Plan● Place of data mining methods in MVC model● Data mining alternatives   ○ Benefits and restrictions● Focus on SQL   ○ ...
MVCpassive                       active          Trygve Reenskaug (1979)
Methods to obtaining data ● DB native API & native SQL ● SQL    ○ SQL92    ○ SQL99    ○ SQL2003    ○ SQL2008 ● Object-rela...
DB native API & native SQL        Benefits                      Losses● Quick                   ● Specificity● Fine reques...
ORM layer          Benefits                       Losses● Simple work with entities   ● Misunderstanding data● Misundersta...
SQL           Benefits                       Losses● Portability between other      ● No portability to noSQL  RDBMS      ...
"Innovations" in examples  from simple to complex    and again to simple
just "SELECT" them!!!      ...but not only!
Client (C): Please, give me all products from my storage whereprice greater then $100...Programmer (P): its easy!!!> SELEC...
C: Please, give me all products from my storage where pricegreater then $100 and corresponds to "tools" categoryP: (ha! it...
C: Great! And now, please, give me all products from mystorage where price greater then $100 and corresponds to"tools" cat...
C: Please, give me all products from my storage where pricegreater then $100 and corresponds to "tools" category, and Iwan...
WITHp100 as ( SELECT deliveryId, title, price, produceDate FROM products p WHERE price> 100)SELECT title, priceFROM p100 I...
What more?RECURSIVE QUERY
Simple hierarchy                   ID                  PARENT_ID                    CONTENT                   1           ...
DB2, MS SQLWITH TEMP(ID, PARENT_ID) AS(  SELECT ID, PARENT_ID FROM T2 WHERE ID = 6  UNION ALL  SELECT T2.ID, T2.PARENT_ID ...
OracleSELECT child FROM T2 WHERE id = 6START WITH id = R CONNECT BY PRIOR parent_id = id;Display the full tree (indenting ...
MySQL
Infinitive loop
DB2WITH list(k, leaf, path) AS( SELECT DISTINCT 1, id, parent_id||, ||id FROM T2 WHERE parent_id = R UNION ALL SELECT k + ...
Oracle1. How to exclude cycleSELECT child FROM T2 START WITH id IS NULL CONNECTBY NOCYCLE PRIOR parent_id = id;2. How to r...
Useful recursive SQLWITH ALL_DAYS(DT) AS (   VALUES (DATE(2011-10-01))    UNION ALL   SELECT DT + 1 DAY FROM ALL_DAYS   WH...
I think it enoughThank you for attention!!!
Upcoming SlideShare
Loading in …5
×

Sql 99 and_some_techniques

729 views

Published on

SQL 99 and some techniques

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
729
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql 99 and_some_techniques

  1. 1. SQL’99 and some techniques of data mining Kiselyov Alexey
  2. 2. Plan● Place of data mining methods in MVC model● Data mining alternatives ○ Benefits and restrictions● Focus on SQL ○ Some "innovations" SQL99 ○ examples
  3. 3. MVCpassive active Trygve Reenskaug (1979)
  4. 4. Methods to obtaining data ● DB native API & native SQL ● SQL ○ SQL92 ○ SQL99 ○ SQL2003 ○ SQL2008 ● Object-relational mapping (ORM)
  5. 5. DB native API & native SQL Benefits Losses● Quick ● Specificity● Fine request tuning ● difficulty or inability to● Compact data transfer transfer to another DB
  6. 6. ORM layer Benefits Losses● Simple work with entities ● Misunderstanding data● Misunderstanding data extracting processes extracting processes ● Cant be used for● Portability between any recursive data DB (RDBMS and noSQL connections DBMS)
  7. 7. SQL Benefits Losses● Portability between other ● No portability to noSQL RDBMS DBMS● Data extracting processes is a clear● All thirteen from twelve Codds rules are rules!!! :)
  8. 8. "Innovations" in examples from simple to complex and again to simple
  9. 9. just "SELECT" them!!! ...but not only!
  10. 10. Client (C): Please, give me all products from my storage whereprice greater then $100...Programmer (P): its easy!!!> SELECT title, price FROM products WHERE price > 100 All are happy!!!
  11. 11. C: Please, give me all products from my storage where pricegreater then $100 and corresponds to "tools" categoryP: (ha! its easy too!!!)> SELECT p.title, p.priceFROM products p INNER JOIN category c ON p.categoryId = c.idWHERE price > 100 and c.title = tool All are happy!!!
  12. 12. C: Great! And now, please, give me all products from mystorage where price greater then $100 and corresponds to"tools" category and I want to get all product with price greaterthen $100 that delivered from China!P: its easy too... (but)> SELECT p.title, p.priceFROM products p INNER JOIN category c ON p.categoryId = c.idWHERE price > 100 and c.title = toolUNION ALLSELECT p.title, p.priceFROM products p INNER JOIN countries cs ON p.deliveryId = cs.idWHERE price > 100 and cs.title = China Сustomer is happy... maybe
  13. 13. C: Please, give me all products from my storage where pricegreater then $100 and corresponds to "tools" category, and Iwant to get all product with price greater then $100 thatdelivered from China! And I want to exclude from previouscollection products with price greater than $150 from Chinaproduced 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 = toolUNION 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)
  14. 14. WITHp100 as ( SELECT deliveryId, title, price, produceDate FROM products p WHERE price> 100)SELECT title, priceFROM p100 INNER JOIN category c ON p100.categoryId = c.idWHERE c.title = toolUNION DISTINCTSELECT title, priceFROM p100 INNER JOIN countries cs ON p100.deliveryId = cs.idWHERE cs.title = China and (p100.price > 150 and p100.produceDate > 01.01.2000)
  15. 15. What more?RECURSIVE QUERY
  16. 16. Simple hierarchy ID PARENT_ID CONTENT 1 R State 2 1 City 1 3 1 City 2 4 2 Сity block 41 5 4 Street 541 6 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);
  17. 17. DB2, MS SQLWITH 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
  18. 18. OracleSELECT child FROM T2 WHERE id = 6START WITH id = R CONNECT BY PRIOR parent_id = id;Display the full tree (indenting child items)SELECT child FROM T2START WITH id = R CONNECT BY PRIOR parent_id = id;
  19. 19. MySQL
  20. 20. Infinitive loop
  21. 21. DB2WITH 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
  22. 22. Oracle1. How to exclude cycleSELECT child FROM T2 START WITH id IS NULL CONNECTBY NOCYCLE PRIOR parent_id = id;2. How to restrict amount of hierarchy levelSELECT child FROM T2 START WITH id IS NULL CONNECTBY PRIOR parent_id = id AND LEVEL < 5;
  23. 23. Useful recursive SQLWITH 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;
  24. 24. I think it enoughThank you for attention!!!

×