简体   繁体   English

总结Redshift UDF内部

[英]Summing inside Redshift UDF

I have a dates tables, columns: 我有一个日期表,列:

  1. date: the date eg '2018-06-29' 日期:日期,例如“ 2018-06-29”
  2. business_day: 1 or 0 to indicate if date is business day or not business_day:1或0,指示日期是否为营业日

I want to create a User-Defined function in Redshift that outputs between two dates how many business days. 我想在Redshift中创建一个用户定义的函数,该函数在两个日期之间输出多少个工作日。 In a select statement is will look like : 在select语句中将如下所示:

select business_days(start_date timestamp, end_date timestamp) as integer

for example: 例如:

2018-06-29 is a friday business day
2018-06-30 saturday non business day
2018-07-01 sunday non business day
2018-07-02 business day

select business_days( '2018-06-29', '2018-07-02') should output 2 select business_days( '2018-06-29', '2018-07-02')应该输出2

Redshift does not allow aggregates inside a function and I am looking for a workaround. Redshift不允许在函数内部进行聚合,我正在寻找一种解决方法。

Amazon Redshift User-Defined Functions cannot access the network, nor can they access data stored in tables. Amazon Redshift用户定义函数无法访问网络,也无法访问表中存储的数据。

As an alternative approach, you could create a Python function that can calculate the difference between two dates. 作为一种替代方法,您可以创建一个Python函数 ,该函数可以计算两个日期之间的差。

If you merely wish to skip weekends, you could Count number of days between dates, ignoring weekends . 如果您只想跳过周末,则可以计算日期之间的天数,而忽略周末

If you have more complex logic, such as skipping holidays that vary by year, you will need to supply that information to the Python function, such as including the special dates within the function itself (eg in a list of holiday days). 如果您有更复杂的逻辑,例如跳过每年不同的假期,则需要将该信息提供给Python函数,例如在函数本身中包括特殊日期(例如,在假期列表中)。

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

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