我的思路大概如下,主要是先存储通过union all合并2个表,
然后设置一个RID作为总的id,然后A和B各自有CRID 作为子id,并且防止了一个标记用来表名该行属于哪个表
select ROW_NUMBER() OVER (ORDER BY A.ID ASC) RID,* from (
select ROW_NUMBER() OVER (ORDER BY A.ID ASC) CRID, A.ID,A.TIME,'A' 标记 from A
union all
select ROW_NUMBER() OVER (ORDER BY B.ID ASC) CRID, B.ID,B.TIME,'B' 标记 from B
) TAB
放到临时表。
(1) select count(*) from TAB where RID<(select RID from TAB where TAB.标记='B' and TAB.CRID=1) and TAB.标记='A'
(2) select count(*) from TAB where RID between (select RID from TAB where TAB.标记='B' and TAB.CRID=1) and (select RID from TAB where TAB.标记='B' and TAB.CRID=2) and TAB.标记='A'
(3) 同上
(4) select count(*) from TAB where RID>(select RID from TAB where TAB.标记='B' and TAB.CRID=10000) and TAB.标记='A'