我想做查询。 ı使用Microsoft sql
如果乘积等于FAST且tree_level等于0,1,2,3,4,5则计数数字0,1,2,3,4,5并乘以2且tree_level等于-1计数乘以2 < / p>
和 如果乘积等于MOBIL且tree_level等于0,1,2,3,4,5则将该数乘以3 和
如果乘积等于FACE且tree_level等于0,1,2,3,4,5则计数0,1,2,3,4,5并乘以3且tree_level等于-1计数乘以2 < / p>
我想做同样的查询,但我不能做算法
陈旧表
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
答案 0 :(得分: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
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之前使用逗号,只需在每个字段之间)
答案 1 :(得分: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
答案 2 :(得分: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
....