t-sql maximum date on self join(t-sql 自加入的最大日期)
问题描述
在 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 自加入的最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:t-sql 自加入的最大日期
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
