多条件运算时SQL的写法

当需要基于不同条件产生的结果集上运算时,我们可以采用对单一结果集运算后,保持原有结果集,然后统一根据某个字段聚合后来求一些字段的和。

select A.port_no as port_no,

(select max(key_date) from dbo.TblHNWDate) as key_date,
sum(A.pfd_shares) as tot_pfd_shares,
sum(A.par_value) as tot_par_value,
sum(A.cost) as tot_cost,
sum(A.adjusted_cost) as tot_adjusted_cost,
sum(A.market) as tot_market_value,
CASE
when (sum(A.market)-sum(A.adjusted_cost))>0 then ''
else 'N'
end
as tot_net_unrl_gl_s,
(sum(A.market)-sum(A.adjusted_cost)) as tot_net_unrl_gl
from
(
select
port.port_no as port_no,
case
when port.mod_secr_type='05' AND secr.class_code!='934' then port.quantity
else 0
end
as pfd_shares,
case
when port.mod_secr_type='05' AND secr.class_code!='934' then (port.quantity*secr.unit_cash_value)
when port.mod_secr_type='05' AND secr.class_code='934' then (port.quantity*secr.market_price_nuv)
when port.mod_secr_type!='05' then port.quantity
end
as par_value,
port.cost as cost,
port.adjusted_cost as adjusted_cost,
port.market as market
from TblHNWPort port,TblHNWSecr secr
where port.cusip = secr.cusip
and port.key_date = secr.key_date)A
group by A.port_no
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值