答案 0 :(得分:0)
由于缺少表格结构等,这里有一些“虚拟”代码。我们的想法是首先评估每个客户的最小和最大日期,然后在相应的记录之间进行连接:
WITH cte AS(
SELECT [Customer Code_Transaction]) AS CustomerCode
,min([Invoice Date_Transaction]) AS MinDate
,max([Invoice Date_Transaction]) AS MaxDate
FROM [yourtable...]
GROUP BY [Customer Code_Transaction]
)
SELECT t1.[Invoice Date_Transaction]
,t1.[Customer Code_Transaction]
,t1.[Customer Name_Transaction]
,t2.[Address_CustReport]
FROM [yourtable...] AS t1
JOIN cte AS c ON c.MinDate = t1.[Invoice Date_Transaction] AND c.CustomerCode = t1.[Customer Code_Transaction]
JOIN [yourtable...] AS t2 ON t2.[Customer Code_Transaction] = c.[Customer Code_Transaction] AND t2.[Invoice Date_Transaction] = c.MaxDate