0

我在 MySQL 添加三个值时遇到问题,这应该很简单吧?

我有根据第二列的值从列中选择值的代码,我使用这样的 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

前三个 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 回答