Download presentation
Presentation is loading. Please wait.
Published byAmbrose Evans Modified over 9 years ago
1
Installment Numbers 6 Answers to Puzzle Corner Problems (Installment Numbers1-5) 會研所 黃潔
2
The Median Problem Source: “ Theory Is Practical! ” Problem statement: write a sequence of SQL statements to compute the median of the values in column T.C.
3
Solution: INSERT INTO T1 (C) SELECT T.C FROM T UNION ALL SELECT T.C FROM T ; INSERT INTO T2 (C) SELECT DISTINCT T1.C FROM T1 WHERE ( SELECT COUNT ( * ) FROM T ) <= ( SELECT COUNT ( * ) FROM T1 X WHERE X.C>=T1.C ) AND ( SELECT COUNT ( * ) FROM T ) <= (SELECT COUNT ( * ) FROM T1 X WHERE X.C <= T1.C ) ; INSERT INTO T3 ( M ) SELECT AVG ( T2.C ) FROM T2 ;
4
Explanation (1/2) We can avoid unknown whether column T.C contains an odd or even number. The purpose of the first INSERT above is to create a table T1 that is a “ doubled up ” version of table T.
5
Explanation (2/2) The second and third INSERTs make use of the following simple theorem. Theorem: S = x [0] ≦ x [1] ≦ … ≦ x [2n-1] The median M= ( x [n-1] + x [n] ) /2
6
Comments (1/2) Ignore the possibility that the given sequence might contain nulls. Omitted the necessary CREATE TABLEs for the intermediate and final result tables T1, T2, and T3. Using the SQL/92 dialect of SQL.
7
Comments (2/2) Allowing tables to include duplicate rows. What happens if table t is empty? What is the median of an empty sequence of value ? The answer clearly is undefined.
8
The Identity Problem Source: ” The Importance of Closure ” Problem statement: What is the identity in the relational model with respect to join and Cartesian product?
9
The Winners Problem Source: “ What ’ s in a Name? ” Problem statement: given the relation EDT { E, D, T, … } PRIMARY KEY { D,T } Use relational algebra operators to produce the relation RESULT{ E, N } PRIMARY KEY { E }
10
Solution T1:= SUMMARIZE EDT BY (E,D)ADD COUNT AS CT; /* T1 {E, D, CT} - E occurs CT times on D */ T2:= SUMMARIZE T1 BY (D) ADD MAX(CT) AS CT; /* T2{D, CT} – CT is max no. of times any E occurs on D */ T3:= T1 JOIN T2; /* T3{E, D, CT} – E occurs CT times on D and no distinct */ /* E ’ occurs more than CT times on D */ T4:= SUMMARIZE T3 BY (E) ADD COUNT AS N; /* T4{E, N } – N is no. of days on which e is a winner */
11
Comments Broken the overall problem into a sequence of separate relational assignments. A good implementation of the relational algebra would not require any such declarations. Here is an SQL analog of the foregoing solution.
12
The Three-valued Logic Problem Source: “ Why Three – Valued Logic Is a Mistake ” Problem statement: the three-valued logic (3VL) and real world answers to the following pseudoSQL query: SELECT E# FROM EMP WHERE MAYBE (D# = ‘ D1 ’ ) ;
13
Solution The 3VL answer is “ E1 ”. The real – world answer, by contrast, is no employee numbers at all. The expression in the WHERE clause must evaluate to false in the real world. 3VL does not match reality.
14
Operators of Two-Valued Logic Source: “ Nothing in Excess ” Problem statement: The four monadic and 16 dyadic operators of two-valued logic can all be formulated in terms of suitable combinations of NOT and either AND or OR.
15
Solution Let f(A) be the monadic operator Let f(A,B) be the dyadic operator
16
The median problem revisited
17
The winners problem revisited
18
2VL operators revisited 2VL operators can all be formulated in terms of the single logical operator “ NOR ”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.