sqlserver裏,case when和cast函數一起使用

sqlserver裏,查詢order_id,amount,如果Transaction_Amount< Order_Amount 就返回 Order_Amount,否則就返回Transaction_Amount,並且除以一百保留兩位小數的sql

select order_id, 
case when  Transaction_Amount < Order_Amount 
then cast((Order_Amount/100.0) as decimal(12,2))
else cast((Transaction_Amount/100.0) as decimal(12,2))
end as  amount
from xxxx
where  Transaction_Amount != Order_Amount
order by Transaction_Time desc
offset 0 rows fetch next 50 rows only

在上述的問題上在加上sum計算

select isNull(cast(SUM((case when (Order_Amount > Transaction_Amount) then Order_Amount
            else Transaction_Amount end -  Refund_Amount))/100.0  as decimal(12,2)),0) as amount
from xxxx
where Insert_Time > '2020-04-16 00:00' and Insert_Time < '2020-04-16 12:00'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章