characteristic functions

33
Characteristic Functions

Upload: emmett

Post on 08-Jan-2016

57 views

Category:

Documents


1 download

DESCRIPTION

Characteristic Functions. Characteristic Functions. (from fin_data table in Sybase Sample Database) Have: Year quarter code amount 2001Q1e1 198 2001Q2e1 204 2001Q3e1 214 2001Q4e1 231. Want: Year Code Q1Amt Q2Amt Q3Amt Q4Amt - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Characteristic Functions

Characteristic Functions

Page 2: Characteristic Functions

Characteristic Functions

Want:

Year Code Q1Amt Q2Amt Q3Amt Q4Amt2001 e1 198 204 214 231

(from fin_data table in Sybase Sample Database)

Have:

Year quarter code amount2001 Q1 e1 1982001 Q2 e1 2042001 Q3 e1 2142001 Q4 e1 231

Page 3: Characteristic Functions

Characteristic Functions

Want: Un-normalized

Year Code Q1Amt Q2Amt Q3Amt Q4Amt2001 e1 198 204 214 231

Have: Normalized

Year quarter code amount2001 Q1 e1 1982001 Q2 e1 2042001 Q3 e1 2142001 Q4 e1 231

Page 4: Characteristic Functions

Characteristic Functions

Pivot Table

Year Code Q1Amt Q2Amt Q3Amt Q4Amt2001 e1 198 204 214 231

Similar to Excel Database Format

Year quarter code amount2001 Q1 e1 1982001 Q2 e1 2042001 Q3 e1 2142001 Q4 e1 231

Page 5: Characteristic Functions

Possible Solutions(without using Characteristic Functions)

Self Join as in type 4 queries

Page 6: Characteristic Functions

Possible Solution – Self Join

Start with just Q1 and Q2, code E1, Year 2001

Year code Q1Amt Q2Amt

2001 e1 198 204

Use a self join as in type 4 queries

Write the SQL query

Page 7: Characteristic Functions

Possible Solution – Self Join

Code

SELECT F1.year, F1.code, F1.amount AS Q1Amt, F2.amount AS Q2Amt FROM fin_data F1, fin_data F2

WHERE F1.year = 2001 // Only 2001AND F2.year = 2001

AND F1.code = ‘e1’ // Only financial code e1AND F2.code = ‘e1’

AND F1.quarter = ‘Q1’ // Get Q1 amountAND F2.quarter = ‘Q2’ // Get Q2 amount

Year code Q1Amt Q2Amt

2001 e1 198 204

Page 8: Characteristic Functions

Possible Solution – Self Join

Expand to all years

Year code Q1Amt Q2Amt

1999 e1 101 932000 e1 153 1492001 e1 198 204

Page 9: Characteristic Functions

Possible Solution – Self Join

Code

SELECT F1.year, F1.code, F1.amount AS Q1Amt, F2.amount AS Q2Amt FROM fin_data F1, fin_data F2

WHERE F1.year = F2.year // Same Year

AND F1.code = ‘e1’ // Only financial code e1AND F2.code = ‘e1’

AND F1.quarter = ‘Q1’ // Get Q1 amountAND F2.quarter = ‘Q2’ // Get Q2 amount

Year code Q1Amt Q2Amt

1999 e1 101 932000 e1 153 1492001 e1 198 204

Expand to all years

Page 10: Characteristic Functions

Possible Solution – Self Join

Expand to all four quarters

Year code Q1Amt Q2Amt Q3Amt Q4Amt

1999 e1 101 93 129 1452000 e1 153 149 157 1632001 e1 198 204 214 231

Page 11: Characteristic Functions

Possible Solution – Self Join

Code

All four quarters

SELECT F1.year, F1.code, F1.amount AS Q1Amt, F2.amount AS Q2Amt, F3.amount AS Q3Amt, F4.amount AS Q4AmtFROM fin_data F1, fin_data F2, fin_data F3, fin_data F4 WHERE F1.year = F2.year // Same YearAND F2.year = F3.yearAND F3.year = F4.yearAND F1.code = ‘e1’ // Only financial code e1AND F2.code = ‘e1’AND F3.code = ‘e1’AND F4.code = ‘e1’AND F1.quarter = ‘Q1’ // One record for each quarterAND F2.quarter = ‘Q2’AND F3.quarter = ‘Q3’AND F4.quarter = ‘Q4’

Year code Q1Amt Q2Amt Q3Amt Q4Amt

1999 e1 101 93 129 1452000 e1 153 149 157 1632001 e1 198 204 214 231

Page 12: Characteristic Functions

Possible Solution – Self Join

Problems

Coding:Suppose we wanted months instead of quarters…

Performance:Suppose fin_data had 100,000 records instead of 84…

We need a better solution!

Page 13: Characteristic Functions

Possible Solutions(without using Characteristic Functions)

SubQueries

Page 14: Characteristic Functions

Possible Solution – SubQueries

Start with just Q1 and Q2, code E1, Year 2001

Year code Q1Amt Q2Amt

2001 e1 198 204

Use a subquery as a field in the select clause

Write the SQL query

Page 15: Characteristic Functions

Possible Solution – SubQueries

Code

SELECT F1.year, F1.code, F1.amount AS Q1Amt,( SELECT F2.amount as Q2Amt FROM fin_data F2 WHERE F2.quarter = ‘Q2’ AND F2.code = ‘e1’ AND F2.year = 2001)FROM fin_data F1WHERE F1.quarter = ‘Q1’AND F1.code = ‘e1’AND F1.year = 2001

Year code Q1Amt Q2Amt

2001 e1 198 204

Use a subquery as a field in the select clause

Page 16: Characteristic Functions

Possible Solution – SubQueries

Expand to all years

Year code Q1Amt Q2Amt

1999 e1 101 932000 e1 153 1492001 e1 198 204

Page 17: Characteristic Functions

Possible Solution – SubQueries

Code

SELECT F1.year, F1.code, F1.amount AS Q1Amt,( SELECT F2.amount as Q2Amt FROM fin_data F2 WHERE F2.quarter = ‘Q2’ AND F2.code = ‘e1’ AND F2.year = F1.year)FROM fin_data F1WHERE F1.quarter = ‘Q1’AND F1.code = ‘e1’//AND F1.year = 2001

Year code Q1Amt Q2Amt

1999 e1 101 932000 e1 153 1492001 e1 198 204

Expand to all years

This is now a correlated subquery

Page 18: Characteristic Functions

Possible Solution – SubQueries

Expand to all four quarters

Year code Q1Amt Q2Amt Q3Amt Q4Amt

1999 e1 101 93 129 1452000 e1 153 149 157 1632001 e1 198 204 214 231

Page 19: Characteristic Functions

Possible Solution – SubQueries

Code

All four quarters

SELECT F1.year, F1.code, F1.amount AS Q1Amt,( SELECT F2.amount as Q2Amt FROM fin_data F2 WHERE F2.quarter = ‘Q2’ AND F2.code = ‘e1’ AND F2.year = F1.year),( SELECT F2.amount as Q3Amt FROM fin_data F2 WHERE F2.quarter = ‘Q3’ AND F2.code = ‘e1’ AND F2.year = F1.year),

Year code Q1Amt Q2Amt Q3Amt Q4Amt

1999 e1 101 93 129 1452000 e1 153 149 157 1632001 e1 198 204 214 231

( SELECT F2.amount as Q4Amt FROM fin_data F2 WHERE F2.quarter = ‘Q4’ AND F2.code = ‘e1’ AND F2.year = F1.year)

FROM fin_data F1WHERE F1.quarter = ‘Q1’AND F1.code = ‘e1’

Page 20: Characteristic Functions

Possible Solution – Self Join

ProblemsCoding:Again, Suppose we wanted months instead of quarters…

Performance:Our example requires the effective execution of 10

queries• The F1 query is run once to return the year, code and

Q1Amt columns• The F2 query is run 9 times return the Q2, Q3, and

Q4 amounts for 1999, 2000 and 2001 If our table had 100,000 rows F2 would run 300,000

times!

We still need a better solution!

Page 21: Characteristic Functions

Possible Solution – Temporary Table

Strategy

Create a table with fields for year, code, Q1Amt, Q2Amt, Q3Amt, and Q4Amt

Insert Query to add records and fill in year, code, Q1Amt

Update Query to add Q2Amt

Update Query to add Q3Amt

Update Query to add Q4Amt

Page 22: Characteristic Functions

Possible Solution – Temporary Table

Create a Table

Create a table with fields for year, code, Q1Amt, Q2Amt, Q3Amt, and Q4Amt

CREATE TABLE QAmt

(

year char(4),

code char(2),

Q1Amt numeric(9),

Q2Amt numeric(9),

Q3Amt numeric(9),

Q4Amt numeric(9)

);

Page 23: Characteristic Functions

Possible Solution – Temporary Table

DROP a Table

There will be times when you want to get rid of a table you have created. Perhaps you made an error when you created it. Or, you may simply be through using it. To get rid of a table you DROP it from the database.

DROP TABLE QAmt

Page 24: Characteristic Functions

Possible Solution – Temporary Table

INSERT Query

Insert Query to add records and fill in year, code, Q1Amt

INSERT INTO QAmt (year, code, Q1Amt)SELECT year, code, amountFROM fin_dataWHERE quarter = 'Q1'AND code = 'e1'

Page 25: Characteristic Functions

Possible Solution – Temporary Table

Delete Query

There may also be times when you want to keep a table, but get rid of all the records in the table… to empty it. If, for example, you find a problem with your INSERT query, you may want to empty the table before you run the corrected INSERT query. To empty a table you DELETE all the records FROM the table. To empty the QAmt table you:

DELETE FROM QAmt

Page 26: Characteristic Functions

Possible Solution – Temporary Table

After INSERT Query

Check the results:

SELECT *

FROM QAmt

Year code Q1Amt Q2Amt Q3Amt Q4Amt1999 e1 101 (NULL) (NULL) (NULL)2000 e1 153 (NULL) (NULL) (NULL)2001 e1 198 (NULL) (NULL) (NULL)

Page 27: Characteristic Functions

Possible Solution – Temporary Table

Update Query

Year code Q1Amt Q2Amt Q3Amt Q4Amt1999 e1 101 93 (NULL) (NULL)2000 e1 153 149 (NULL) (NULL)2001 e1 198 204 (NULL) (NULL)

Update Query to add Q2Amt

UPDATE QAmt Q

SET Q2Amt =

( SELECT amount

FROM fin_data F

WHERE F.year = Q.year

AND F.code = Q.code

AND F.quarter = 'Q2‘ )

We do two more update queries

to fill in Q3Amt and Q4Amt

Page 28: Characteristic Functions

Possible Solution – Temporary Table

Is this really different At this point you may have noticed that the temporary table approach looks a lot like the subquery approach. In fact, it is virtually the same. It seems conceptually simpler because the temporary table allows us break down the problem into separate and distinct subtasks. We can complete part of the solution, check the results, complete some more, etc. For this reason, you will see it used. It has some legitimate applications, but this isn’t one of them.

This point becomes even more evident if we use either the self-join approach or the subquery approach to populate the table in one step.

Page 29: Characteristic Functions

Possible Solution – Temporary Table

One Step Using Self Join INSERT INTO QAmt

(year, code, Q1Amt, Q2Amt, Q3Amt, Q4Amt)

SELECT F1.year, F1.code,

F1.amount,

F2.amount,

F3.amount,

F4.amount

FROM

fin_data F1, fin_data F2,

fin_data F3, fin_data F4

WHERE F1.year = F2.year

AND F2.year = F3.year

AND F3.year = F4.year

AND F1.code = 'e1'

AND F2.code = 'e1'

AND F3.code = 'e1'

AND F4.code = 'e1'

AND F1.quarter = 'Q1'

AND F2.quarter = 'Q2'

AND F3.quarter = 'Q3'

AND F4.quarter = 'Q4'

Page 30: Characteristic Functions

Possible Solution – Temporary Table

One Step Using SubQuery INSERT INTO QAmt

(year, code, Q1Amt, Q2Amt, Q3Amt, Q4Amt)

SELECT year, code, amount,

(SELECT amount FROM fin_data F WHERE F.year = Q.year AND F.code = Q.code AND F.quarter = 'Q2' ),

(SELECT amount FROM fin_data F WHERE F.year = Q.year AND F.code = Q.code AND F.quarter = 'Q3' ),

(SELECT amount FROM fin_data F WHERE F.year = Q.year AND F.code = Q.code AND F.quarter = 'Q4' )

FROM fin_data QWHERE quarter = 'Q1'AND code = 'e1'

Page 31: Characteristic Functions

Possible Solution – Temporary Table

Problems

We still need a better solution!

So… using a temporary table really isn’t a distinct approach at all. It can make the solution conceptually simpler by allowing us to divide the solution into discrete steps, but it does nothing to reduce overall coding complexity or to improve performance.

In addition, it introduces potential problems with regard to updates made between the time the table is created and the time it is used (currency). If you really do want a snapshot of the data at a particular time then this can be useful. In general, it’s undesirable

You can overcome the currency problem by creating a view instead of a table but, so far, you’re still basically using either a self-join or a subquery method. If you want to create a View you’re better off using Characteristic Functions to do it!

Page 32: Characteristic Functions

Better Solution

Characteristic

Functions

Page 33: Characteristic Functions

Characteristic Function

• <mathematics> The characteristic function of set returns True if its argument is an element of the set and False otherwise.

http://www.cacs.louisiana.edu/~mgr/404/burks/foldoc/98/18.htm