简体   繁体   English

如何在SQL Server中填充日期维度表,以便获得具有此特定值的列?

[英]How do I populate my Date Dimension Table in SQL Server so as to get a column with this specific value?

I have the following query that runs in SQL Server 2014 to populate a table called DateDimension. 我在SQL Server 2014中运行以下查询,以填充名为DateDimension的表。

DECLARE @startdate datetime
   DECLARE @enddate datetime
SET @startdate = '01/01/2000'
   SET @enddate = '12/31/2018'
DECLARE @loopdate datetime
   SET @loopdate = @startdate
WHILE @loopdate <= @enddate
   BEGIN
INSERT INTO DateDimension VALUES (
   Day(@loopdate),
   Month(@loopdate), 
   Year(@loopdate), 
   CASE WHEN Month(@loopdate) IN (1, 2, 3) THEN 3 -- Since Financial Year starts in July, January to March is considered as Quarter 3
   WHEN Month(@loopdate) IN (4, 5, 6) THEN 4
   WHEN Month(@loopdate) IN (7, 8, 9) THEN 1
   WHEN Month(@loopdate) IN (10, 11, 12) THEN 2
   END,
   @loopdate,
   CONVERT(VARCHAR(10),@loopdate,111) 
   ) 

   SET @loopdate = DateAdd(d, 1, @loopdate)
   END

Extract of current table, after running the above query is as follows: 运行上述查询后,当前表的提取如下:

id  Day Month   Year quarter    date        datevarchar
1   1   1       2000    3    2000-01-01       1/1/2000
2   2   1       2000    3    2000-01-02       1/2/2000
3   3   1       2000    3    2000-01-03       1/3/2000
4   4   1       2000    3    2000-01-04       1/4/2000
5   5   1       2000    3    2000-01-05       1/5/2000

I need the "quarter" column to show its values as below: 我需要“季度”列来显示其值,如下所示:

   quarter
  Qr 3 2000

It would be even nicer if the "quarter" column be left as-is and a new column added to show what I'm after. 如果将“ quarter”列保持原样,并添加一个新列以显示我要执行的操作,那就更好了。 How can I do this? 我怎样才能做到这一点?

You will need to add the column on to the DateDimension table first before running the query. 在运行查询之前,您需要先将该列添加到DateDimension表上。 It will be a varchar(9) column. 这将是varchar(9)列。

DECLARE @startdate datetime
   DECLARE @enddate datetime
SET @startdate = '01/01/2000'
   SET @enddate = '12/31/2018'
DECLARE @loopdate datetime
   SET @loopdate = @startdate
WHILE @loopdate <= @enddate
   BEGIN
INSERT INTO DateDimension VALUES (
   Day(@loopdate),
   Month(@loopdate), 
   Year(@loopdate), 
   CASE WHEN Month(@loopdate) IN (1, 2, 3) THEN 3 -- Since Financial Year starts in July, January to March is considered as Quarter 3
   WHEN Month(@loopdate) IN (4, 5, 6) THEN 4
   WHEN Month(@loopdate) IN (7, 8, 9) THEN 1
   WHEN Month(@loopdate) IN (10, 11, 12) THEN 2
   END,
   @loopdate,
   CONVERT(VARCHAR(10),@loopdate,111),
   CASE WHEN Month(@loopdate) IN (1, 2, 3) THEN 'Qr 3 ' + CONVERT(VARCHAR(4),Year(@loopdate))
   WHEN Month(@loopdate) IN (4, 5, 6) THEN 'Qr 4 ' + CONVERT(VARCHAR(4),Year(@loopdate))
   WHEN Month(@loopdate) IN (7, 8, 9) THEN 'Qr 1 ' + CONVERT(VARCHAR(4),Year(@loopdate))
   WHEN Month(@loopdate) IN (10, 11, 12) THEN 'Qr 2 ' + CONVERT(VARCHAR(4),Year(@loopdate))
   END
   ) 

   SET @loopdate = DateAdd(d, 1, @loopdate)
   END

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL SERVER SSAS:如何处理我的事实表中的NULL日期值,这样我可以处理我的时间维度而不会出错? - SQL SERVER SSAS: How do I handle a NULL date value in my fact table, so I can process my time dimension without errors? SQL Server:填充(分钟)日期维表 - SQL Server: Populate (Minute) Date Dimension table 如何根据此特定逻辑在我的日期维度表中创建周组列? - How can I create a week group column in my Date Dimension Table based on this specific logic? 如何在我的“日期维度”表中创建一列,以将“日期”列分类为该特定逻辑? - How to create a column in my Date Dimension table that will classify the Date column into this specific logic? 如何获得对 SQL Server 表列的约束 - How do I get constraints on a SQL Server table column 如何从我的表中选择一列,并根据 SQL Server 中该列中的值获得第 n 行? - How can I select a column from my table, and get very nth row based on the value in that column in SQL Server? 从表列的SQL Server日期值获取月份名称 - Get month name from SQL Server date value in table column 从 SQL 服务器中的另一个表中获取最近的日期列值 - Get nearest date column value from another table in SQL Server 如何基于SQL Server中其他表上的时间戳记条目在表中获取最新的用户udpated列值? - How do i get the latest user udpated column value in a table based on timestamp entry on a different table in SQL Server? SQL Server如何更改视图,以便在插入表时不会产生重复? - SQL Server How do I change my view so it doesn't produces duplicates when inserting into a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM