SQL查询下表

时间:2020-04-07 19:12:14

标签: sql

我正在尝试编写查询以获取以下结果

预期结果如下表

HouseName   Address        CurrentResident
------------------------------------------
A           1 Market St    Smith
B           2 Market St    Einstein
X           3 Market St    [Vacant]
D           4 Market St    [Vacant]

请帮助

表的SQL查询

CREATE TABLE House 
( 
     HouseName Varchar(1), 
     Address Varchar(255)
);

CREATE TABLE Events 
(
    Id Integer, 
    HouseName Varchar(1), 
    EventName Varchar(255), 
    Date Date, 
    Family Varchar(255)
);

INSERT House VALUES('A', '1 Market St');
INSERT House VALUES('B', '2 Market St');
INSERT House VALUES('X', '3 Market St');
INSERT House VALUES('D', '4 Market St');

INSERT Events VALUES(1,'A','MoveIn','2005-01-31','Smith');
INSERT Events VALUES(2,'A','Remodel','2005-03-31','Smith');
INSERT Events VALUES(3,'A','Remodel','2008-03-31','Smith');
INSERT Events VALUES(4,'A','CleanUp','2008-05-31','Smith');
INSERT Events VALUES(5,'B','MoveIn','2005-01-31','Newton');
INSERT Events VALUES(6,'B','MoveOut','2008-05-01','Newton');
INSERT Events VALUES(7,'B','MoveIn','2008-12-01','Einstein');
INSERT Events VALUES(8,'D','MoveIn','2007-08-31','Soo');
INSERT Events VALUES(9,'D','MoveOut','2010-08-13','Soo');

两个结果表如下

HouseName   Address
    A   1 Market St
    B   2 Market St
    X   3 Market St
    D   4 Market St

事件

   Id   HouseName   EventName   Date    Family
    1   A             MoveIn    2005-01-31  Smith
    2   A            Remodel    2005-03-31  Smith
    3   A            Remodel    2008-03-31  Smith
    4   A            CleanUp    2008-05-31  Smith
    5   B             MoveIn    2005-01-31  Newton
    6   B            MoveOut    2008-05-01  Newton
    7   B             MoveIn    2008-12-01  Einstein
    8   D             MoveIn    2007-08-31  Musk
    9   D             MoveOut   2010-08-13  Musk

所以对于d房,有进场和出场,但之后没有记录在案的进场,所以应该显示为空

我的代码

SELECT HouseName, Address, currentresident  
FROM (SELECT h.*, COALESCE(e.Family, 'Vacant') AS currentresident, 
        ROW_NUMBER()OVER(PARTITION BY h.HouseName ORDER BY e.Date desc) AS rn       
       FROM House AS h left JOIN Events AS e ON h.HouseName=e.HouseName) 
AS t     WHERE t.rn=1

3 个答案:

答案 0 :(得分:0)

在较大的数据库上尝试一下。

SELECT HouseName, Address, currentresident  
FROM (SELECT h.*,
        Case When e.EventName = 'MoveOut' Then 'Vacant' 
             Else COALESCE(e.Family, 'Vacant') End AS currentresident, 
        ROW_NUMBER()OVER(PARTITION BY h.HouseName ORDER BY e.Date desc) AS rn       
       FROM xyzHouse AS h left JOIN xyzEvents AS e ON h.HouseName=e.HouseName) 
AS t     WHERE t.rn=1

答案 1 :(得分:0)

SELECT HouseName, Address, currentresident  
FROM (SELECT h.*, COALESCE(case when e.EventName='MoveOut' then null else e.Family end, 'Vacant') AS currentresident, 
        ROW_NUMBER()OVER(PARTITION BY h.HouseName ORDER BY e.Date desc) AS rn       
       FROM House AS h left JOIN Events AS e ON h.HouseName=e.HouseName) 
AS t     WHERE t.rn=1

答案 2 :(得分:0)

select H.HouseName, H.Address, 
Case When ISNULL(e.EventName,'MoveOut')='MoveOut' then'[Vacant]'
      Else e.Family end as CurrentResident 
from House h
left join Events e on e.id = 
  (
      select max(e2.id) from events e2 
      where e2.housename = h.housename and e2.EventName in ('MoveIn','MoveOut')
  )
相关问题