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'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章