Thursday, January 14, 2010

Headstart on a Geography Dimension

Use this table to get started with a Geography Dimension. The next step for me will be to add zip codes or a County/Town/Neighborhood breakdown. Different clients will have different needs. You could store this table separate from a further breakdown and then either snowflake them in the Cube's Data Source View or use a named query in the Data Source view to combine them and star them.

ID

Name

ParentID

1

US

1

2

Northeast Region

1

3

Midwest Region

1

4

South Region

1

5

West Region

1

6

New England Div

2

7

Mid-Atlantic Div

2

8

East North Central Div

3

9

West North Central Div

3

10

South Atlantic Div

4

11

East South Central Div

4

12

West South Central Div

4

13

Mountain Div

5

14

Pacific Div

5

15

Maine

6

16

New Hampshire

6

17

Vermont

6

18

Massachusetts

6

19

Rhode Island

6

20

Connecticut

6

21

New York

7

22

New Jersey

7

23

Pennsylvania

7

24

Wisconsin

8

25

Michigan

8

26

Illinois

8

27

Indiana

8

28

Ohio

8

29

Missouri

9

30

North Dakota

9

31

South Dakota

9

32

Nebraska

9

33

Kansas

9

34

Minnesota

9

35

Iowa

9

36

Delaware

10

37

Maryland

10

38

District of Columbia

10

39

Virginia

10

40

West Virginia

10

41

North Carolina

10

42

South Carolina

10

43

Georgia

10

44

Florida

10

45

Kentucky

11

46

Tennessee

11

47

Mississippi

11

48

Alabama

11

49

Oklahoma

12

50

Texas

12

51

Arkansas

12

52

Louisiana

12

53

Idaho

13

54

Montana

13

55

Wyoming

13

56

Nevada

13

57

Utah

13

58

Colorado

13

59

Arizona

13

60

New Mexico

13

61

Alaska

14

62

Washington

14

63

Oregon

14

64

California

14

65

Hawaii

14

SQL Script to Generate Your Time Dimension Table

Do you need a Time Dimension in your warehouse? Use my script (included below) to generate your time dimension database table from scratch. The table allows for a dimension to be built with three or fewer layers - Time of Day (Hour Category), Hour and Minute

For Hour Categories I use: Wee Hours, Morning, Noon Hour, Afternoon, Evening, Night and Midnight Hour. Obviously, clients generally want these changed to fit their exact needs to I have made the editing of those values super obvious and easy.

[Click on this graphic to enlarge the image]

===================================================================
CREATE TABLE #24hours (
HourID int,
HourName char(15),
[24Hour] varchar(2),
[12Hour] varchar(2),
AMPM varchar(2),
HourCategory varchar(15),
JoinID int
)

--this routine could have been done with far fewer lines of code
--had I used a looping structure, however, I wanted people to be
--able to quickly and easily understand what would be entered and
--be able to quickly and easily make modifications to suit their needs
insert into #24hours
(HourID, HourName,[24Hour],[12Hour],AMPM, HourCategory)
select 0, '12 Midnight','00',12,'AM', 'Midnight Hour'
union
select 1, '1:00 AM','01',1,'AM', 'Wee Hours'
union
select 2, '2:00 AM','02',2,'AM', 'Wee Hours'
union
select 3, '3:00 AM', '03',3,'AM', 'Wee Hours'
union
select 4, '4:00 AM', '04',4,'AM', 'Wee Hours'
union
select 5, '5:00 AM', '05',5,'AM', 'Wee Hours'
union
select 6, '6:00 AM', '06',6,'AM', 'Morning'
union
select 7, '7:00 AM', '07',7,'AM', 'Morning'
union
select 8, '8:00 AM', '08',8,'AM', 'Morning'
union
select 9, '9:00 AM', '09',9,'AM', 'Morning'
union
select 10, '10:00 AM', '10',10,'AM', 'Morning'
union
select 11, '11:00 AM', '11',11,'AM', 'Morning'
union
select 12, '12 Noon', '12',12,'PM', 'Noon Hour'
union
select 13, '1:00 PM', '13',1,'PM','Afternoon'
union
select 14, '2:00 PM', '14',2,'PM','Afternoon'
union
select 15, '3:00 PM', '15',3,'PM','Afternoon'
union
select 16, '4:00 PM', '16',4,'PM','Afternoon'
union
select 17, '5:00 PM', '17',5,'PM','Evening'
union
select 18, '6:00 PM', '18',6,'PM','Evening'
union
select 19, '7:00 PM', '19',7,'PM','Evening'
union
select 20, '8:00 PM', '20',8,'PM','Night'
union
select 21, '9:00 PM', '21',9,'PM','Night'
union
select 22, '10:00 PM', '22',10,'PM','Night'
union
select 23, '11:00 PM', '23',11,'PM','Night'


CREATE TABLE #60Minutes (
JoinID int,
MinString varchar(2),
)

declare @counter int
set @counter = 1

while @counter <>
begin
insert into #60Minutes (MinString)
select Right('00' + convert(varchar, @counter),2)
set @counter = @counter + 1
end


Create table dbo.dim_Time
([TimeID] [int] IDENTITY(1,1) NOT NULL,
[24HourInt] int NOT NULL,
[12HourInt] int NOT NULL,
[MinInt] int NOT NULL,
HourName varchar(15) NOT NULL,
AMPM varchar(2) NOT NULL,
[12HourName] varchar(8) NOT NULL,
[24HourName] varchar(5) NOT NULL,
HourCategory varchar(50)
) ON [PRIMARY]


INSERT INTO [dim_Time]
([24HourInt]
,[12HourInt]
,[MinInt]
,[HourName]
,[AMPM]
,[12HourName]
,[24HourName]
,[HourCategory])
(Select
HourID as [24HourInt]
,Convert(varchar, [12Hour]) as [12HourInt]
, Convert(int,MinString) as MinInt
, HourName
, AMPM
,[12HourName] = [12Hour] + ':' + MinString + ' ' + AMPM
,[24HourName] = [24Hour] + ':' + MinString
,HourCategory
from #24hours H
join #60Minutes M
on IsNull(H.JoinID,0) = IsNull(M.JoinID,0))

drop table #24hours
drop table #60Minutes

--needs a key
--needs an index