我需要使用定义的开始日期和结束日期之间的所有日期填充行集.如果我的开始日期是2017年7月19日,结束日期是2017年7月21日,则行集应包含19/7/2017,20/7/2017和21/7/2017.
我想知道是否有一种使用U-SQL的简单方法
最简单的方法是从您喜欢的仓库中导出您最喜欢的日期维度并将其导入到U-SQL表中.
您也可以使用自定义U-SQL代码执行此操作,如下所示:
DECLARE @outputFilepath string = "output/output74.csv";
//DECLARE @startDate DateTime = DateTime.Parse("19/7/2017");
//DECLARE @endDate DateTime = DateTime.Parse("21/7/2017");
DECLARE @startDate DateTime = DateTime.Parse("1/1/2000");
DECLARE @endDate DateTime = DateTime.Parse("31/12/2017");
// User-defined appliers
// Take one row and produce 0 to n rows
// Used with OUTER/CROSS APPLY
@output =
SELECT outputDate
FROM(
VALUES ( 1 )
) AS dummy(x)
CROSS APPLY new USQLtpch.makeDateRange (@startDate, @endDate) AS properties(outputDate DateTime);
OUTPUT @output
TO @outputFilepath
USING Outputters.Tsv();
Run Code Online (Sandbox Code Playgroud)
代码隐藏文件:
using Microsoft.Analytics.Interfaces;
using Microsoft.Analytics.Types.Sql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace USQLtpch
{
[SqlUserDefinedApplier]
public class makeDateRange : IApplier
{
private DateTime startDate;
private DateTime endDate;
public makeDateRange(DateTime startDate, DateTime endDate)
{
this.startDate = startDate;
this.endDate = endDate;
}
public override IEnumerable<IRow> Apply(IRow input, IUpdatableRow output)
{
// Initialise
DateTime outputDate = this.startDate;
// Loop until date range has been filled out
while (outputDate <= endDate)
{
output.Set<DateTime>("outputDate", outputDate);
// Increment date
outputDate = outputDate.AddDays(1);
yield return output.AsReadOnly();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用自定义Applier完成了这个操作,它需要1行并将其转换为0或n.