日期:2014-05-17 浏览次数:20712 次
--1
select [流水号范围]=cast((select min([流水号]) from t1) as varchar(10))+'-'+cast((select max([流水号]) from t1) as varchar(10)),
[总单据数]=(select count(*) from t1),
[有效数]=(select count(*) from t1 where [状态]='有效'),
[合计金额]=(select sum(金额) from t1 where [状态]='有效'),
[现金]=(select sum(t1.金额-isnull(t2.实扣,0)) from t1 left join t2 on t1.[流水号]=t2.[流水号] where t1.[状态]='有效' and t1.[类型] in ('现金','支票'))
[非现金]=(select sum(金额) from t1 where [状态]='有效' and 类型<>'现金')
-(select sum(t1.金额-t2.实扣) from t1,t2 where t1.[流水号]=t2.[流水号] and t1.[状态]='有效' and t1.[类型]='支票')
[无效数]=(select count(*) from t1 where [状态]='作废')
--2
select 类型 as 类型分类,count(*) as 单据数,sum(t1.金额) as 合计,
sum(case when 类型='现金' then t1.金额
when 类型='支票' then t1.金额-isnull(t2.实扣,0) else 0 end as 现金,
sum(case when 类型='支票' then isnull(t2.实扣,0) when 类型='转账' then t1.金额 else 0 end as 非现金
from t1 left join t2 on t1.[流水号]=t2.[流水号 where [状态]='有效'