2017-01-31 3 views
0

私は比較的新しいSQL(Sql Serverを使用しています)です。私は、アカウント情報とさまざまなエラーコードを格納する10の列を持つテーブルを持っています。問題は、私がリストで提供するエラーコードの少なくとも1つがこれらの10の列のいずれかに存在する場合、レコードを削除する必要があることです。複数の列に存在する可能性のある値に基づいてレコードを削除する

たとえば、番号98と99がColumn1またはColumn2またはColumn3のいずれかに見つかった場合は、結果セットから除外します。

特に、誰かが自分のストアドプロシージャのロジックを私に移していますが、この問題を処理していると思われるコードは(明らかに何かが分からない限り)想定されていることをしません。コードは下にあり、where句にあります。

Select * From arcu.arcuaccountdetailed ad 
Where 
(Case 
when ad.AccountWarningcode1 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode2 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode3 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode4 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode5 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode6 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode7 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode8 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode9 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode10 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode11 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode12 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode13 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode14 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode15 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode16 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode17 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode18 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode19 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
when ad.AccountWarningcode20 IN (98,99,2,29,30,21,10,11,50,53,97) then 1 
Else 0 End) = 1 

私は大いに感謝します。

+0

これは '= 1'とは何でしょうか? –

+0

完全なクエリを表示できますか?何が効いていないのですか? –

+0

Grzegorz、率直に言って、このコード全体が全く意味をなしませんが、私の同僚はそうではないと主張しています。私はこの問題に正しくアプローチしたいと思います。 –

答えて

0

データを修正する必要があります。数値が十分な表に列がある場合は常に、データ・モデルは疑わしいものです。ああ、時には便利ですが、あなたの問題はなぜこれが問題であるかを明確に示しています。 AccountWarningCodeごとに1つの行を持つ別のテーブルが必要です。あなたが行うことができます

ことの一つは、cross applyの巧妙なアプリケーションです。

select ad.* 
from arcu.arcuaccountdetailed ad cross apply 
    (select max(case when v.AccountWarningCode in (98, 99, 2, 29, 30, 21, 10, 11, 50, 53, 97) 
         then 1 else 0 
       end) as flag 
     from (values (ad.AccountWarningcode1), 
        (ad.AccountWarningcode2), 
        . . . 
      ) v(AccountWarningCode) 
    ) awc 
where awc.flag = 1 

この特定の問題を解決しないことがあります - それは、問題のロジックを実装します。しかし、それはコード上でより良い扱いを与え、条件の異なる部分でタイプミスを防止する必要があります。

+0

私は応答を感謝します。私はこの解決策を私の問題に適用する方法を本当に分かっていないが、私はそれを撃つだろう。 @MarkFallone。 –

+0

。 。問合せのどの部分をどのように適用するか分かりませんか? –

関連する問題