power and simplicity using tables for numbers, dates, and times

Post on 09-Dec-2014

123 Views

Category:

Data & Analytics

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation by Adolfo Socorro given at PRPASS February 27 2014 Meeting

TRANSCRIPT

Power and Simplicity with

Numbers and Dates Tables

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

Agenda

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

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

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

Today: February 27, 2014

Is State Holiday No

Is Federal Holiday No

Calendar Quarter 1

Calendar Year 2014

Fiscal Quarter 3

Fiscal Year 2013

Grouping by Calendar Date Periods

Grouping by Fiscal Year Periods

Grouping by “Program Year”

What’s a Dates Table for?

Provides Precalculated Information

Facilitates Algorithms

EnablesSet-based

Logic

Clients Served by Fiscal Quarter and Year

Dates with Sales

New requirement: show all dates.

Dates with No SalesLEFT

JOIN

Dates Sales

Opened vs. Closed Observations per Day

Opened vs. Closed Observations per Day Dates Opened Closed

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

Sum of Sales per Fiscal Quarter and Year

Creating a Dates Table

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

Precalculate the Day Occurrence

1234

1234

1234

ROW_NUMBER()

Calculate Primary Days

Third Tuesday of March, every four years

1960+4

1964+4

1968+4

Calculate Election Days

First Tuesday of November, every four years

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

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

Ese subquery es un revolú.

¡Métele un GROUP BY!

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

Numbers Table

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

Building the Table

Table 1 Table 2 Result Table1 1 1 1

Building the Table

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

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()

Example: Saving a Checkbox List

Parsing a CSV List of Numbers

1,3,8,22

,1,3,8,22,

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

,22,

Recap

Provide Precalculated Information

Facilitate Algorithms

EnableSet-based

Logic

top related