1

クエリを実行したい。Microsoft SQL を使用します

product が FAST に等しく、tree_level が 0,1,2,3,4,5 に等しい場合 数値 0,1,2,3,4,5 をカウントし、2 を乗算し、tree_level が -1 に等しい 数値をカウントし、2 を乗算します

product が MOBIL に等しく、tree_level が 0,1,2,3,4,5 に等しい場合、その数に 3 を掛けてカウントし、

product が FACE に等しく、tree_level が 0,1,2,3,4,5 に等しい場合 0,1,2,3,4,5 の数を数えて 3 を掛け、tree_level が -1 に等しい数を数えます 2 を掛けます

同じクエリを実行したいのですが、アルゴリズムを実行できません

結合されたテーブル

perstel| AD|SOYAD|RefPhoner|Product |Tree_level
_______________________________________________
7857887|AS |DFDSF|5645545  |FAST    |0
6566464|SD |DFDDS|4578857  |MOBİL   |1
7487887|SD |FSDFD|8787878  |FACE    |2 
7487887|SD |FSDFD|8788278  |FACE    |2 
7487887|SD |FPOFD|8933878  |MOBIL   |5 
7445887|WE |FSPLD|8771878  |FACE    |3 
7387887|SD |LBDFD|8712878  |FAST    |4 
0487887|WE |FSPLD|8771878  |FACE    |-1
4487887|WE |FOLLD|8771878  |MOBIL   |-1

はこれを出力したい

perstel| AD(name at eng)|SOYAD|RefPhoner|Product |Tree_level | POint
_________________________________________________________________
7857887|AS       |DFDSF|5645545  |FAST    |-1         | 2 (because it is -1 and it is face so it is point 2)
6566464|EM       |DFDDS|4578857  |FACE    |2          | 3 (because it is 2 and it is face  so it is point 3)
7487887|MM       |FSDFD|8787878  |FAST    |2          | 2 .....
7487887|AS       |DFDSF|8788278  |MOBIL   |0          | 3 ...
7487887|EM       |DFDDS|8933878  |FAST    |-1         | 2 ...
7445887|HL       |FSPLD|8771878  |FACE    |3          | 3 ...

だから私はその後、その人のすべてのポイントを合計します

私はそれをするだけです:(

select
    DS.PersTel ,
    DW.AD ,
    DW.SOYAD ,
    DS.RefPhoner   ,
    DS.Product ,
    DS.Tree_level 

    from dw_prod.FRTN.DIG_SEFER  AS DS 
    inner join dw_prod.dbo.DW_MUST AS DW 
    ON DW.CEP_TEL = DS.PersTel

私はそれを更新します私はそれを試しましたが、それでも私の間違いは何ですか

   select
    DS.PersTel ,
        DW.AD ,
        DW.SOYAD ,
        DS.RefPhoner   ,
        DS.Product ,
        DS.Tree_level 
    CASE DS.Tree_level 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) THEN count(DS.Tree_level) * 3 
    WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
    WHERE DS.Product like '%FACE%' END AS Answer1

    CASE DS.Tree_level 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) THEN count(DS.Tree_level) * 3 
    WHERE DS.Product like '%MOBIL%' END AS Answer2

    CASE DS.Tree_level 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) THEN count(DS.Tree_level) * 2 
    WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
    WHERE DS.Product like '%FAST%' END AS Answer3

    from dw_prod.FRTN.DIG_SEFER  AS DS 
        inner join dw_prod.dbo.DW_MUST AS DW 
        ON DW.CEP_TEL = DS.PersTel

更新されたケース部分

       select
        DS.PersTel ,
            DW.AD ,
            DW.SOYAD ,
            DS.RefPhoner   ,
            DS.Product ,
            DS.Tree_level 
    CASE  
    WHEN DS.Tree_level IN (0,1,2,3,4,5)AND DS.Product LIKE '%FACE%' THEN count(DS.Tree_level) * 3 
    WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
    END AS Answer1 

    CASE DS.Tree_level
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%MOBIL%' THEN count(DS.Tree_level) * 3 
    END AS Answer2 

    CASE  DS.Tree_level
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%FAST%' THEN count(DS.Tree_level) * 2 
    WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
    END AS Answer3

  from dw_prod.FRTN.DIG_SEFER  AS DS 
            inner join dw_prod.dbo.DW_MUST AS DW 
            ON DW.CEP_TEL = DS.PersTel
4

3 に答える 3

1

このクエリを試して、まだ問題に直面しているかどうか教えてください

select
DS.PersTel ,
DW.AD ,
DW.SOYAD ,        
DS.RefPhoner   ,
DS.Product ,
DS.Tree_level ,
CASE  
    WHEN DS.Tree_level IN (-1) And DS.Product LIKE '%FACE%' THEN count(DS.Tree_level) * 2
    WHEN DS.Tree_level IN (-1) And DS.Product LIKE '%FAST%' THEN count(DS.Tree_level) * 2
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%FACE%' THEN count(DS.Tree_level) * 3 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%MOBIL%' THEN count(DS.Tree_level) * 3 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%FAST%' THEN count(DS.Tree_level) * 2 
    Else DS.Tree_level 
END AS Answer1 
from dw_prod.FRTN.DIG_SEFER  AS DS 
inner join dw_prod.dbo.DW_MUST AS DW 
ON DW.CEP_TEL = DS.PersTel
    Group by DS.PersTel , DW.AD , DW.SOYAD , DS.RefPhoner   , DS.Product , DS.Tree_level   
于 2013-08-29T12:30:29.983 に答える
1

あなたはこれを行うことはできません:

CASE DS.Tree_level 
WHEN DS.Tree_level IN (0,1,2,3,4,5) THEN count(DS.Tree_level) * 3 
WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
WHERE DS.Product like '%FACE%' END AS Answer1

しかし、次のことができます。

CASE 
    WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product like '%FACE%' THEN count(DS.Tree_level) * 3 
    WHEN DS.Tree_level IN (-1) AND DS.Product like '%FACE%' THEN count(DS.Tree_level) * 2
END AS Answer1

(または、case ステートメントをネストすることもできますが、それは見苦しいかもしれません!)

アップデート:

各「ケース」をコンマで区切ってください:

CASE  
WHEN DS.Tree_level IN (0,1,2,3,4,5)AND DS.Product LIKE '%FACE%' THEN count(DS.Tree_level) * 3 
WHEN DS.Tree_level IN (-1) THEN count(DS.Tree_level) * 2
END AS Answer1, -- COMMA HERE IMPORTANT 

CASE DS.Tree_level
WHEN DS.Tree_level IN (0,1,2,3,4,5) AND DS.Product LIKE '%MOBIL%' THEN count(DS.Tree_level) * 3 
END AS Answer2 

通常のクエリの場合と同様に、フィールドを区切る必要があるためです。

SELECT Field1, Field2, Field3 FROM X

また

SELECT CASE X WHEN Y THEN Z END as Field1, Field2, Field3 FROM X

カンマは重要です (FROM の前にカンマは必要ありません。各フィールドの間だけです)。

于 2013-08-29T11:22:13.643 に答える
0

SQL Server の CASE ステートメントを見てください。

MSDN の例

Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 
Searched CASE expression:
CASE
     WHEN Boolean_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END

リンクはこちら

何かのようなもの

SELECT
CASE WHEN tree_level IN (0,1,2,3,4,5) THEN tree_level * 2 END AS Answer1
....
于 2013-08-29T09:06:01.273 に答える