今天偶尔看到一题,做了一下

今天偶尔看到一题,网址如下:

http://topic.csdn.net/t/20061023/11/5101874.html

感觉两位的回答是正确的,但总感觉不是很爽,花了点时间,整理了下


with cte as

(
select 1 mo,'收' me,1 nu union all
select 1,'收',2 union all
select 1,'付',2 union all
select 2,'收',1 union all
select 3,'付',1
),tempa as
(
select * ,
case me when '收' then 1 else 2 end row
from (
select mo,me,SUM(nu) nu from cte group by mo,me
) a
)
,ci as
(
select mo,me, nu = isnull((select top 1 nu from tempa c where c.mo = a.mo and c.me = b.me),0) from
(select mo from tempa group by mo ) a,
(select me from tempa group by me ) b
)
select * from ci union
select mo,'存',
sum(case when me = '付' then -nu else + nu end)
from ci group by mo order by mo, me desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值