使用If语句计算列

时间:2014-09-11 17:59:48

标签: sql

我有一个包含这些列的表:

用户名 TotalTransactions SKU

我想创建一个名为Over:

的自动生成列

如果SKU是abc和TotalTransactions> 6,000,然后Over是真的:

UserName        TotalTransactions  Sku  Over
joe@mysite.com  6001               abc  True

如何使用If语句添加计算列?这是我的代码:

select settings.UserName, SUM(TransactionCount) as TransactionCount, settings.SettingValue as Sku,
CASE WHEN settings.SettingValue = 'abc' AND TransactionCount > 6000 
 THEN 'True' ELSE 'False' END As Over
from jma_qb_trans_count trans
Inner Join jma_settings settings on settings.UserName = trans.UserName
where settings.SettingName = 'QB:JMAWebStoreSku' and settings.SettingValue != ''
Group by settings.UserName, settings.SettingValue, Over
Order By settings.UserName

当我使用此代码时,收到此错误:

Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'Over'.

1 个答案:

答案 0 :(得分:1)

我相信你正在寻找(添加到Select语句):

CASE WHEN SKU = 'abc' AND TotalTransactions > 6000 
     THEN 'True' ELSE 'False' END AS Over