【发布时间】:2012-05-25 10:17:28
【问题描述】:
我在 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 列还包含许多其他代码,所以我不能在不先将它们挑出的情况下将所有值相加。
对这些值求和一定很简单,但显然我遗漏了一些东西……请帮助:-)
谢谢
【问题讨论】:
-
去掉总成本后多余的逗号。
-
抱歉打错了 - 实际的 sql 在此之后选择其他内容。