问题描述
在 t-sql 2008 中,我有一个表,我需要根据客户编号、cust_date 和属性 ID 将其连接到自己的服务器项.属性 id 值为 53、54 和 55.相同的属性 id 在一年中可能出现多次,因此 cust_date 可以更改.我需要多次将表中的行连接到自身,其中 cust_date 是相同的并且是最新的日期.
In t-sql 2008, I have a table that I need to join to itself server items based upon customer number, cust_date, and attribute id. The attrribute id values are 53, 54, and 55. There are the same attribute ids that can occur lots of times during the year so the cust_date can change. I need to join rows in the table to itself several times where the cust_date is the same and the most current date.
因此,您能告诉我如何通过选择最大 cust_date 和 for 属性值 = 53、54 和 55 将表连接到自身吗?
Thus can you show me how to join the table to itself with selecting the maximum cust_date and the for attribute value = 53, 54, and 55?
推荐答案
如果我理解你的要求,你可能不需要加入,但可以做类似的事情
If I understand your requirement you may not need a join but could do something like
select customerNumber,
Max(case where attributeid = 53 then Cust_date else null end) as A53CustDate,
Max(case where attributeid = 54 then Cust_date else null end) as A54CustDate,
Max(case where attributeid = 55 then Cust_date else null end) as A55CustDate,
from MyTable
where Attributeid in (53,54,55)
group by
CustomerNumber
这篇关于t-sql 自加入的最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!