2

仍未找到更新 2 中描述的解决方案

谢谢帮助

我试着用我糟糕的英语解释我的问题。希望有人能解决我的问题。

我得到了下表

A      B
1      Y
2      null
3      Y

我想要什么结果?根据A列中的排名,我想合并B列。

该示例中的结果是......没有结果原因是因为排名 2 中有一个空值,而下一个和最后一个排名 (=3) 有一个值 (=Y)。

下一个例子

A      B
1      Y
2      null
3      null

我想要的结果是

A   B
1   Y

因为之后的方式是免费的......意味着2和最后3有空

另一个例子

A    B
1    null
2    N
3    null

在这种情况下,我想要再次没有结果。因为 first =1 有空值。

我现在尝试得出结论...如果 B 列的 n(例如 2)具有值 Y 或 N,则元素 bevor(在此 1 中)必须具有值 Y 或 N。

非常感谢您。我尝试了不同的技术但没有成功...

更新 1 谢谢你的快速评论

一些具有预期结果的示例日期

example 1
A   B
1   Y
2   N
3   null
4   null

expected result
A    B
2    N

example 2
A   B
1   N
2   Y
3   N
4   null

expected result
A   B
3   N

example 3
A   B
1   null
2   Y
3   Y
4   null

expected result
no result

example 4
A   B
1   Y
2   Y
3   null
4   Y

expected result
no result

更新 2

忘记基本情况

A   B
1   Y
2   N
3   Y

预期结果

A   B
3   N
4

4 回答 4

3

建立 A 的最大值,其中 B 为 Y 或 N,以及 A 的最小值,其中 B 为空。如果第一个值低于第二个值,则您有一个有效的结果集。

select yt.A 
       , yt.B
from 
    ( select max(case when B is not null then A else null end) as max_b_yn
             , min(case when B is null then A else null end) as min_b_null
      from your_table ) t1 
   , your_table yt
where ( t1.min_b_null is null 
        or t1.max_b_yn < t1.min_b_null )
and yt.A = t1.max_b_yn 
/
于 2013-01-31T14:26:40.690 回答
0

我认为你想要第一个 NULL 之前的最后一行。如果这是正确的,那么以下得到你想要的:

 select t.*
 from t join
      (select min(A) as NullA from t where B is NULL) t2
      on t.A = t2.NullA - 1

啊,我明白了。要获得带有“N”的最后一行:

 select t.*
 from t
 where t.A in (select max(A) as MaxA
               from t join
                    (select min(A) as NullA from t where B is NULL) t2
                    on t.A < t2.NullA
                where t.B = 'N'
               )
于 2013-01-31T14:14:52.473 回答
0

好的,我想我有你需要的东西。只要在它之前没有带有 NULL 的行,它就会抓取 B 列中具有值的最后一行。

select MAX(A), B
from table_a
where B IS NOT NULL
and table_a.A > (
          select MIN(A)
          FROM table_a
          WHERE B IS NULL
         )
Group by B
于 2013-01-31T14:18:05.210 回答
-3
select top 1 A, B
from #Temp
where B is not null
order by A desc

或者

select top 1 A, B
from #Temp
where B = 'N'
order by A desc
于 2013-01-31T14:19:56.613 回答