0

我想在我的查询中再添加一个案例。如果电话号码的产品有 FACE、FAST 和 MOBIL,则在 answer1 别名列上加 10 分。

我该怎么做我认为我应该使用联合但我不能做算法

我解释其他情况

如果 product 等于 FAST 并且 tree_level 等于 0,1,2,3,4,5 并且 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 点是 3 并且 tree_level 等于 -1 点是 2

我想添加如果号码的产品有脸,移动,快速产品给10分

product.DIG_SEF 表

product|phone|tree_lev  | start_date              | end_date
___________________________________________________________________________
fast   |98998|0         |2013-06-15 00.00.00.0000 |2013-09-15 00.00.00.0000 
mobil  |98998|0         |2013-06-15 00.00.00.0000 |2013-09-15 00.00.00.0000 
face   |98998|0         |2013-06-15 00.00.00.0000 |2013-09-15 00.00.00.0000 
face   |54545|1         |2013-09-15 00.00.00.0000 |2013-12-15 00.00.00.0000 
mobil  |66545|2         |2013-12-15 00.00.00.0000 |2014-03-15 00.00.00.0000 
mobil  |26545|2         |2013-12-15 00.00.00.0000 |2014-03-15 00.00.00.0000 
face   |43645|1         |2013-09-15 00.00.00.0000 |2013-12-15 00.00.00.0000 
face   |34545|1         |2013-09-15 00.00.00.0000 |2013-12-15 00.00.00.0000 
mobil  |26545|2         |2013-12-15 00.00.00.0000 |2014-03-15 00.00.00.0000
face   |56898|-1        |2014-03-15 00.00.00.0000 |2014-06-15 00.00.00.0000 

例如,电话 98998 有 FACE、MOBIL 和 FAST。我想给 answer1 10 分

 select
    DS.phone,
    DS.Product,
    DS.Tree_lev,
CASE  
    WHEN DS.Tree_lev IN (-1) And DS.Product LIKE '%FACE%' THEN  2
    WHEN DS.Tree_lev IN (-1) And DS.Product LIKE '%FAST%' THEN c 2
    WHEN DS.Tree_lev >=0 AND DS.Product LIKE '%FACE%' THEN 3 
    WHEN DS.Tree_lev >=0 AND DS.Product LIKE '%MOBIL%' THEN 3 
    WHEN DS.Tree_lev  AND DS.Product LIKE '%FAST%' THEN  2 
    Else DS.Tree_lev 
    END AS Answer1 
    from produc.DIG_SEF AS DS  

我的查询给出了这个输出

product|phone|tree_lev  | answer1
___________________________________________________________________________
fast   |98998|0         |2
mobil  |98998|0         |3
face   |98998|0         |3
face   |54545|-1        |2
mobil  |66545|2         |3
mobil  |26545|3         |3

我想要这个输出

product     |phone| answer1
___________________________________________________________________________
fast        |98998|2
mobil       |98998|3
face        |98998|3
threproduct |98998|10 // **ı want to add this** 
face        |54545|2
mobil       |66545|3
mobil       |26545|3
4

2 回答 2

0

如果我正确理解了您的问题,则此查询会找到其他行:

select 'theproduct', DS.phone, 10
from produc.DIG_SEf
group by DS.phone
having sum(case when product = 'FAST' then 1 else 0 end) > 0 and
       sum(case when product = 'MOBIL' then 1 else 0 end) > 0 and
       sum(case when product = 'FACE' then 1 else 0 end) > 0;

这将返回所有phone三个产品。

您可以将其与您的查询结合使用union all

select DS.phone, DS.Product, DS.Tree_lev,
       (CASE WHEN DS.Tree_lev IN (-1) And DS.Product LIKE '%FACE%' THEN  2
             WHEN DS.Tree_lev IN (-1) And DS.Product LIKE '%FAST%' THEN c 2
             WHEN DS.Tree_lev >=0 AND DS.Product LIKE '%FACE%' THEN 3 
             WHEN DS.Tree_lev >=0 AND DS.Product LIKE '%MOBIL%' THEN 3 
             WHEN DS.Tree_lev  AND DS.Product LIKE '%FAST%' THEN  2 
             Else DS.Tree_lev 
        END
       ) AS Answer1 
from produc.DIG_SEf DS
union all
select 'theproduct', DS.phone, 10
from produc.DIG_SEf
group by DS.phone
having sum(case when product = 'FAST' then 1 else 0 end) > 0 and
       sum(case when product = 'MOBIL' then 1 else 0 end) > 0 and
       sum(case when product = 'FACE' then 1 else 0 end) > 0;
order by phone;
于 2013-09-03T11:09:34.203 回答
0

你说的对。对于其他结果记录,您需要一个联合子句。假设每个数字在 DIG_SEF 中有 1 到 3 条记录,如果一个数字存在三个条目(即所有产品),您将需要附加行。

select ...
UNION ALL
select 
    phone,
    'threproduct' as product,
    null as Tree_lev,
    10 AS Answer1 
from produc.DIG_SEF
group by phone
having count(*) = 3
order by 1,4
于 2013-09-03T11:16:06.777 回答