查找仅包含零行和一行的特殊数据的行

时间:2018-06-20 10:30:25

标签: sql join row rank

我有数据,其中group将一行作为零和一,对于相同的数据值将给出一和两。

enter image description here

我尝试使用下面的代码。它似乎不起作用

select *
from (select livecasnum, flag,
             DENSE_RANK()over (partition by livecasnum order by flag) as Ranks
      from TblcaseFlag
      group by livecasnum, flag
     ) b
 group by livecasnum,flag,Ranks
 having count(flag + Ranks) = 1 and flag <> 1

我只需要一排只有零和一个ex的数据:99149

1 个答案:

答案 0 :(得分:1)

为什么不使用not exists呢?

select tf.*
from TblcaseFlag tf
where tf.flag = 0 and
      not exists (select 1 
                  from TblcaseFlag tf1 
                  where tf.livecasnum = tf1.livecasnum and 
                        tf1.flag = 1
                 );