加入USQL脚本

时间:2018-12-06 15:08:25

标签: visual-studio azure u-sql

在连接多个表时,可以在usql脚本中使用“间隔”条件吗?下面是代码片段:

SELECT sf.ScoringFeedCode AS ScoringFeedCode,         
     mc.CalendarCode AS CycleCode,
     mc.CalendarGUID AS CycleCalendarGUID,
     (DateTime.ParseExact(mc.StartDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS CycleStartDate,
     (DateTime.ParseExact(mc.EndDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS CycleEndDate,
     (DateTime.ParseExact(mc.CalendarStartDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS MonthCalStartDate,
     (DateTime.ParseExact(mc.CalendarEndDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS MonthCalEndDate,
     qc.CalendarCode AS QtrCode,
     qc.CalendarGUID AS QtrCalendarGUID,
     (DateTime.ParseExact(qc.StartDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS QtrStartDate,
     (DateTime.ParseExact(qc.EndDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS QtrEndDate,
     (DateTime.ParseExact(qc.CalendarStartDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS QtrCalStartDate,
     (DateTime.ParseExact(qc.CalendarEndDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS QtrCalEndDate,
     (DateTime.ParseExact(wc.EndDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS WeekEndDate,
     (DateTime.ParseExact(sfc.MaxChangedOnDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS MaxChangedOnDate,
     (DateTime.ParseExact(sf.LockDate, @datetimeformat, null)).ToString("yyyy-MM-dd") AS LockDate
FROM @ScoringFeedControlData AS sfc
      INNER JOIN
     @ScoringFeedData AS sf
      ON sfc.ScoringFeedGUID == sf.ScoringFeedGUID
      INNER JOIN
          @CalendarFileData AS sc
      ON sfc.CalendarGUID == sc.CalendarGUID
      INNER JOIN
          @CalendarFileData AS wc
      ON wc.EndDate BETWEEN sc.StartDate AND sc.EndDate
      INNER JOIN
          @PeriodTypeFileData AS pt
      ON pt.PeriodTypeGUID == wc.PeriodTypeGUID
      INNER JOIN
          @CalendarFileData AS qc
      ON wc.EndDate BETWEEN qc.StartDate AND qc.EndDate
      INNER JOIN
          @PeriodTypeFileData AS qpt
      ON qpt.PeriodTypeGUID == qc.PeriodTypeGUID
      INNER JOIN
          @CalendarFileData AS mc
      ON wc.EndDate BETWEEN mc.StartDate AND mc.EndDate
      INNER JOIN
          @PeriodTypeFileData AS mpt
      ON mpt.PeriodTypeGUID == mc.PeriodTypeGUID.

usql脚本的联接条件中不允许和允许哪些条件?

1 个答案:

答案 0 :(得分:0)

根据documentation,您只能提供等值联接的联接。任何其他连接条件都必须变成等值连接,例如,通过使计算列映射到“类别”值(仅在某些条件下有效)或将连接条件移到WHERE子句中(如果是INNER JOIN)或进入WHERE子句和其他UNION(如果是OUTER JOIN)。如果没有相等的连接条件,则将INNER JOIN变成CROSS JOIN

这样做的原因是底层引擎对等联接具有更好的分布式执行策略,但是其他条件与用户重写一样昂贵,因此我们决定使成本对查询作者可见。

对于您而言,我希望将INNER JOIN变成CROSS JOIN并将BETWEEN谓词移到WHERE子句中是解决方案。您可能还需要确保连接顺序将在最后一次交叉连接处进行,这会使数据爆炸最少(优化器可以做到这一点,但是有时您需要“帮助”)。