仅当单行时,自联接不返回行

时间:2018-03-21 15:01:40

标签: tsql

下面显示的是居民录取状况表的摘录。  我想过滤掉那些日期(允许/重新接纳日期)的居民  在他们最后一次出院后至少30天。

下面的代码似乎工作正常,除非居民只有一行。   Resident 200(见表)只有一个条目,因此不会被查询返回。

如何让查询返回此行? 左右连接不能给出正确的结果。

 ;with sorted as 
  (
     select ROW_NUMBER() over(partition by rsa.cono,rsa.resno order by rsa.sort desc) rn,rsa.*
  from ri_status_admit rsa 
 )

select   s1.cono,s1.resno,s1.rn,s1.date,s1.admit_disch,s2.date,s2.rn,
                s2.admit_disch,datediff(dd,s2.date, s1.date)
         from sorted s1 
             join sorted s2
         on 
              s1.cono = s2.cono
              and s1.resno = s2.resno 
              and s1.admit_disch in ('A','R')
              and s2.rn = s1.rn + 1
              and datediff(dd,s2.date, s1.date) >= 30 


 rn cono    resno   date            sort    admit_disch
 1  01     145     7/5/2009     73047           D
 2  01     145     11/23/2007    4872           R
 3  01     145     11/19/2007    54797          D
 4  01     145     4/21/2007    48403           R
 5  01     145     4/20/2007    48400           D
 1  01     152     1/4/2010     77674           D
 2  01     152     6/1/2001        97           A
 1  01     170     9/15/2009    74881           D
 2  01     170     5/15/2004    18733           R
 3  01     170     5/12/2004    18666           D
 4  01     170     3/1/2004     17014           R
 5  01     170     2/17/2004    16686           D
 6  01     170     1/2/2001       104           A
 1  01     200     1/4/2017       100           A

2 个答案:

答案 0 :(得分:0)

您可以切换INNER JOIN的{​​{1}}并使用条件LEFT JOIN强制加入WHERE

EXISTS

答案 1 :(得分:0)

我认为这是不正确的。假设在'A'或'R'之前总是存在'D'行,这对于该数据是正确的。但是连续也有两个D行。反向假设D行后跟A和R不成立。

 declare @T table (cono int, resno int, dt  date, sort int, a_d char(1))
 insert into @T values 
        (01, 145, '7/5/2009',   73047, 'D')
      , (01, 145, '11/23/2007',  4872, 'R')
      , (01, 145, '11/19/2007', 54797, 'D')
      , (01, 145, '4/21/2007',  48403, 'R')
      , (01, 145, '4/20/2007',  48400, 'D')
      , (01, 152, '1/4/2010',   77674, 'D')
      , (01, 152, '6/1/2001',      97, 'A')
      , (01, 170, '9/15/2009',  74881, 'D')
      , (01, 170, '5/15/2004',  18733, 'R')
      , (01, 170, '5/12/2004',  18666, 'D')
      , (01, 170, '3/1/2004',   17014, 'R')
      , (01, 170, '2/17/2004',  16686, 'D')
      , (01, 170, '1/2/2001',     104, 'A')
      , (01, 200, '1/4/2017',     100, 'A');
select t.* 
     , ROW_NUMBER() over (partition by cono, resno order by sort desc) as rn
 from @T t
order by cono, resno, sort desc 

cono        resno       dt         sort        a_d  rn
----------- ----------- ---------- ----------- ---- --------------------
1           145         2009-07-05 73047       D    1
1           145         2007-11-19 54797       D    2
1           145         2007-04-21 48403       R    3
1           145         2007-04-20 48400       D    4
1           145         2007-11-23 4872        R    5
1           152         2010-01-04 77674       D    1
1           152         2001-06-01 97          A    2
1           170         2009-09-15 74881       D    1
1           170         2004-05-15 18733       R    2
1           170         2004-05-12 18666       D    3
1           170         2004-03-01 17014       R    4
1           170         2004-02-17 16686       D    5
1           170         2001-01-02 104         A    6
1           200         2017-01-04 100         A    1