0

MySQL で 3 つの値を追加する際に問題が発生しています。これは単純なはずです。

2 番目の列の値に基づいて列から値を選択するコードがあり、次のような case ステートメントを使用します。

Select
        Max(Case
            When Table1.costcode Like '%Costcode1%' 
            Then Table1.costs 
            Else Null End) As 'Costcode1',
        Max(Case
            When Table1.costcode Like '%Costcode2%' 
            Then Table1.costs
            Else Null End) As 'Costcode2',
        Max(Case
            When Table1.costcode Like '%Costcode3%' 
            Then Table1.costs 
            Else Null End) As 'Costcode3',
        (Case
            When Table1.costcode In ('%Costcode1%','%Costcode2%','%Costcode3%')
            Then Sum(Table1.costs)
            Else Null End) As 'Total Cost',

From Table1

最初の 3 つの Case ステートメントは正常に機能し、すべての値が返されます (これらはデータベースに負の値 (-13624.00 など) として保持されます) ですが、Total Cost Case は単に Null を返します...

列 Table1.costcode には他の多くのコードも含まれているため、最初にそれらを選択せず​​にすべての値を合計することはできません。

これらの値を合計するのは簡単なはずですが、明らかに何か不足しています...助けてください:-)

ありがとう

4

1 に答える 1

0

これを試して -

    SUM(Case Table1.costcode
        When LIKE ('%Costcode1%') Then Table1.costs
        When LIKE ('%Costcode2%') Then Table1.costs
        When LIKE ('%Costcode3%') Then Table1.costs
        Then Table1.costs
        Else 0.00 End) As 'Total Cost'
于 2012-05-25T10:23:38.767 に答える