简单的T-sql查询

时间:2012-06-22 06:23:21

标签: sql-server-2008 tsql

enter image description here

从上面的示例数据中,我想编写一个查询,如果ExpRow值= 1则将SalesAmount返回为0.0,如果ExpRow值= 2,则返回ExpenseAmount为0.0

2 个答案:

答案 0 :(得分:1)

SELECT
    ExpRow,
    ExpenseDate,
    CASE WHEN ExpRow = 2 THEN 0 ELSE ExpenseAmount END AS ExpenseAmount,
    CASE WHEN ExpRow = 1 THEN 0 ELSE SalesAmount END AS SalesAmount
FROM
    Table

答案 1 :(得分:0)

不确定这是否与您完全相同,但是......

SELECT CASE([ExpRow]) WHEN 1 THEN 0 ELSE SalesAmount END AS 'SalesAmount',
       CASE([ExpRow]) WHEN 2 THEN 0 ELSE ExpenseAmount END AS 'ExpenseAmount'
FROM [YOUR_TABLE]