简体   繁体   English

如何更新日期从第一个交易日期开始的日期和地址从最近交易的日期开始的交易数据(如果已更新)?

[英]How to get the transaction data with Date from First transaction date and Address from latest transaction Date if updated?

I am trying to rearrange the data with Date as First transaction/Shopping date and address from latest transaction. 我正在尝试重新排列日期为第一笔交易/购物日期和最新交易的地址的数据。 The Data consists of transaction of a single customer on various dates. 数据包括单个客户在不同日期的交易。 Please help. 请帮忙。

在此处输入图片说明

The Desired Out should be like: 期望的输出应类似于:

在此处输入图片说明

Due to lack of table structure etc., here some "dummy" code. 由于缺少表结构等,这里有一些“虚拟”代码。 The idea is to first evaluate min and max date for each customer and then perform a join between the corresponding records: 这个想法是先评估每个客户的最小和最大日期,然后在相应的记录之间执行联接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM