2

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

  • product が FAST と等しい場合...
    • tree_level が 0,1,2,3,4,5 の場合、0,1,2,3,4,5 の数を数えます。たとえば、「0,1,2,3,4,5」を含む項目が 30 個あるとします。次に、30*2 を掛けます。
    • および tree_level が -1 の場合、-1 の数を数えて 2 を掛けます
  • product が 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 の場合、-1 の数を数えて 2 を掛けます

where、case、および counter ステートメントを一緒に使用するにはどうすればよいですか? 私はそんなことはできません。

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

WHERE DS.Product like '%FACE%' (
CASE  
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
END  

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

WHERE DS.Product like '%FAST%' (
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
END  )


    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 d.FR  AS Ds 
            inner join d.dbo.DW AS Dw 
            ON DW.CEP_TEL = DS.PersTel
4

1 に答える 1

1

これで始められるはずです:

select
    product,
    tree_level,
    count(1) over (partition by product,tree_level_category) 
        * case when product like '%FACE%'
               then case tree_level_category
                        when '0-5' then 3
                        when '-1' then 2
                    end
               when product like '%MOBIL%'
               then case tree_level_category
                        when '0-5' then 3
                    end
               when product like '%FAST%'
               then case tree_level_category
                   when '0-5' then 2
                   when '-1' then 2
              end
        end 
from (
    select
        product,
        tree_level,
        case when tree_level in (0,1,2,3,4,5)
             then '0-5'
             when tree_level = '-1'
             then '-1'
             else null 
        end tree_level_category
    from
       product
) as t

ここで SQLFiddle

于 2013-08-29T12:51:36.597 に答える