Monday, 3 August 2020

SQL script for creating and populating the Time Dimension table for a data warehouse

In the previous post, we see the SQL script for creating and populating the Time Dimension table for a data warehouse. In this post, we will create a time dimension table. Basically, we are using this dimension table where we are using the sensor data.

DimDatehttps://bageshkumarbagi-msbi.blogspot.com/2020/08/sql-script-for-creating-and-populating.html

Creating the Time Dimension table using the below script.

 

CREATE TABLE DimTime(

TimeKey int NOT NULL,

Hour24 int NULL,

Hour24ShortString varchar(2) NULL,

Hour24MinString varchar(5) NULL,

Hour24FullString varchar(8) NULL,

Hour12 int NULL,

Hour12ShortString varchar(2) NULL,

Hour12MinString varchar(5) NULL,

Hour12FullString varchar(8) NULL,

AmPmCode int NULL,

AmPmString varchar(2) NOT NULL,

Minute int NULL,

MinuteCode int NULL,

MinuteShortString varchar(2) NULL,

MinuteFullString24 varchar(8) NULL,

MinuteFullString12 varchar(8) NULL,

HalfHour int NULL,

HalfHourCode int NULL,

HalfHourShortString varchar(2) NULL,

HalfHourFullString24 varchar(8) NULL,

HalfHourFullString12 varchar(8) NULL,

Second int NULL,

SecondShortString varchar(2) NULL,

FullTimeString24 varchar(8) NULL,

FullTimeString12 varchar(8) NULL,

FullTime time(7) NULL,

DayTimeSlot varchar(100) NULL,

DayTimeSlotOrderKey int NULL,

CONSTRAINT PK_DimTime PRIMARY KEY CLUSTERED

(

TimeKey ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

 Script to populate this table.

declare @hour int

declare @minute int

declare @second int

declare @TimeKey int

set @hour=0

set @TimeKey=0

while @hour<24

begin

set @minute=0

while @minute<60

begin

set @second=0

while @second<60

begin

 

set @TimeKey=(@hour*10000) + (@minute*100) + @second;

 

INSERT INTO dbo.DimTime

(TimeKey

,Hour24

,Hour24ShortString

,Hour24MinString

,Hour24FullString

,Hour12

,Hour12ShortString

,Hour12MinString

,Hour12FullString

,AmPmCode

,AmPmString

,Minute

,MinuteCode

,MinuteShortString

,MinuteFullString24

,MinuteFullString12

,HalfHour

,HalfHourCode

,HalfHourShortString

,HalfHourFullString24

,HalfHourFullString12

,Second

,SecondShortString

,FullTimeString24

,FullTimeString12

,FullTime

,DayTimeSlot

,DayTimeSlotOrderKey)

 

select

@TimeKey as TimeKey,

@hour as Hour24,

right('0'+convert(varchar(2),@hour),2) Hour24ShortString,

right('0'+convert(varchar(2),@hour),2)+':00' Hour24MinString,

right('0'+convert(varchar(2),@hour),2)+':00:00' Hour24FullString,

@hour%12 as Hour12,

right('0'+convert(varchar(2),@hour%12),2) Hour12ShortString,

right('0'+convert(varchar(2),@hour%12),2)+':00' Hour12MinString,

right('0'+convert(varchar(2),@hour%12),2)+':00:00' Hour12FullString,

@hour/12 as AmPmCode,

case when @hour<12 then 'AM' else 'PM' end as AmPmString,

@minute as Minute,

(@hour*100) + (@minute) MinuteCode,

right('0'+convert(varchar(2),@minute),2) MinuteShortString,

right('0'+convert(varchar(2),@hour),2)+':'+

right('0'+convert(varchar(2),@minute),2)+':00' MinuteFullString24,

right('0'+convert(varchar(2),@hour%12),2)+':'+

right('0'+convert(varchar(2),@minute),2)+':00' MinuteFullString12,

@minute/30 as HalfHour,

(@hour*100) + ((@minute/30)*30) HalfHourCode,

right('0'+convert(varchar(2),((@minute/30)*30)),2) HalfHourShortString,

right('0'+convert(varchar(2),@hour),2)+':'+

right('0'+convert(varchar(2),((@minute/30)*30)),2)+':00' HalfHourFullString24,

right('0'+convert(varchar(2),@hour%12),2)+':'+

right('0'+convert(varchar(2),((@minute/30)*30)),2)+':00' HalfHourFullString12,

@second as Second,

right('0'+convert(varchar(2),@second),2) SecondShortString,

right('0'+convert(varchar(2),@hour),2)+':'+

right('0'+convert(varchar(2),@minute),2)+':'+

right('0'+convert(varchar(2),@second),2) FullTimeString24,

right('0'+convert(varchar(2),@hour%12),2)+':'+

right('0'+convert(varchar(2),@minute),2)+':'+

right('0'+convert(varchar(2),@second),2) FullTimeString12,

convert(time,right('0'+convert(varchar(2),@hour),2)+':'+

right('0'+convert(varchar(2),@minute),2)+':'+

right('0'+convert(varchar(2),@second),2)) as FullTime,

CASE

WHEN ((@TimeKey) >= 00000 AND (@TimeKey) <= 25959)

THEN 'Late Night (00:00 AM To 02:59 AM)'

WHEN ((@TimeKey)  >= 30000 AND (@TimeKey)  <= 65959)

THEN 'Early Morning(03:00 AM To 6:59 AM)'

WHEN ((@TimeKey)  >= 70000 AND (@TimeKey)  <= 85959)

THEN 'AM Peak (7:00 AM To 8:59 AM)'

WHEN ((@TimeKey)  >= 90000 AND (@TimeKey)  <= 115959)

THEN 'Mid Morning (9:00 AM To 11:59 AM)'

WHEN ((@TimeKey)  >= 120000 AND (@TimeKey)  <= 135959)

THEN 'Lunch (12:00 PM To 13:59 PM)'

WHEN ((@TimeKey)  >= 140000 AND (@TimeKey)  <= 155959)

THEN 'Mid Afternoon (14:00 PM To 15:59 PM)'

WHEN ((@TimeKey)  >= 50000 AND (@TimeKey)  <= 175959)

THEN 'PM Peak (16:00 PM To 17:59 PM)'

WHEN ((@TimeKey)  >= 180000 AND (@TimeKey)  <= 235959)

THEN 'Evening (18:00 PM To 23:59 PM)'

WHEN ((@TimeKey)  >= 240000) THEN 'Previous Day Late Night

(24:00 PM to '+cast(  @hour as varchar(10)) +':00 PM )'

END   as DayTimeSlot,

CASE

WHEN ((@TimeKey) >= 00000 AND (@TimeKey) <= 25959)

THEN 1

WHEN ((@TimeKey)  >= 30000 AND (@TimeKey)  <= 65959)

THEN 2

WHEN ((@TimeKey)  >= 70000 AND (@TimeKey)  <= 85959)

THEN 3

WHEN ((@TimeKey)  >= 90000 AND (@TimeKey)  <= 115959)

THEN 4

WHEN ((@TimeKey)  >= 120000 AND (@TimeKey)  <= 135959)

THEN 5

WHEN ((@TimeKey)  >= 140000 AND (@TimeKey)  <= 155959)

THEN 6

WHEN ((@TimeKey)  >= 50000 AND (@TimeKey)  <= 175959)

THEN 7

WHEN ((@TimeKey)  >= 180000 AND (@TimeKey)  <= 235959)

THEN 8

WHEN ((@TimeKey)  >= 240000) THEN 9

END   as DayTimeSlotOrderKey

set @second=@second+1

end

set @minute=@minute+1

end

set @hour=@hour+1

end

 Running this script.

                 

Records inserted successfully.

Now see the records into the table

   


14 comments:

  1. Business and economics are a very broad field in applied economic theory that uses both economic theory and mathematical models to study the dynamics of company enterprises, social relationships among firms, capital markets, and labor markets, and the relationship between firms and individual consumers. You can find more details on business guide on the site picturethisink.com.

    ReplyDelete
  2. Business and economics are a very broad field in applied economic theory that uses both economic theory and mathematical models to study the dynamics of company enterprises, social relationships among firms, capital markets, and labor markets, and the relationship between firms and individual consumers. You can find more details on business guide on the site picturethisink.com.

    ReplyDelete
  3. What an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.

    360DigiTMG

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Dimension Control Service

    ReplyDelete
  5. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Dimension Control Service

    ReplyDelete

  6. incredible article!! sharing these kind of articles is the decent one and I trust you will share an article on information science.By giving an organization like 360DigiTMG.it is one the best foundation for doing guaranteed courses

    hrdf claimable training

    ReplyDelete
  7. I feel extremely glad to have seen your site page and anticipate such a large number of additionally engaging occasions perusing here. Much obliged again for all the subtleties.
    360DigiTG supply chain analytics beginner's guide

    ReplyDelete
  8. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    data science malaysia

    ReplyDelete
  9. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing on Msbi online training
    Msbi online training Hyderabad
    Msbi online training India
    Msbi online course
    Msbi course
    Msbi training
    Msbi certification training

    ReplyDelete
  10. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    difference between analysis and analytics

    ReplyDelete
  11. it was so good to read and very useful to update my skill as updated one.
    Dimension Control Services in Houston

    ReplyDelete
  12. This website is remarkable information and facts it's really excellent
    cyber security course in malaysia

    ReplyDelete
  13. Thanks for sharing. For more information on human resource matters in Malaysia, you can refer to hrdf claimable. These are the exercises provided that employers and employees need to know.

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts