【问题标题】:write a query to fill the above table from 1 Jan, 2008 to 31 Dec 2014从 2008 年 1 月 1 日到 2014 年 12 月 31 日,写一个查询来填写上表
【发布时间】:2013-06-20 05:11:56
【问题描述】:

创建一个包含以下列的表格

DateId, Date, Day_Name, Day_of_Week, Day_of_Month, Day_of_Year,
Mon… _Name, Quarter_of_Year, Year_Name,Year

从 2008 年 1 月 1 日到 2014 年 12 月 31 日编写查询以填充上表

DECLARE @DATE smalldatetime
DECLARE @DATESTART smalldatetime
DECLARE @DATEEND smalldatetime

SET @DATESTART = '01-01-2008'
SET @DATEEND = '12-31-2014'

--SET @DATE = @DATESTART

DECLARE @days int 
DECLARE @index int = 1

SET @days= DATEPART(dayofyear, @DATEEND) - DATEPART(dayofyear, @DATESTART)

SELECT @days
WHILE()
INSERT INTO [BI_Database].[dbo].[Date_
Range]
([Date],[Day_Name],[Day_of_Week],[Day_of…
VALUES(<Date, datetime,>,<Day_Name, nvarchar(50),>,<Day_of_Week, smallint,>,<Day_of_Month, smallint,>,<Day_of_Year, smallint,>,<Month_Name, nvarchar(50),>
,<Month_of_Year, smallint,>,<Quarter_Name, nvarchar(50),>,<Quarter_of_Year, nvarchar(50),>,<Year_Name, nvarchar(50),>,<Year, smallint,>)

【问题讨论】:

  • 你可以使用while循环...
  • 为什么要在日期中已经包含日期的所有信息?还有什么是年份名称?
  • @t-clausen.dk - “龙年”? “鼠年”?在没有看到表格定义的情况下,我同意这很棘手,但不难理解某些文化会命名它们的年份。
  • @t-clausen.dk 这是一个“维度表”,通常用于数据仓库等。它的优势在于仅动态计算相关值,当它们在表中实现时,然后它们可以被索引(也是 FK 的目标),并用于索引查找。

标签: sql-server sql-server-2008 tsql sql-server-2005 sql-server-2000


【解决方案1】:

应该这样做:

DECLARE @DATESTART smalldatetime
DECLARE @DATEEND smalldatetime

SET @DATESTART = '01-01-2008'
SET @DATEEND = '12-31-2014'

;WITH cte4 As (Select 0 As N Union All Select 1 Union All Select 2 Union All Select 4)
, cte64 As (Select 0 As N From cte4 n0, cte4 n1, cte4 n2)
, cte4k As (Select ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) As N From cte64 n0, cte64 n1)
, cteDate As (Select N, CAST(@DATESTART+N-1 As smalldatetime) As date_ From cte4k)
INSERT INTO [BI_Database].[dbo].[Date_Range]
([Date],Day_Name,Day_of_Week,Day_of_Week,Day_of_Month,Day_of_Year,Month_Name,Month_of_Year,
 Quarter_Name,Quarter_of_Year,Year_Name,[Year])
SELECT
    N                           As DateId,
    date_                       As [Date],
    DATENAME(WEEKDAY, date_)    As Day_Name,
    DATEPART(WEEKDAY, date_)    As Day_of_Week,
    Day(date_)                  As Day_of_Month,
    DATEPART(DAYOFYEAR, date_)  As Day_of_Year,
    DATENAME(MONTH, date_)      As Month_Name,
    MONTH(date_)                As Month_of_Year,
    DATENAME(QUARTER, date_)    As Quarter_Name,
    DATEPART(QUARTER, date_)    As Quarter_of_Year,
    DATENAME(YEAR, date_)       As Year_Name,
    YEAR(date_)                 As [Year]
FROM cteDate
WHERE date_ BETWEEN @DATESTART AND @DATEEND

注意:仅适用于 SQL Server 2005+,

【讨论】:

    【解决方案2】:

    试试这个,不幸的是我不知道用什么填充 year_name。我用的是临时表,这个可以换成你自己的表

    create table #t
    (DateId int identity(1,1), Date date, Day_Name varchar(8), 
    Day_of_Week smallint, Day_of_Month smallint, Day_of_Year int,
    Mon_Name varchar(12), Quarter_of_Year smallint, Year_Name varchar(30),
    Year int)
    
    insert #t
    (Date,Day_Name,Day_of_Week,Day_of_Month,Day_of_Year,Mon_Name, 
    Quarter_of_Year, Year_Name, Year)
    select Date1 Date
    ,datename(weekday, date1) Day_Name
    ,datediff(day, 0, date1) %7 + 1 Day_of_Week
    ,day(date1) Day_of_Month
    ,datepart(dy, date1) Day_of_Year
    ,datepart(mm, date1) Mon_Name
    ,datepart(qq, date1) Quarter,
    null Year_Name, -- don't know what you want here
    year(date1) year 
    from 
    master..spt_values v1
    cross join
    (select 0 mul union all select 1 union all select 2) m
    cross apply
    (select dateadd(day, v1.number+ 2048 * mul,'2008-01-01') date1) a
    where date1 <= '2014-12-31'
    and type = 'P'
    
    select * from #t
    drop table #t
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多