Create OLAP Datetime table dynamically using SQL Views

自古美人都是妖i 提交于 2020-02-07 07:46:31

问题


As I want to use the dynamic datetime values to pass as dimension in my OLAP Cube. Since, There is specific FY format for us. So, I decided to go with sql views. I have partly done writing my sql views. I need to have all the columns as when DateTime table created using Cube wizard. ie, PK_Date, Date_Name, Fiscal_Year, Fiscal_Year_Name, Fiscal_Half_Year, Fiscal_Half_Year_Name etc....

But, Only change with generating DateTime table from cube wizard is, in our approach we are going to use view table as source table for DateTime dimension.

As far now I have done the below code

ALTER VIEW [dbo].[FYExtraction] AS WITH E1(N) AS
(SELECT 1
 FROM (
     VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) DT(N)),
       E2(N) AS
(SELECT 1
 FROM E1 A, E1 B),
       E4(N) AS
(SELECT 1
 FROM E2 A, E2 B),
       Numbers(N) AS
( SELECT ROW_NUMBER() OVER (
                          ORDER BY
                            (SELECT NULL)) - 1
 FROM E4 )
SELECT temp.PK_Date,
   CASE
       WHEN (temp.PK_Date >= cast(cast(year(temp.PK_Date) AS varchar(10))+'-'+
                                    (SELECT Line2
                                     FROM SiteFile
                                     WHERE code='ACQFY') AS datetime)
             AND temp.PK_Date <= cast(cast(year(temp.PK_Date)+1 AS varchar(10))+'-'+
                                        (SELECT Line3
                                         FROM SiteFile
                                         WHERE code='ACQFY') AS datetime)) THEN 'FY '+ cast(year(temp.PK_Date) AS varchar(100))
       ELSE 'FY '+cast(year(temp.PK_Date) -1 AS varchar(100))
   END AS Fiscal_Year_Name
FROM
(SELECT N,
      DATEADD(D, N,
                (SELECT cast(
                               (SELECT top 1 cast(year(CreatedOn)-1 AS varchar(10))
                                FROM items
                                ORDER BY CreatedOn ASC)+'-'+Line2 AS datetime)
                 FROM sitefile
                 WHERE code='ACQFY')) AS PK_Date
 FROM Numbers
 WHERE N <= DATEDIFF(D,
                     (SELECT cast(
                                    (SELECT top 1 cast(year(CreatedOn)-1 AS varchar(10))
                                     FROM items
                                     ORDER BY CreatedOn ASC)+'-'+Line2 AS datetime)
                      FROM sitefile
                      WHERE code='ACQFY'),
                     (SELECT cast(
                                    (SELECT top 1 cast(year(CreatedOn)+1 AS varchar(10))
                                     FROM items
                                     ORDER BY CreatedOn DESC)+'-'+Line3 AS datetime)
                      FROM sitefile
                      WHERE code='ACQFY')))AS TEMP GO

来源:https://stackoverflow.com/questions/28558905/create-olap-datetime-table-dynamically-using-sql-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!