2017-03-20 25 views
0

Table_01:選擇從一個表中不同的用戶和其他表的最新日期

---------------- 
ROW_ID | NAME 
---------------- 
11223 | Frank  
11224 | Frank 
11225 | James 
11226 | James 
11227 | Joe 
11228 | Joe 
11229 | Joe 
11230 | Tom 
11231 | Tom 
11232 | Tom 

Table_02:

-------------------------------------------------- 
ROW_ID | NAME | EVENT_DATE | EVENT_LOCATION 
-------------------------------------------------- 
11106 | Frank | 27/01/2017 | London 
11109 | Frank | 19/01/2017 | Munich 
11105 | James | 27/01/2017 | New York 
11110 | James | 02/01/2017 | L.A 
11101 | Joe | 27/01/2017 | Paris 
11104 | Joe | 26/01/2017 | Barcelona 
11107 | Joe | 31/01/2017 | Tokyo 
11102 | Tom | 24/01/2017 | Prague 
11103 | Tom | 03/01/2017 | Lisbon 
11108 | Tom | 20/01/2017 | Dublin 

我想查詢Table_01爲不同的用戶,也知道自己最最近EVENT_DATE和EVENT_LOCATION從表_02

答案:

--------------------------------------------------- 
NAME | NAME | EVENT_DATE | EVENT_LOCATION 
--------------------------------------------------- 
Frank | Frank | 27/01/2017 | London 
James | James | 27/01/2017 | New York 
Joe  | Joe  | 31/01/2017 | Tokyo 
Tom  | Tom  | 24/01/2017 | Prague 

的Microsoft SQL Server 2016兼容級別:SQL Server 2005中(90)

回答

1

可以使用row_number()做到這一點:

select t2.* 
from (select distinct name from table_01) n join 
    (select t2.*, 
      row_number() over (partition by name order by event_date desc) as seqnum 
     from table_02 t2 
    ) t2 
    on n.name = t2.name and t2.seqnum = 1. 

你並不真的似乎需要table_01此,除非你想過濾:

select t2.* 
from (select t2.*, 
      row_number() over (partition by name order by event_date desc) as seqnum 
     from table_02 t2 
    ) t2 
where t2.seqnum = 1. 
+0

什麼table_02和T2之間的區別? –

+0

't2'是'table_02'的別名。 –

0

我不明白這兩個表如何加入exc ept代碼爲name

有很多方法可以做到這一點。下面是其中一些:使用row_number()

with cte as (
    select u.Name, e.Event_Date, e.Event_Location 
     , rn = row_number() over (
       partition by u.Name 
       order by e.[Event_Date] desc 
      ) 
    from (select distinct Name from table_01) u 
     left join table_02 e 
     on u.Name = e.Name 
) 
select Name, Event_Location, Event_Date 
    from cte 
    where rn = 1 

子查詢,而不是cte

with common_table_expression as()

select Name, Event_Location, Event_Date 
from (
    select u.Name, e.Event_Date, e.Event_Location 
     , rn = row_number() over (
       partition by u.Name 
       order by e.[Event_Date] desc 
      ) 
    from (select distinct Name from table_01) u 
     left join table_02 e 
     on u.Name = e.Name 
) as sub 
where rn = 1 

crossapply版本:

select distinct 
    u.Name 
    , e.Event_Location 
    , e.[Event_Date] 
    from (select distinct Name from table_01) u 
    cross apply (
     select top 1 
      Event_Date 
      , Event_Location 
     from table_02 e 
     where e.Name =u.Name 
     order by e.Event_Date desc 
     ) as e; 

top with ties版本:

select top 1 with ties 
    u.Name, e.Event_Date, e.Event_Location 
from (select distinct Name from table_01) u 
    left join table_02 e 
    on u.Name = e.Name 
    order by 
    row_number() over (
     partition by u.Name 
      order by e.Event_Date desc 
    ) 
+0

名稱是唯一的連接行。 –

相關問題