SQL-对具有重复键的表进行联接

SQL-对具有重复键的表进行联接,sql,sql-server,tsql,Sql,Sql Server,Tsql,如果让人困惑,我很抱歉,但是……我尽力了:) 客户表 ╔══════════════════════════════════════════════╗ ║ CustomerKey DayOfUpdate Info1 Info2 ║ ╠══════════════════════════════════════════════╣ ║ 1 201201 x x ║ ║ 1 201305

如果让人困惑,我很抱歉,但是……我尽力了:)

客户表

╔══════════════════════════════════════════════╗
║ CustomerKey    DayOfUpdate    Info1    Info2 ║
╠══════════════════════════════════════════════╣
║ 1              201201         x        x     ║
║ 1              201305         x        x     ║
║ 1              201405         x        x     ║
║ 2              201306         x        x     ║
║ 3              201308         x        x     ║
╚══════════════════════════════════════════════╝
╔═══════════════════════════╗
║     CustomerKey    AccKey ║
╠═══════════════════════════╣
║     1              1      ║
║     2              2      ║
║     3              3      ║
╚═══════════════════════════╝
账户表

╔══════════════════════════════════════════════╗
║ CustomerKey    DayOfUpdate    Info1    Info2 ║
╠══════════════════════════════════════════════╣
║ 1              201201         x        x     ║
║ 1              201305         x        x     ║
║ 1              201405         x        x     ║
║ 2              201306         x        x     ║
║ 3              201308         x        x     ║
╚══════════════════════════════════════════════╝
╔═══════════════════════════╗
║     CustomerKey    AccKey ║
╠═══════════════════════════╣
║     1              1      ║
║     2              2      ║
║     3              3      ║
╚═══════════════════════════╝
我想要关于在表Accounts中具有AccKey值的每个客户的最新信息

我试着做了一个:

SELECT a.customerkey, 
       b.info1, 
       b.info2, 
       b.info3 
FROM   accounts a 
       JOIN customer b 
         ON a.customerkey = b.customerkey 
WHERE  acckey IN ( 1, 2, 3 ) 
例如,对于
CustomerKey
为1的客户,它返回3行,但我只想要关于他的最新信息,即具有最新更新日期的行。如何编写该查询?

试试:

select a.CustomerKey, b.info1, b.info2, b.info3
  from accounts a
  join customer b
    on a.customerKey = b.CustomerKey
 where AccKey in (1, 2, 3)
   and b.dayofupdate =
       (select max(x.dayofupdate)
          from customer x
         where x.customerkey = b.customerkey)
使用sub获取每个客户的最高dayofupdate值(每个customerkey)

尝试:

select a.CustomerKey, b.info1, b.info2, b.info3
  from accounts a
  join customer b
    on a.customerKey = b.CustomerKey
 where AccKey in (1, 2, 3)
   and b.dayofupdate =
       (select max(x.dayofupdate)
          from customer x
         where x.customerkey = b.customerkey)
使用sub获取每个客户的最高dayofupdate值(每个customerkey)

尝试:

select a.CustomerKey, b.info1, b.info2, b.info3
  from accounts a
  join customer b
    on a.customerKey = b.CustomerKey
 where AccKey in (1, 2, 3)
   and b.dayofupdate =
       (select max(x.dayofupdate)
          from customer x
         where x.customerkey = b.customerkey)
使用sub获取每个客户的最高dayofupdate值(每个customerkey)

尝试:

select a.CustomerKey, b.info1, b.info2, b.info3
  from accounts a
  join customer b
    on a.customerKey = b.CustomerKey
 where AccKey in (1, 2, 3)
   and b.dayofupdate =
       (select max(x.dayofupdate)
          from customer x
         where x.customerkey = b.customerkey)

使用sub获取每个客户的最高dayofupdate值(每个customerkey)

假设SQL Server 2005或更高版本,下面是一些其他方法:

解决方案1(交叉应用)

解决方案2(CTE)


假设SQL Server 2005或更高版本,以下是两种其他方法:

解决方案1(交叉应用)

解决方案2(CTE)


假设SQL Server 2005或更高版本,以下是两种其他方法:

解决方案1(交叉应用)

解决方案2(CTE)


假设SQL Server 2005或更高版本,以下是两种其他方法:

解决方案1(交叉应用)

解决方案2(CTE)