我试图在Tableau中使用以下内容声明日期变量,稍后将其在SQL代码中称为它们。
但是我无法在Tableau中执行相同的操作。您能帮助/指导我正确使用这些功能
-- Declare the parameter dates and get the start date and date for all queries.
Declare @ParamDate as date = '2018-12-31';
Declare @Trailing90Days as date = DATEADD(DAY,-90,GETDATE());
Declare @MonthStartDate as date = DATEADD(DAY, 1, EOMONTH(@ParamDate, -1));
Declare @MonthEndDate as date = EOMONTH(@ParamDate);
Declare @YearStartDate as date = DATEADD(yy, DATEDIFF(yy, 0, @ParamDate), 0);
Declare @YearEndDate as date = DATEADD (dd, -1, DATEADD(yy, DATEDIFF(yy, 0, @ParamDate) +1, 0));
Declare @YoYMonthStartDate as date = DATEADD(YEAR, -1, @MonthStartDate);
Declare @YoYMonthEndDate as date = DATEADD(YEAR, -1, @MonthEndDate);
Declare @MoMMonthStartDate as date = DATEADD(MONTH, -1, @MonthStartDate);
Declare @MoMMonthEndDate as date = DATEADD(MONTH, -1, @MonthEndDate);
Declare @QoQMonthStartDate as date = DATEADD(Quarter, -1, @MonthStartDate);
Declare @QoQMonthEndDate as date = DATEADD(Quarter, -1, @MonthEndDate);
select
@ParamDate as ParamDate,
@MonthStartDate as MonthStartDate,
@MonthEndDate as MonthEndDate,
@YearStartDate as YearStartDate,
@YearEndDate as YearEndDate,
@YoYMonthStartDate as YoYMonthStartDate,
@YoYMonthEndDate as YoYMonthEndDate,
@MoMMonthStartDate as MoMMonthStartDate,
@MoMMonthEndDate as MoMMonthEndDate,
@QoQMonthStartDate as QoQMonthStartDate,
@QoQMonthEndDate as QoQMonthEndDate,
@Trailing90Days as Trailing90Days
答案 0 :(得分:0)
我假设您正在尝试使用Custom SQL-您实际上并没有说出来,但是代码和错误消息提示了它。
Tableau在这样的选择中“包装”自定义SQL:
SELECT * FROM
( <custom sql goes here> ) a
这就是为什么您的代码无法正常运行的原因。
此外,您的参数在SQL本身中是硬编码的。我猜您会在适当的时候用Tableau参数替换它。
因此,简单的答案是,您不能按预期使用DECLARE。 长答案是,无需在SQL中声明变量,您只需要始终在自定义SQL中引用参数值即可。
尽管不要尝试使用自定义SQL,它只会使Tableau慢于应有的速度,因为当它可能知道有一个更快的选项时,您将迫使它运行代码。
答案 1 :(得分:0)
将所有代码放入初始SQL中。这是您的连接设置中的一个选项。最终声明应将您的数据推送到临时表中。
然后创建一个自定义sql查询:
Select *
From #temp