33
Power and Simplicity with Numbers and Dates Tables Adolfo J. Socorro, Ph.D. @AdolfoSocorro eSolutions, Inc.

Power and Simplicity using Tables for Numbers, Dates, and Times

Embed Size (px)

DESCRIPTION

Presentation by Adolfo Socorro given at PRPASS February 27 2014 Meeting

Citation preview

Page 1: Power and Simplicity using Tables for Numbers, Dates, and Times

Power and Simplicity with

Numbers and Dates Tables

Adolfo J. Socorro, Ph.D.@AdolfoSocorroeSolutions, Inc.

Page 2: Power and Simplicity using Tables for Numbers, Dates, and Times

Agenda

• What are dates and numbers tables?• Examples of queries with dates and numbers tables• How to build dates and numbers tables

Page 3: Power and Simplicity using Tables for Numbers, Dates, and Times

Today: February 27, 2014

Raw Date (Date Key) 20140227

MM/DD/YYYY 02/27/2014

DD/MM/YYYY 27/02/2014

Day Name Thursday

Day Number 27

Day Number of Week 5

Day Number of Year 58

Page 4: Power and Simplicity using Tables for Numbers, Dates, and Times

Today: February 27, 2014

Month Name February

Month Number of Year 2

Is Last Day of Month No

Is First Day of Quarter No

Is Last Day of Quarter No

Page 5: Power and Simplicity using Tables for Numbers, Dates, and Times

Today: February 27, 2014

Is State Holiday No

Is Federal Holiday No

Calendar Quarter 1

Calendar Year 2014

Fiscal Quarter 3

Fiscal Year 2013

Page 6: Power and Simplicity using Tables for Numbers, Dates, and Times

Grouping by Calendar Date Periods

Page 7: Power and Simplicity using Tables for Numbers, Dates, and Times

Grouping by Fiscal Year Periods

Page 8: Power and Simplicity using Tables for Numbers, Dates, and Times

Grouping by “Program Year”

Page 9: Power and Simplicity using Tables for Numbers, Dates, and Times

What’s a Dates Table for?

Provides Precalculated Information

Facilitates Algorithms

EnablesSet-based

Logic

Page 10: Power and Simplicity using Tables for Numbers, Dates, and Times

Clients Served by Fiscal Quarter and Year

Page 11: Power and Simplicity using Tables for Numbers, Dates, and Times

Dates with Sales

New requirement: show all dates.

Page 12: Power and Simplicity using Tables for Numbers, Dates, and Times

Dates with No SalesLEFT

JOIN

Dates Sales

Page 13: Power and Simplicity using Tables for Numbers, Dates, and Times

Opened vs. Closed Observations per Day

Page 14: Power and Simplicity using Tables for Numbers, Dates, and Times

Opened vs. Closed Observations per Day Dates Opened Closed

Page 15: Power and Simplicity using Tables for Numbers, Dates, and Times

PR PASS Meetings for 2014

January•30 (1)•23 (2)•…

February•27 (1)•20 (2)•…

1. Get all the Thursdays in 2014 that are not holidays2. Partition them by month3. Order descending4. Take the first one

Page 16: Power and Simplicity using Tables for Numbers, Dates, and Times

Sum of Sales per Fiscal Quarter and Year

Page 17: Power and Simplicity using Tables for Numbers, Dates, and Times

Creating a Dates Table

Page 18: Power and Simplicity using Tables for Numbers, Dates, and Times

Set the Holidays

New Year’s Day•January 1

Independence Day•July 4

MLK, Jr.•3rd Monday in January (since 1983)

Thanksgiving•4th Thursday in November

Page 19: Power and Simplicity using Tables for Numbers, Dates, and Times

Precalculate the Day Occurrence

1234

1234

1234

ROW_NUMBER()

Page 20: Power and Simplicity using Tables for Numbers, Dates, and Times

Calculate Primary Days

Third Tuesday of March, every four years

1960+4

1964+4

1968+4

Page 21: Power and Simplicity using Tables for Numbers, Dates, and Times

Calculate Election Days

First Tuesday of November, every four years

But it’s the first Tuesday AFTER the first Sunday!

Page 22: Power and Simplicity using Tables for Numbers, Dates, and Times

Wow, yo creo que he estado usando 2005.¿De dónde salió esa data?

Ese subquery es un revolú.

Page 23: Power and Simplicity using Tables for Numbers, Dates, and Times

¡Métele un GROUP BY!

Page 24: Power and Simplicity using Tables for Numbers, Dates, and Times

De mala fé, la oposición borró una cláusula del WHERE.

Page 25: Power and Simplicity using Tables for Numbers, Dates, and Times

Numbers Table

Page 26: Power and Simplicity using Tables for Numbers, Dates, and Times

1 11 21 31 41 51 61 71 81 912 12 22 32 42 52 62 72 82 923 13 23 33 43 53 63 73 83 934 14 24 34 44 54 64 74 84 945 15 25 35 45 55 65 75 85 956 16 26 36 46 56 66 76 86 967 17 27 37 47 57 67 77 87 978 18 28 38 48 58 68 78 88 989 19 29 39 49 59 69 79 89 99

10 20 30 40 50 60 70 80 90 100

Page 27: Power and Simplicity using Tables for Numbers, Dates, and Times

Building the Table

Table 1 Table 2 Result Table1 1 1 1

Page 28: Power and Simplicity using Tables for Numbers, Dates, and Times

Building the Table

Table 1 Table 2 Result Table1 1 11 1 1 1 1

Page 29: Power and Simplicity using Tables for Numbers, Dates, and Times

Building the TableTable 1 Table 2 Result Table

1 1 11 1 11 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1

1234567891011…16

ROW_NUMBER()

Page 30: Power and Simplicity using Tables for Numbers, Dates, and Times

Example: Saving a Checkbox List

Page 31: Power and Simplicity using Tables for Numbers, Dates, and Times

Parsing a CSV List of Numbers

1,3,8,22

,1,3,8,22,

,1,,2,,3,,4,

,22,

Page 32: Power and Simplicity using Tables for Numbers, Dates, and Times

Recap

Provide Precalculated Information

Facilitate Algorithms

EnableSet-based

Logic

Page 33: Power and Simplicity using Tables for Numbers, Dates, and Times