分类: Oracle
2008-06-27 08:55:18
Sql中Case用法:
select
a.customer_id,
a.currency_id,
b.balance,
isnull(sum(-c.amount),0) credit_amount,
isnull(sum(d.amount),0) debit_amount,
isnull(sum(case when a.invoice_date < 0 then a.amount end),0) amount0,
isnull(sum(case when a.invoice_date BETWEEN 0 and 30 then a.amount end),0) amount30,
isnull(sum(case when a.invoice_date BETWEEN 31 and 60 then a.amount end),0) amount60,
isnull(sum(case when a.invoice_date BETWEEN 61 and 90 then a.amount end),0) amount90
from
(select
customer_id,currency_id,
datediff(day,getdate(),
invoice_date)+payment_terms invoice_date,
amount_total-amount_settled amount
from customer_invoice) a
join customer_current_account b
on a.customer_id = b.customer_id
and a.currency_id= b.currency_id
left join customer_credit_note c
on a.customer_id = c.customer_id
and a.currency_id = c.currency_id
left join customer_debit_note d
on a.customer_id = d.customer_id
and a.currency_id = d.currency_id
group by a.customer_id,a.currency_id,b.balance
Oracle中的Case与Sql差不多:
SELECT LogID,UserID,LogTime,Url,Description,(
case
when OperateType = 0
then '新增'
when OperateType=1
then '修改'
else '删除'
end ) OperationName
FROM LOG
也可以用Oracle中的 Decode 函数
·含义解释:
decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
SELECT LogID,UserID,LogTime,Url,Description,
decode(OperateType,0,'新增',1,'修改','删除') OperationName
FROM LOG