我有一个包含客户信息的表.为每个客户分配一个客户ID(他们的SSN),他们在打开更多帐户时保留这些ID.两个客户可能在同一个帐户中,每个客户都有自己的ID.帐号不按日期排序.
我想找到每个客户或客户群的最新帐户.如果两个客户曾经在一个帐户上,我想要返回客户所在的最新帐户.
这是一个包含一些可能情况的示例表.
示例表ACCT:
acctnumber date Cust1ID Cust2ID
10000 '2016-02-01' 1110 NULL --Case0-customer has only ever had
--one account
10001 '2016-02-01' 1111 NULL --Case1-one customer has multiple
10050 '2017-02-01' 1111 NULL --accounts
400050 '2017-06-01' 1111 NULL
10089 '2017-12-08' 1111 NULL
10008 '2016-02-01' 1120 NULL --Case2-customer has account(s) and later
10038 '2016-04-01' 1120 NULL
10058 '2017-02-03' 1120 1121 --gets account(s) with another customer
10002 '2016-02-01' 1112 NULL --Case3-customer has account(s) and later
10052 '2017-02-02' 1113 1112 --becomes the second customer on …Run Code Online (Sandbox Code Playgroud)