1

我想做查询。我使用微软 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

你不能这样做:

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 回答
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 回答
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 回答