I am trying to load time dimension in a specific order and have attached (below) having sample data showing actual and desired output
desired output
BIZ_YEAR | BIZ_MONTH | BIZ_MONTH_VAL |
2000 | 1 | Jan |
2000 | 2 | feb |
2000 | 3 | mar |
2000 | 4 | Apr |
2000 | 5 | may |
2000 | 6 | Jun |
2000 | 7 | jul |
2000 | 8 | aug |
2000 | 9 | sep |
2000 | 10 | oct |
2000 | 11 | nov |
2000 | 12 | dec |
2001 | 1 | Jan |
2001 | 2 | feb |
2001 | 3 | mar |
2001 | 4 | Apr |
2001 | 5 | may |
2001 | 6 | Jun |
2001 | 7 | jul |
2001 | 8 | aug |
2001 | 9 | sep |
2001 | 10 | oct |
2001 | 11 | nov |
2001 | 12 | dec |
actual output
---
BIZ_YEAR | BIZ_MONTH | BIZ_MONTH_VAL |
2000 | 1 | Jan |
2000 | 10 | Oct |
2000 | 11 | Nov |
2000 | 12 | Dec |
2000 | 2 | Feb |
2000 | 3 | Mar |
2000 | 4 | Apr |
2000 | 5 | May |
2000 | 6 | Jun |
2000 | 7 | Jul |
2000 | 8 | Aug |
2000 | 9 | Sep |
2001 | 1 | Jan |
2001 | 10 | Oct |
2001 | 11 | Nov |
2001 | 12 | Dec |
2001 | 2 | Feb |
2001 | 3 | Mar |
2001 | 4 | Apr |
2001 | 5 | May |
2001 | 6 | Jun |
2001 | 7 | Jul |
2001 | 8 | Aug |
2001 | 9 | Sep |
Here is my script which will load data in a temp table before it goes to target time dim however I am unable to insert data in a particular order( refer sample data for reference)
USE TEST--Query to populate time dimension
Declare
@StartDate datetime,
@EndDate datetime,
@Date datetime
Set @StartDate = '2000/01/01'
Set @EndDate = '2020/12/31'
Set @Date = @StartDate
--create table Test1
--(
-- BIZ_YEAR varchar(100),
-- BIZ_MONTH varchar(100),
-- BIZ_MONTH_VAL varchar(100),
-- LOAD_DT varchar(100),
-- UPDATE_DT varchar(100),
-- START_DT varchar(100),
-- END_DT varchar(100)
-- )
select * from Test1
-- Loop through dates
WHILE @Date <=@EndDate
BEGIN
-- Insert record in dimension table
INSERT Into Test1
( BIZ_YEAR,
BIZ_MONTH,
BIZ_MONTH_VAL,
LOAD_DT,
UPDATE_DT,
START_DT,
END_DT
)
Values
(cast(convert(char(8), Year(@Date),112) as int),
cast(convert(char(8),DATEPART(mm, @Date),112) as int),
cast (convert(Char(3),DATENAME(MONTH, @Date)) AS CHAR(3)),
GETDATE(),
GETDATE(),
(GETDATE()-1),
null
)
-- Goto next day
Set @Date = @Date + 1
END
GO
----
use TEST
create table temp1
(
BIZ_YEAR varchar(100),
BIZ_MONTH int,
BIZ_MONTH_VAL varchar(100),
LOAD_DT varchar(100),
UPDATE_DT varchar(100),
START_DT varchar(100),
END_DT varchar(100)
)
---insert in temp table the data only distinct values which would give 252 records for the period
-- @StartDate = '2000/01/01'
-- @EndDate = '2020/12/31'
INSERT INTO temp1
(BIZ_YEAR,
BIZ_MONTH,
BIZ_MONTH_VAL
)
(select distinct BIZ_YEAR,
BIZ_MONTH,
BIZ_MONTH_VAL
from Test1
)
From this Test1 table I thought of pulling data in a specific order but it seems i cant use order by.
So looking fwd for some advice on how time dimension data should look like
Mudassar