因此,让我们将我的表视为:
col1 data1 col2 data2
0 apple 1 frog
1 orange 0 dino
1 pine 0 dog
0 guava 0 cat
我的结果查询应以data1
的方式由data2
和col1=1
组成,然后显示data1
的值,与col2=1
相同,然后显示data2
。
理想的输出:
data1 data2
orange frog
pine NULL
那有可能吗?
答案 0 :(得分:0)
一切皆有可能:)此sql将为您提供所需的结果:
WITH t1 as
(select rowid,col1,data1,
(select count(*) from thetable temp1
where temp1.col1 = tt.col1 and temp1.rowid < tt.rowid) seq1
from thetable tt
),
t2 as (select col2,data2,
(select count(*) from thetable temp2
where temp2.col2 = tt.col2 and temp2.rowid < tt.rowid) seq2
from thetable tt
)
select data1, data2
from t1
LEFT join t2 on col1 = col2 and seq1 = seq2
where col1 = 1
order by col1
想法是: