从内部联接子查询中选择前1个

时间:2018-10-23 08:17:13

标签: azure-data-lake u-sql

我希望为每个成功的联接选择内部联接中列的第一个值。

@getEndDate = SELECT c.CustomerId, c.ProductId FROM @customer AS c
INNER JOIN (SELECT (DateTime?) EndDate AS EndDate, (int) CustomerId AS CustomerId 
FROM @Installation 
ORDER BY EndDate FETCH 1 ROW OFFSET 0 ROWS) AS i ON c.CustomerId == i.CustomerId

这样做,我得到了@Installation中满足连接Ciretia的最低EndDate。

对于每个成功的联接,如何从内部联接子查询中仅获取第一个值?

2 个答案:

答案 0 :(得分:0)

选择在内部联接子查询中执行以下操作:

(SELECT (DateTime?) MAX(EndDate) AS EndDate, (int) CustomerID AS CustomerID FROM 
@Installation GROUP BY CustomerID) 

适合我的情况。

答案 1 :(得分:0)

您可以在INNER JOIN SELECT上应用FIRST_VALUE函数:

检查以下内容: https://docs.microsoft.com/en-us/u-sql/functions/analytic/first-value