1

SQLクエリに少し問題があります。

私は2つのテーブルを持っています:

表1

id  guid  title     D0      D1     D2
-----------------------------------------
1   guid1 Title1    0.123  -0.235  0.789
2   guid2 Title2   -0.343   0.435  0.459
3   guid3 Title3    0.243  -0.267 -0.934
...
100 guid4 Title100 -0.423   0.955  0.029

および表2(スキーマは同じで、データが異なることに注意してください)。

id  guid  title     D0      D1     D2
----------------------------------------
1   guid1 Title1    0.233  -0.436 -0.389
2   guid2 Title2   -0.343   0.235  0.789
3   guid3 Title3    0.573  -0.067 -0.124
...
100 guid4 Title100 -0.343   0.155  0.005

SELECTすべてのタイトルを返すステートメントを作成する方法を理解しようとしています。すべてWHEREの組み合わせがABS(Table1_D0*Table2_D0)+ABS(Table1_D1*Table2_D1)+ABS(Table1_D2*Table2_D2)しきい値未満です(おそらくハードコーディングされています)。

これまでのところ、を使用しようとしていますがCROSS JOIN、これが正しいアプローチかどうかはわかりません。

これは意味がありますか?Table1、row1はTable2のすべての行に対して、次にTable1、row2はTable2のすべての行に対して

重要な場合は、MSSQLを使用しています。

どうもありがとう!ブレット

4

4 に答える 4

6
SELECT t1.title
FROM Table1 t1
CROSS JOIN table2 t2
WHERE ABS(t1.D0*t2.D0)+ABS(t1.D1*t2.D1)+ABS(t1.D2*t2.D2)<10
于 2011-02-23T20:42:21.977 に答える
0
select t1o.title
from Table1 t1o
where not exists
(
    -- none of the cross-joined rows for t1o must be above the threshold
    select t1.title
    from Table1 t1
    cross join Table2 t2
    where t1.id = t1o.id -- only the cross joined rows for the current t1o row
    -- inverted b/c not exists
    and abs(t1.D0*t2.D0)+abs(t1.D1*t2.D1)+abs(t1.D2*t2.D2) > 10 
)
于 2011-02-23T21:03:31.290 に答える
0

CROSS JOINは正しい選択であり、CROSS JOINは、まったく参加しないのとまったく同じです(Snowbearの回答を参照)。

于 2011-02-23T20:43:06.690 に答える
0
SELECT *
  FROM  Table1 a inner join
        Table2 b on a.Id=b.Id
where   ABS(a.D0*b.D0)+ABS(a.D1*b.D1)+ABS(a.D2*b.D2)<=@Value
于 2011-02-23T20:43:38.247 に答える