2015-06-25 90 views
0
SELECT name , 
     (SUM(CASE WHEN TransTypeName LIKE 'credit%' THEN amount 
        ELSE 0 
       END) - SUM(CASE WHEN TransTypeName LIKE 'Debit%' THEN amount 
           ELSE 0 
         END)) * 5/100 AS Interest 
    FROM ..... 

此查詢返回一些負值,我想將負值轉換爲零。 如何編寫一個查詢如何將負值轉換爲零

+1

呃,負值爲負....借方(貸方)爲什麼會是零? –

+1

嗨米奇,我明白你的問題,但這是我的要求任務。 –

+0

任何有這種荒謬要求的東西一定是功課。 –

回答

4

像這樣的事情可能?

SELECT t1.name AS Name, CASE WHEN t1.Interest > 0 THEN Interest ELSE 0 END AS Interest 
FROM (select name, (sum(case when TransTypeName like 'credit%' then amount else 0 end) - sum(case when TransTypeName like 'Debit%' then amount else 0 end)) *5/100 as Interest from ...........) as t1 
3

使用第二個case語句:

select name, (sum(case when TransTypeName like 'credit%' then (case when amount>0 then amount else 0 end;) else 0 end) - 
sum(case when TransTypeName like 'Debit%' then (case when amount>0 then amount else 0 end;) else 0 end)) *5/100 as Interest 
from ........... 

,如果你不什麼返回了一加一是負面的:

select name, (case when (sum(case when TransTypeName like 'credit%' then amount else 0 end) - 
sum(case when TransTypeName like 'Debit%' then amount) else 0 end)) *5/100)<0 then 0 else (sum(case when TransTypeName like 'credit%' then amount else 0 end) - 
sum(case when TransTypeName like 'Debit%' then amount) else 0 end)) *5/100) as Interest 
from ........... 
0
SELECT t1.name AS Name, CASE WHEN t1.Interest > 0 THEN Interest ELSE 0 END AS Interest 
FROM (select name, (sum(case when TransTypeName like 'credit%' then amount else 0 end) - sum(case when TransTypeName like 'Debit%' then amount else 0 end)) *5/100 as Interest from ...........) as t1 
相關問題