0

私は次のようなテーブルを持っています:

テーブルtable1(id integer、firstname text、lastname text);を作成します。

id名姓

====================
1ベンテイラー
2ロブテイラー
3ロブスミス
4ロブゾンビ
5ピータースミス
6ベンスミス
7ピーターテイラー

これで、別のテーブルがテーブルtable2(id integer、position integer);を作成するようになりました。

ID位置

===========
1 5
1 9
2 6
3 7
6 2

姓がbenとrobで共有され、名がbenとrobである必要がある、姓の行を選択したいと思います。
ここで、ベンの位置をロブの位置より1つ小さくしたいので、結果は次のようになります。

id名姓位置

===========================
1ベンテイラー52
ロブテイラー6

SQLクエリはどうあるべきですか?

4

3 に答える 3

2

姓を取得するには:

select lastname
from names n join
     position p
     on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname
having count(distinct firstname) = 2 and
       1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)

その後、次の方法で元のリストを取得できます。

select n.*, p.position
from names n join
     position p
     on n.id = p.id
where firstname in ('ben', 'rob') and
      lastname in (select lastname
                   from names n join
                        position p
                        on n.id = p.id
                   where firstname in ('ben', 'rob')
                   group by lastname
                   having count(distinct firstname) = 2 and
                          1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)
                  )

次のクエリはあなたの質問に答えると思いますが、これは名前を 1 つの行に結合するという警告があります。

select nben.*, p.position, nrob.*, prob.position
from names nben join
     positions p
     on nben.id = p.id and
        nben.firstname = 'ben' join
     names nrob
     on nrob.firstname = 'rob' and
        nrob.lastname = nben.lastname join
     positions prob
     on nrob.id = prob.id and
        p.position = prob.position - 1

また、これはテストされていません。

于 2013-01-14T17:34:12.337 に答える
0

これにより、Gordon Linoff のソリューションから得た必要な結果が得られます:
select * from(select n.*, p.position
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob' ) and
lastname in (select lastname
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname with
count(distinct firstname) = 2

              )
         )

as x,names,position
where
(x.id!=names.id and names.id=position.id and names.lastname=x.lastname
and (x.firstname='rob' and names.firstname='ben') x.position=position.position+1)
または
(x.id!=names.id and names.id=position.id and names.lastname=x.lastname
and (x.firstname='ben' and names.firstname) ='rob') および x.position+1=position.position)


于 2013-01-15T04:25:58.797 に答える
-1

Gordon Linoff の解決策: 名前から
nben.*, p.position を選択 nben 位置 p を nben.id = p.id および nben.firstname = 'ben' で結合 nrob.firstname = 'rob' および nrob.lastname =で 名前 nrobを結合nben.lastname は、nrob.id = prob.id および p.position = prob.position - 1で 位置 probを結合すると、次 と同じ結果が得られます。













select * from(select n.*, p.position
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob') and
lastname in (select lastname
from names n join
position p.
on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname
count(distinct firstname) = 2

          )
     )

as x,names,position
where (x.id!=names.id and names.id=position.id
and names.lastname=x.lastname
and (x.firstname='ben' and names.firstname='rob')
x.position+1=position.position)

于 2013-01-15T04:49:47.187 に答える