0

販売注文データ (注文番号、製品番号、販売価格など) を含むテーブルがあります。

ただし、テーブルには修正やその他の無効なデータが散らばっています。主な問題の 1 つは、以前の注文の金額に等しいマイナスの合計を含む新しい行を追加することによって修正が入力されたことです。営業担当者は常に徹底しているとは言えず、新しい注文番号を付けたり、修正に製品番号を記載することさえなかったりすることがよくありました。

合計が負のすべての行と、一致する (または合計が同じ他の) 合計が正の行をすべて削除したいと思います。

私の最初の考えは、すべての負の合計行と、反対の合計を持つ正の行を単純に削除することでした。ただし、多くの負の注文に対して複数の正の注文が存在するため、誤って正の行を多数削除することになります。

合計が負の行をすべて削除し、合計が逆の行ごとに1 つの行を削除するにはどうすればよいですか?

4

4 に答える 4

1

Data cleanup tasks are painful no matter what. From what you've described, there is not enough information to fully automate this task. This is typical for data cleanup.

First you need to have a talk with your immediate manager and let him know the magnitude of the problem. It's not your fault the data is all screwed up, and it will take time to fix it without losing any valid information and without interrupting the sales operations.

The most important tip about data cleanup is that it's more trouble than it's worth to try to automate fully. Your strategy should be to reduce the problem by taking care of the easy cases, until you can do the remainder manually. There will always be complex edge cases, and trying to handle them all with clever SQL is an exercise in diminishing returns.

  1. Take care of the low-hanging fruit, where the negative "correction" has a valid order number, so you can make a strong correlation to the order it is intended to cancel.

  2. Create a correlation between the remaining negatives and the most recent single order rows with the same quantity. Use other columns to correlate them if you can, for instance if the correction is entered by the same salesperson who entered the original order.

  3. The next stage would be to delete negatives where the order number is valid, but it maps to multiple rows that sum up to the total value.

  4. Then start on matching negatives without order numbers to multiple rows that sum up to the value in the correction. This can be tricky to automate, but by this time the number of negatives might be few enough that you can do it manually, by eyeballing them one by one.

The other tip is that SQL Anywhere appears to have a multi-table DELETE syntax. I don't use SQL Anywhere, but I found this in the online docs:

Syntax

DELETE [ row-limitation ] 
  [ FROM ] [ owner.]table-expression
  [ FROM table-list [,...] ]
  [ WHERE search-condition ]
  [ ORDER BY { expression | integer } [ ASC | DESC ], ... ]
  [ OPTION( query-hint, ... ) ]

It looks like the first FROM clause lists the table you want to delete rows in. The second FROM clause allows you to do joins for purposes of restricting the rows. Since you're likely to be doing self-joins, remember that you need to give an alias (aka correlation name) in the first FROM to avoid ambiguity.

于 2009-11-12T19:29:19.133 に答える
1

データの量にもよりますが、私は力ずくでそれを行います。

すべての負の合計行を一時テーブルに選択します

カーソルを使用して各行を調べてから、単一の一致についてデータベースにクエリを実行します (タイムスタンプ、注文番号、または所有している可能性のある主キーで max() を使用します。その「一致する」行を削除します。

次に、すべての負の行を削除します

サブクエリを使用して1つのステートメントで実行できることは間違いありませんが、それを理解してテストするまでに、上記を使用して仕事を完了できます:)

于 2009-11-12T19:11:26.880 に答える
0

DELETEを1回ラップせずに、内側のSELECTを実行して、実行する前にデータに問題がないことを確認しますが、これで問題ないと確信しています。

DELETE FROM
   orders
WHERE
   orderID IN (
       SELECT
          orderID
       FROM (
          SELECT 
             MIN(orderID) orderID, total
          FROM
             orders
          WHERE
             total IN (
                SELECT
                   total * -1
                FROM
                   orders
                WHERE
                   total < 0
             )
          GROUP BY
             total
      )derived
   )

DELETE FROM
    orders
WHERE
    total < 0
于 2009-11-12T19:20:47.173 に答える
0

2 つの行をリンクする共有識別子は何ですか? これがないと、行をリンクするものが何もないのでできません

とにかく、それは次のようなものになります

DELETE MyTable
WHERE EXISTS (
    SELECT * FROM MyTable M2
    GROUP BY M2.LinkID
    HAVING SUM(M2.ValueCol) < 0 AND MyTable.KeyCol = M2.KeyCol
    )
于 2009-11-12T19:11:36.660 に答える