Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 111 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
111
Dung lượng
242 KB
Nội dung
84136 - Week lide Week - SQL more complex problems unit: 84136 by Kathy Egea 84136 - Week lide Text Edmond: Chapter 6,7 all database examples are based on subject database, p 92 text 84136 - Week lide Overview group by concept (alternative to week work) multiple group by queries useful product queries simple query nesting relational calculus and SQL more examples (reading 3: p.25-p.28) 84136 - Week lide Group by Concept The Group by clause is used to rearrange a table into categories These categories are then analysed usually using a statistical function example What is the total assessment for each student? 84136 - Week lide Group by Process Steps Steps to to guide guide group group by by queries queries Step Step1: 1: the the partition partition the the table table into into separate separate row row categories categories (group (groupby by ) ) Step Step2: 2: the the statistical statistical calculation calculation operations operations performed performedon on one oneor ormore more columns columns Step Step3: 3: the the output output one one row row per percategory category (select ) (select ) 84136 - Week lide example GROUP BY Item the Results table, P 92 Edmond ITEM 1 1 1 2 2 3 3 3 ID 871 862 854 872 868 869 871 869 872 862 868 869 872 862 868 871 854 SUBMITTED 908 907 908 910 906 909 1021 1022 1021 1022 1021 ? ? ? ? ? ? MARK 80 60 70 55 90 70 70 80 65 70 75 95 45 40 50 60 65 84136 - Week example GROUP BY Item method the table is partitioned into separate row categories for each item lide ITEM 1 1 1 2 2 3 3 3 ID 871 862 854 872 868 869 871 869 872 862 868 869 872 862 868 871 854 SUBMITTED 908 907 908 910 906 909 1021 1022 1021 1022 1021 ? ? ? ? ? ? MARK 80 60 70 55 90 70 70 80 65 70 75 95 45 40 50 60 65 84136 - Week example GROUP BY Id lide ITEM -1 3 3 3 ID 854 854 862 862 862 868 868 868 869 869 869 871 871 871 872 872 872 SUBMITTED 908 ? 907 1022 ? 906 1021 ? 909 1022 ? 908 1021 ? 910 1021 ? MARK -70 65 60 70 40 90 75 50 70 80 95 80 70 60 55 65 45 84136 - Week example GROUP BY Id the table is partitioned into row categories for each id (student) lide ITEM -1 3 3 3 ID 854 854 862 862 862 868 868 868 869 869 869 871 871 871 872 872 872 72 SUBMITTED 908 ? 907 1022 ? 906 1021 ? 909 1022 ? 908 1021 ? 910 1021 ? MARK -70 65 60 70 40 90 75 50 70 80 95 80 70 60 55 65 45 84136 - Week example GROUP BY Item Statistical calculation locate Min(Mark) for each group lide 10 Circle the answers ITEM 1 1 1 2 2 3 3 3 ID 871 862 854 872 868 869 871 869 872 862 868 869 872 862 868 871 854 SUBMITTED 908 907 908 910 906 909 1021 1022 1021 1022 1021 ? ? ? ? ? ? MARK 80 60 70 55 90 70 70 80 65 70 75 95 45 40 50 60 65 84136 - Week Group5.dat SELECT R.ID, MAX(FIRST),MAX(LAST), SUM(MARK*WEIGHT/100) FROM STUDENTS S, RESULTS R, ASSESS A WHERE S.ID = R.ID AND R.ITEM = A.ITEM GROUP BY R.ID ORDER BY R.ID R.ID MAX(FIRST) MAX(LAST) SUM(MARK*WEIGHT/100) - -854 Ann Dover 46 862 Bill Board 51 868 Will Gambol 61 869 Rip Orff 88 871 Hans Zupp 65 872 Betty Kahn 51 * END OF RESULT ***** ROWS DISPLAYED ************ lide 97 84136 - Week lide 98 Group5.dat SELECT R.ID, MAX(FIRST),MAX(LAST), SUM(MARK*WEIGHT/100) FROM STUDENTS S, RESULTS R, ASSESS A WHERE S.ID = R.ID AND R.ITEM = A.ITEM GROUP BY R.ID ORDER BY R.ID Question? 84136 - Week lide 99 Group5.dat SELECT R.ID, MAX(FIRST),MAX(LAST), SUM(MARK*WEIGHT/100) FROM STUDENTS S, RESULTS R, ASSESS A WHERE S.ID = R.ID AND R.ITEM = A.ITEM GROUP BY R.ID ORDER BY R.ID Question? List the name of each student and their overall mark 84136 - Week prod1.dat SELECT a.ID, a.Mark, b.Mark FROM Results a, Results b A.ID A.MARK B.MARK WHERE a.ID = b.ID - AND a.ITEM =1 AND b.item = END OF RESULT ***** and a.Mark > b.Mark ROWS DISPLAYED * Complete the table? lide 100 84136 - Week prod1.dat SELECT a.ID, a.Mark, b.Mark FROM Results a, Results b A.ID A.MARK B.MARK WHERE a.ID = b.ID -871 80 70 AND a.ITEM =1 868 90 75 AND b.item = END OF RESULT ***** and a.Mark > b.Mark lide 101 ROWS DISPLAYED * 84136 - Week prod1.dat SELECT a.ID, a.Mark, b.Mark FROM Results a, Results b A.ID A.MARK B.MARK WHERE a.ID = b.ID -871 80 70 AND a.ITEM =1 868 90 75 AND b.item = END OF RESULT ***** and a.Mark > b.Mark ROWS DISPLAYED * Question? lide 102 84136 - Week prod1.dat SELECT a.ID, a.Mark, b.Mark FROM Results a, Results b A.ID A.MARK B.MARK WHERE a.ID = b.ID -871 80 70 AND a.ITEM =1 868 90 75 AND b.item = END OF RESULT ***** and a.Mark > b.Mark ROWS DISPLAYED * Question? Who did better in assignment than assignment 2? lide 103 84136 - Week Nest1.dat SELECT Id, Item, Mark FROM Results WHERE Item = AND Mark > (SELECT Min(Mark) FROM Results WHERE Item = 1) complete this table lide 104 ID ITEM MARK END OF RESULT ***** ROWS DISPLAYED **** 84136 - Week Nest1.dat SELECT Id, Item, Mark FROM Results WHERE Item = AND Mark > (SELECT Min(Mark) FROM Results WHERE Item = 1) lide 105 ID ITEM MARK 871 80 862 60 854 70 868 90 869 70 END OF RESULT ***** ROWS DISPLAYED **** 84136 - Week Nest1.dat SELECT Id, Item, Mark FROM Results WHERE Item = AND Mark > (SELECT Min(Mark) FROM Results WHERE Item = 1) ID ITEM MARK 871 80 862 60 854 70 868 90 869 70 END OF RESULT ***** ROWS DISPLAYED **** Question? lide 106 84136 - Week Nest1.dat SELECT Id, Item, Mark FROM Results WHERE Item = AND Mark = (SELECT Min(Mark) FROM Results WHERE Item = 1) ID ITEM MARK 871 80 862 60 854 70 868 90 869 70 END OF RESULT ***** ROWS DISPLAYED **** Question? Which student obtained more than the lowest mark lide 107 in assignment 1? 84136 - Week Nest2.dat SELECT * Complete this table FROM Students ID FIRST LAST - -WHERE Id = (SELECT Id END OF RESULT **** FROM Results ROWS DISPLAYED WHERE Item = AND Mark = (SELECT MIN(Mark) FROM results WHERE Item = 1) ) lide 108 84136 - Week Nest2.dat SELECT * FROM Students ID FIRST LAST - -WHERE Id = 872 Betty Kahn (SELECT Id END OF RESULT **** FROM Results ROWS DISPLA WHERE Item = AND Mark = (SELECT MIN(Mark) FROM results WHERE Item = 1) ) lide 109 84136 - Week Question? SELECT * FROM Students ID FIRST LAST - -WHERE Id = 872 Betty Kahn (SELECT Id END OF RESULT **** FROM Results ROWS DISPLA WHERE Item = AND Mark = (SELECT MIN(Mark) FROM results WHERE Item = 1) ) lide 110 84136 - Week Question? List all details of the student who obtained the lowest mark SELECT * FROM Students ID FIRST LAST - -WHERE Id = 872 Betty Kahn (SELECT Id END OF RESULT **** FROM Results ROWS DISPLA WHERE Item = AND Mark = (SELECT MIN(Mark) FROM results WHERE Item = 1) ) lide 111 ... 1021 ? MARK -7 0 65 60 70 40 90 75 50 70 80 95 80 70 60 55 65 45 84136 - Week example GROUP BY Id the table is partitioned into row categories for each id (student) lide ITEM -1 3 3 3 ID ... 60 65 84136 - Week Step 3: the output Output SELECT Item,Min(Mark) FROM Results ITEM MIN(MARK) GROUP BY Item lide 16 - 55 65 40 * END OF RESULT **** * ROWS DISPLAYED * 84136 - Week What... ? ? ? ? ? ? MARK 80 60 70 55 90 70 70 80 65 70 75 95 45 40 50 60 65 84136 - Week example GROUP BY Id lide ITEM -1 3 3 3 ID 854 854 862 862 862 868 868 868 869 869 869 871 871 871 872 872