SQL - 使用重复键连接表

时间:2014-07-20 20:09:07

标签: sql sql-server tsql

我道歉,如果它令人困惑但是......我尽我所能:)

客户表

╔══════════════════════════════════════════════╗
║ 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      ║
╚═══════════════════════════╝

我想了解表帐户

中每个具有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行,但我只想要有关他的最新信息,即最新更新日的行。我该如何编写该查询?

2 个答案:

答案 0 :(得分:2)

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

解决方案1(交叉申请)

SELECT a.customerkey, 
       b.info1, 
       b.info2, 
       b.info3 
FROM   accounts a 
       CROSS apply (SELECT TOP (1) * 
                    FROM   customer b 
                    WHERE  a.customerkey = b.customerkey
                    ORDER BY b.dayofupdate DESC) AS b 
WHERE  acckey IN ( 1, 2, 3 ); 

解决方案2(CTE)

WITH latest_customers 
     AS (SELECT *, 
                Row_number() 
                  OVER( 
                    partition BY customerkey 
                    ORDER BY dayofupdate DESC) AS row_num 
         FROM   customer) 
SELECT a.customerkey, 
       b.info1, 
       b.info2, 
       b.info3 
FROM   accounts a 
       JOIN latest_customers b 
         ON a.customerkey = b.customerkey 
WHERE  acckey IN ( 1, 2, 3 ) 
       AND row_num = 1. 

答案 1 :(得分:1)

尝试:

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获取每个客户(每个customerkey)的最高dayofupdate值