2017-06-16 11 views
0

私は1つの質問がありますが、テーブルから選択したいがエイリアス列を持ち、各列には条件付きが1つあります。値がSQL - 各列には1つの条件があります

Select Value, Target, Plan from Product 

条件があります、すべてのおかげで:where Product.DetailCode = 'B'、 列Value =Product.Value * 100

EDIT:私はC#でのようにしたい:

if(Product.DetailCode == 'A') {Value = Product.Value * 10} else if(Product.DetailCode == 'B') {Value = Product.Value * 100} 

EDIT2 where Product.DetailCode = 'A'列​​を、ターゲットの条件を持っています 最後に、私は自分の答えを持っています。

select case when product.code = 'A' then product.Value * 10 end as Value, case when product.code = 'B' then product.Value * 100 end as Target from product

本当にありがとうございました!

+0

ご質問はありますか? –

+1

'Product.DetailCode = 'A'とProduct.DetailCode = 'A''はどこですか?それは働いていないのですか? –

+0

のようなものを探しています: 'Product.DetailCode = 'A'またはProduct.DetailCode = 'A'' –

答えて

0

編集:変更内容を読んだ後 :

Select 
    if(Product.DetailCode = 'A', value *10, if(Product.DetailCode = 'B', value * 100, value)) AS target 

detailCodeは何か他のものであればdetailCodeはBと値の場合detailcodeは、値* 100である場合、これは10 *値を使用します。

1
Select 
case when Product.DetailCode = 'A' then 100 
    WHEN Product.DetailCode = 'B' then 10 end * Product.Value as value 
    , TARGET 
    , Plan 
from Product 
1

あなたはこの

SELECT 
Value = 
     CASE Product.DetailCode 
     WHEN 'A' THEN Product.Value * 10 
     WHEN 'B' THEN Product.Value * 100 
     ELSE Product.Value * 100 
     END 
, Target, Plan 
FROM Product 
0

はそのようなハードコーディング条件の代わりにルックアップテーブルを作成することを検討して使用することができます。クエリは次のようになります。

select p.Value * coalesce(l.koeff,1) Value, Target, Plan 
from Product p 
left join Lookup l on p.DetailCode = l.Code 
関連する問題