Select 来自内部连接子查询的前 1 个

Select top 1 from inner join subquery

我希望 select 每次成功连接时,内部连接中列的第一个值。

@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 中获得了 EndDate 最低的 EndDate,其中满足了加入 ciretia。

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

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

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

适合我的情况。

您可以在 INNER JOIN SELECT:

上应用 FIRST_VALUE 函数

检查这个: https://docs.microsoft.com/en-us/u-sql/functions/analytic/first-value