1

SQL データベースで同様の値を見つけるのに助けが必要です。次のようなテーブル構造:

    id         |        item_id_nm |      height |    width |     length |     weight
    ----------------------------------------------------------------------------------
    1          |       00000000001 |      1.0    |     1.0  |        1.0 |         1.0
    2          |       00000000001 |      1.1    |     1.0  |        0.9 |         1.1
    3          |       00000000001 |      2.0    |     1.0  |        1.0 |         1.0
    4          |       00000000002 |      1.0    |     1.0  |        1.0 |         1.0
    5          |       00000000002 |      1.0    |     1.1  |        1.1 |         1.0
    6          |       00000000002 |      1.0    |     1.0  |        1.0 |         2.0

id は明らかに重複することはできませんが、item_id_nm は重複する可能性があります (実際には 2 回以上発生する可能性があります)。

高さ、幅、長さ、または重さの値が 30% を超えて異なる場合にのみ、重複する item_id_nm を見つける SQL を作成するにはどうすればよいでしょうか。

テーブルをループする必要があることはわかっていますが、チェックを行うにはどうすればよいですか。助けてくれてありがとう。

編集: %30 の違いの例が含まれています。id = 3 で、id の 1 と 2 の 1.0 (または 1.1) との高さの差は 200% です。明確でなくて申し訳ありませんが、高さ、幅、長さ、または重さの各値に対して 30% の差が生じる可能性があります。それらの 1 つに 30% の違いがある場合、他のものの重複としてカウントされます。

4

4 に答える 4

3

これにより、平均とは 30% 以上異なる行が得られるはずです。

SELECT t1.*
FROM tbl t1
INNER JOIN (
    SELECT
         item_id_nm,
        AVG(width) awidth, AVG(height) aheight, 
        AVG(length) alength, AVG(weight) aweight
    FROM tbl
    GROUP BY item_id_nm ) t2
USING (item_id_nm)
WHERE 
    width > awidth * 1.3 OR width < awidth * 0.7
    OR height > aheight * 1.3 OR height < aheight * 0.7
    OR length > alength * 1.3 OR length < alength * 0.7
    OR weight > aweight * 1.3 OR weight < aweight * 0.7

これにより、30% 異なる行のペアが得られるはずです。

SELECT t1.*,t2.*
FROM tbl t1
INNER JOIN tbl t2
USING (item_id_nm)
WHERE 
     (t1.width > t2.with * 1.3 OR t1.width < t2.width * 0.7)
    OR (t1.height > t2.height * 1.3 OR t1.height < t2.height * 0.7)
    OR (t1.length > t2.length * 1.3 OR t1.length < t2.length * 0.7)
    OR (t1.weight > t2.weight * 1.3 OR t1.weight < t2.weight * 0.7)
于 2013-04-04T21:37:29.367 に答える
2

I think you could use something like this:

SELECT item_id_nm
FROM yourtable
GROUP BY item_id_nm
HAVING
  MIN(height)*1.3 < MAX(height) OR
  MIN(width)*1.3 < MAX(width) OR
  MIN(length)*1.3 < MAX(length) OR
  MIN(weight)*1.3 < MAX(weight)
于 2013-04-04T21:27:30.267 に答える
2
SELECT
    *
FROM
    TableName
WHERE
   (height > 1.3 * width OR height < 0.7 width) OR
   (length > 1.3 * width OR length < 0.7 width)
GROUP BY
    item_id_nm
HAVING
    COUNT(item_id_nm) > 1
于 2013-04-04T21:38:04.150 に答える