二月以前欠费的没考虑 先写个简单的select HouseID(房号),
sum(case when type = 1 then 金额 else 0 end) [2月应收],
sum(case when type = -1 then 金额 else 0 end) [2月已收],
sum(金额*type) [2月结余]
(select HouseID(房号), PayDate (应收日期) as date, payValue(应收额) as 金额 ,costItem(费用类型) ,coststart(费用开始), costClose(费用截止) ,type = 1
from 应收表
union all
select HouseID(房号),costDate (收款日期) ,costValue(收款额) ,costItem(费用类型) ,coststart(费用开始) ,costClose(费用截止) ,type = -1
from 收款表 )T where costClose <='2009-02-10' and convert(varchar(7),coststart,120) ='2009-02'
group by HouseID(房号)
sum(case when type = 1 then 金额 else 0 end) [2月应收],
sum(case when type = -1 then 金额 else 0 end) [2月已收],
sum(金额*type) [2月结余]
(select HouseID(房号), PayDate (应收日期) as date, payValue(应收额) as 金额 ,costItem(费用类型) ,coststart(费用开始), costClose(费用截止) ,type = 1
from 应收表
union all
select HouseID(房号),costDate (收款日期) ,costValue(收款额) ,costItem(费用类型) ,coststart(费用开始) ,costClose(费用截止) ,type = -1
from 收款表 )T where costClose <='2009-02-10' and convert(varchar(7),coststart,120) ='2009-02'
group by HouseID(房号)
解决方案 »
- 无法发现microsoft sql server native client
- 如何把A表的a字段的数据复制到B表的b字段中去?a字段和b字段的字段名和数据类型一样.
- sql2005数据库 delete操作突然变慢 是什么原因
- 【Sql Server API】: 哪个API函数可以取得当前的存储过程返回的总记录条数?
- 该事务对象与连接对象无关联
- 请教这样的sql 语句该如何写?
- sql2000导入sql2005问题,,急
- 获取未建立连接关系的记录
- 比如数据库TABLE有个字段Remark , Remark 有:城要888工 , 类似这样有数字有字符的数据, 要怎么把这些有字段的只要数字取出来。
- 关于结存的触发器如何写?
- 关于配置变量@@ERROR
- sql2000的数据库如何转入mySQL
go
create table [应收表]([PactID] int,[HouseID] varchar(3),[PayDate] datetime,[payValue] int,[costItem] varchar(4),[coststart] datetime,[costClose] varchar(10))
insert [应收表]
select 1,'A01','2009-01-01',5000,'租金','2009-01-01','2009-01-31' union all
select 1,'A01','2009-02-01',5000,'租金','2009-02-01','2009-02-28' union all
select 1,'A01','2009-03-01',5000,'租金','2009-03-01','2009-03-31' union all
select 1,'A01','2009-04-01',5000,'租金','2009-04-01','2009-04-31' union all
select 2,'A02','2009-01-15',8000,'租金','2009-01-15','2009-04-14'
go
if object_id('[收款表]') is not null drop table [收款表]
go
create table [收款表]([SCID] int,[HouseID] varchar(3),[costDate] datetime,[costValue] int,[costItem] varchar(4),[coststart] datetime,[costClose] varchar(10))
insert [收款表]
select 1,'A01','2009-01-08',2000,'租金','2009-01-01','2009-01-31' union all
select 2,'A01','2009-01-15',3000,'租金','2009-01-01','2009-01-31' union all
select 3,'A01','2009-02-01',4000,'租金','2009-02-01','2009-02-28' union all
select 4,'A01','2009-02-15',1000,'租金','2009-02-01','2009-02-28' union all
select 5,'A01','2009-03-01',5000,'租金','2009-03-01','2009-03-31' union all
select 6,'A01','2009-04-01',5000,'租金','2009-04-01','2009-04-31'
go
--select * from [应收表]
--select * from [收款表]
declare @dt datetime
set @dt='2009-02-10'
--set @dt='2009-02-16'select a.HouseID
,[2月前欠费]=a.[2月前总应收]-isnull(b.[2月前总已收],0)
,a.[2月应收]
,isnull(b.[2月已收],0) [2月已收]
,[2月结余]=a.[2月底总应收]-isnull(b.[2月底总已收],0)
from
(
select HouseID,[2月应收]=sum(case when month(PayDate)=month(@dt) then payValue else 0 end)
,[2月前总应收]=sum(case when month(PayDate)<month(@dt) then payValue else 0 end)
,[2月底总应收]=sum(case when month(PayDate)<=month(@dt) then payValue else 0 end)
from 应收表
where year(PayDate)=year(@dt) and month(PayDate)<=month(@dt)
group by HouseID
) a
left join
(
select HouseID,[2月已收]=sum(case when month(costDate)=month(@dt) and costDate<=@dt then costValue else 0 end)
,[2月前总已收]=sum(case when month(costDate)<month(@dt) then costValue else 0 end)
,[2月底总已收]=sum(case when month(costDate)<=month(@dt) and costDate<=@dt then costValue else 0 end)
from 收款表
where year(costDate)=year(@dt) and month(costDate)<=month(@dt)
group by HouseID
) b
on a.HouseID=b.HouseID--@dt='2009-02-10'
/*
HouseID 2月前欠费 2月应收 2月已收 2月结余
------- ----------- ----------- ----------- -----------
A01 0 5000 4000 1000
A02 8000 0 0 8000(2 行受影响)
*/--@dt='2009-02-16'
/*
HouseID 2月前欠费 2月应收 2月已收 2月结余
------- ----------- ----------- ----------- -----------
A01 0 5000 5000 0
A02 8000 0 0 8000(2 行受影响)
*/