简体   繁体   English

动态日期表

[英]Dynamic Date Table

I am creating a Data Model in PowerPivot and am wondering if there is anyway I can create a dynamic date table in SQL. 我正在PowerPivot中创建数据模型,并且想知道是否仍然可以在SQL中创建动态日期表。 I was able to create one in PowerQuery however there are some bugs in PowerQuery(read only connection) when a table is modified in PowerPivot. 我可以在PowerQuery中创建一个表,但是在PowerPivot中修改表时,PowerQuery(只读连接)中存在一些错误。 What I am looking for is to have a start date of 1/1/2013 (interval is days) and as each new year rolls around rows are added to the date table. 我正在寻找的是一个开始日期为2013年1月1日(间隔为天),并且随着每个新年滚动,行都将添加到日期表中。 Is there anyway to do this? 反正有这样做吗?

I am running Postgres 我正在运行Postgres

So far I came up with this, 到目前为止,我想到了这个,

SELECT * FROM dbo.fof_GetDates('1/1/2013', GETDATE())

But I want it to display all dates till end of the year. 但我希望它显示到年底为止的所有日期。

The completely dynamic approach would be a query based on generate_series() : 完全动态的方法是基于generate_series()的查询:

SELECT the_date::date
FROM   generate_series('2013-01-01 0:0'::timestamp
                     , date_trunc('year', now()::timestamp)
                                    + interval '1 year - 1 day'
                     , interval '1 day') the_date;
  • Always use ISO 8601 format for dates and timestamps, which works irregardless of locale settings. 始终对日期和时间戳使用ISO 8601格式,无论区域设置如何,都可以使用。

  • A final cast to date ( the_date::date ), because the function returns timestamp (when fed timestamp arguments). 最终转换为datethe_date::date ),因为该函数返回timestamp (当输入timestamp参数时)。

  • The expression 表达方式

     date_trunc('year', now()::timestamp) + interval '1 year - 1 day' 

    calculates the last day of the current year. 计算当年的最后一天。 Alternatively you could use EXTRACT (year FROM now())::text || '-12-31')::date 或者,您可以使用EXTRACT (year FROM now())::text || '-12-31')::date EXTRACT (year FROM now())::text || '-12-31')::date , but that's slower. EXTRACT (year FROM now())::text || '-12-31')::date ,但速度较慢。

You can wrap this into a custom "table-function" (aka set-returning function ) that you can basically use as drop-in replacement for a table name in queries: 您可以将其包装到自定义的“表函数” (又名set-returning函数 )中,该表函数基本上可以用作查询中表名的替代品:

CREATE OR REPLACE FUNCTION f_dates_since_2013()
  RETURNS SETOF date AS
$func$
SELECT the_date::date
FROM   generate_series('2013-01-01 0:0'::timestamp
                     , date_trunc('year', now()::timestamp)
                                    + interval '1 year - 1 day'
                     , interval '1 day') the_date;
$func$  LANGUAGE sql STABLE;

Example: 例:

SELECT * FROM f_dates_since_2013();

Going one step further, you could create a table or - more elegantly - a MATERIALIZED VIEW based on this function (or the underlying query directly): 再进一步,您可以基于此函数(或直接在基础查询中)创建一个表,或者-更优雅地-一个MATERIALIZED VIEW

CREATE MATERIALIZED VIEW my_dates(the_date) AS
SELECT * FROM f_dates_since_2013();

Call: 呼叫:

SELECT * FROM my_dates;

All you have to do now is to schedule a yearly cron job that runs REFRESH MATERIALIZED VIEW at the start of each new year: 您现在要做的就是安排一个年度cron作业 ,该作业在每个新年开始时运行REFRESH MATERIALIZED VIEW

REFRESH MATERIALIZED VIEW my_dates;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM