有两个表格:
一个外借表格,一个归还表格,表格结构如下:
外借表格:外借时间 外借单位 外借数量
2007-01-02 单位1 200
2007-01-02 单位2 200
2007-01-02 单位3 200
2007-01-03 单位1 200
2007-01-03 单位2 200
2007-01-03 单位1 200
2007-02-02 单位3 200
2007-02-02 单位1 200
2007-02-02 单位2 200
2007-02-03 单位3 200
2007-02-03 单位1 200
2007-02-03 单位2 200归还表格:归还时间 归还单位 归还数量
2007-01-02 单位1 100
2007-01-02 单位2 100
2007-01-02 单位3 100
2007-01-03 单位1 100
2007-01-03 单位2 100
2007-01-03 单位1 100
2007-02-02 单位3 100
2007-02-02 单位1 100
2007-02-02 单位2 100
2007-01-03 单位3 100
2007-01-03 单位1 100
2007-01-03 单位2 100组合之后要求数据表格格式如下:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01-02 200 2007-01-02 100 100
单位2 2007-01-02 200 2007-01-02 100 100
单位3 2007-01-02 200 2007-01-02 100 100
合计 ----- 600 ----- 300 300 要求:能够查到每天外借,归还,未归还数量;下面的合计就是每天的;格式如下:例如输入:2007-01-02
显示下列数据:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01-02 200 2007-01-02 100 100
单位2 2007-01-02 200 2007-01-02 100 100
单位3 2007-01-02 200 2007-01-02 100 100
合计 ----- 600 ----- 300 300 能够每月查到外借,归还,未归还数量;下面的合计就是每月的;格式如下:
例如输入:2007-01 显示下列数据:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01 600 2007-01 300 300
单位2 2007-01 400 2007-01 200 200
单位3 2007-01 200 2007-01 100 100
合计 ----- 1200 ----- 600 600
能够每年查到外借,归还,未归还数量;下面的合计就是每年的;例如输入:2007
显示数据如下:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007 1000 2007 500 500
单位2 2007 800 2007 400 400
单位3 2007 600 2007 300 300
合计 ----- 2400 ----- 1200 1200
谢谢各位赐教?感谢!非常感谢!?
一个外借表格,一个归还表格,表格结构如下:
外借表格:外借时间 外借单位 外借数量
2007-01-02 单位1 200
2007-01-02 单位2 200
2007-01-02 单位3 200
2007-01-03 单位1 200
2007-01-03 单位2 200
2007-01-03 单位1 200
2007-02-02 单位3 200
2007-02-02 单位1 200
2007-02-02 单位2 200
2007-02-03 单位3 200
2007-02-03 单位1 200
2007-02-03 单位2 200归还表格:归还时间 归还单位 归还数量
2007-01-02 单位1 100
2007-01-02 单位2 100
2007-01-02 单位3 100
2007-01-03 单位1 100
2007-01-03 单位2 100
2007-01-03 单位1 100
2007-02-02 单位3 100
2007-02-02 单位1 100
2007-02-02 单位2 100
2007-01-03 单位3 100
2007-01-03 单位1 100
2007-01-03 单位2 100组合之后要求数据表格格式如下:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01-02 200 2007-01-02 100 100
单位2 2007-01-02 200 2007-01-02 100 100
单位3 2007-01-02 200 2007-01-02 100 100
合计 ----- 600 ----- 300 300 要求:能够查到每天外借,归还,未归还数量;下面的合计就是每天的;格式如下:例如输入:2007-01-02
显示下列数据:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01-02 200 2007-01-02 100 100
单位2 2007-01-02 200 2007-01-02 100 100
单位3 2007-01-02 200 2007-01-02 100 100
合计 ----- 600 ----- 300 300 能够每月查到外借,归还,未归还数量;下面的合计就是每月的;格式如下:
例如输入:2007-01 显示下列数据:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007-01 600 2007-01 300 300
单位2 2007-01 400 2007-01 200 200
单位3 2007-01 200 2007-01 100 100
合计 ----- 1200 ----- 600 600
能够每年查到外借,归还,未归还数量;下面的合计就是每年的;例如输入:2007
显示数据如下:单位 外借时间 外借数量 归还时间 归还数量 未还数量
单位1 2007 1000 2007 500 500
单位2 2007 800 2007 400 400
单位3 2007 600 2007 300 300
合计 ----- 2400 ----- 1200 1200
谢谢各位赐教?感谢!非常感谢!?
解决方案 »
- 数据库恢复的问题
- mssql里的这些用户是干嘛的,应该删除还是拒绝访问?
- 请教:用OPENROWSET打开别的DB时,中文字段查出来都是乱码,怎么解决啊?
- distinct消除重复数据
- 无法删除表
- 日期格式转换
- 大家可以教教我SQL中的IMAGE字段是怎么用的么?我一直都不知道他到底是做什么的?
- [求助]windows的administrator密码更改后,SQL登录不能。说用户SA登录失败,怎么解决~?~
- 这条SQL语句怎么写?
- 相近SQL语句(记录数接近),在不同库中,查询速度一个快一个慢查,查询快的执行步骤返回的行数和实际行数不一致
- 这种查询该如何写?
- sql2000升级到2005后出现的问题(返回了对列**无效的数据)
外借时间 datetime,
外借单位 varchar(20),
外借数量 int
)
insert into 外借表 select '2007-01-02','单位1',200
union all select '2007-01-02','单位2',200
union all select '2007-01-02','单位3',200
union all select '2007-01-03','单位1',200
union all select '2007-01-03','单位2',200
union all select '2007-01-03','单位1',200
union all select '2007-02-02','单位3',200
union all select '2007-02-02','单位1',200
union all select '2007-02-02','单位2',200
union all select '2007-02-03','单位3',200
union all select '2007-02-03','单位1',200
union all select '2007-02-03','单位2',200create table 归还表(
归还时间 datetime,
归还单位 varchar(20),
归还数量 int
)insert into 归还表 select '2007-01-02','单位1',100
union all select '2007-01-02','单位2',100
union all select '2007-01-02','单位3',100
union all select '2007-01-03','单位1',100
union all select '2007-01-03','单位2',100
union all select '2007-01-03','单位1',100
union all select '2007-02-02','单位3',100
union all select '2007-02-02','单位1',100
union all select '2007-02-02','单位2',100
union all select '2007-01-03','单位3',100
union all select '2007-01-03','单位1',100
union all select '2007-01-03','单位2',100
--查询2007-01-02
select A.单位,A.外借时间,isnull(A.外借数量,0) 外借数量,B.归还时间,isnull(B.归还数量,0) 归还数量,isnull(A.外借数量,0)-isnull(B.归还数量,0) 未还数量
from
(select 外借单位 as 单位,convert(varchar(10),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where 外借时间='2007-01-02' group by 外借单位,外借时间) A
join
(select 归还单位 as 单位,convert(varchar(10),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where 归还时间='2007-01-02' group by 归还单位,归还时间) B
on A.单位=B.单位 and A.外借时间=B.归还时间
union all
select '合计',' ---- ',sum(A.外借数量) 外借数量,' ---- ',sum(B.归还数量),sum(A.外借数量)-sum(B.归还数量)
from
(select 外借单位 as 单位,convert(varchar(10),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where 外借时间='2007-01-02' group by 外借单位,外借时间) A
join
(select 归还单位 as 单位,convert(varchar(10),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where 归还时间='2007-01-02' group by 归还单位,归还时间) B
on A.单位=B.单位 and A.外借时间=B.归还时间
drop table 外借表,归还表
select A.单位,A.外借时间,isnull(A.外借数量,0) 外借数量,B.归还时间,isnull(B.归还数量,0) 归还数量,isnull(A.外借数量,0)-isnull(B.归还数量,0) 未还数量
from
(select 外借单位 as 单位,convert(varchar(7),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where convert(varchar(7),外借时间,120)='2007-01' group by 外借单位,convert(varchar(7),外借时间,120)) A
join
(select 归还单位 as 单位,convert(varchar(7),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where convert(varchar(7),归还时间,120)='2007-01' group by 归还单位,convert(varchar(7),归还时间,120)) B
on A.单位=B.单位 and A.外借时间=B.归还时间
union all
select '合计',' ---- ',sum(A.外借数量) 外借数量,' ---- ',sum(B.归还数量),sum(A.外借数量)-sum(B.归还数量)
from
(select 外借单位 as 单位,convert(varchar(7),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where convert(varchar(7),外借时间,120)='2007-01' group by 外借单位,convert(varchar(7),外借时间,120)) A
join
(select 归还单位 as 单位,convert(varchar(7),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where convert(varchar(7),归还时间,120)='2007-01' group by 归还单位,convert(varchar(7),归还时间,120)) B
on A.单位=B.单位 and A.外借时间=B.归还时间
select A.单位,A.外借时间,isnull(A.外借数量,0) 外借数量,B.归还时间,isnull(B.归还数量,0) 归还数量,isnull(A.外借数量,0)-isnull(B.归还数量,0) 未还数量
from
(select 外借单位 as 单位,convert(varchar(4),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where convert(varchar(4),外借时间,120)='2007' group by 外借单位,convert(varchar(4),外借时间,120)) A
join
(select 归还单位 as 单位,convert(varchar(4),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where convert(varchar(4),归还时间,120)='2007' group by 归还单位,convert(varchar(4),归还时间,120)) B
on A.单位=B.单位 and A.外借时间=B.归还时间
union all
select '合计',' ---- ',sum(A.外借数量) 外借数量,' ---- ',sum(B.归还数量),sum(A.外借数量)-sum(B.归还数量)
from
(select 外借单位 as 单位,convert(varchar(4),外借时间,120) 外借时间,sum(外借数量) 外借数量 from 外借表 where convert(varchar(4),外借时间,120)='2007' group by 外借单位,convert(varchar(4),外借时间,120)) A
join
(select 归还单位 as 单位,convert(varchar(4),归还时间,120) 归还时间,sum(归还数量) 归还数量 from 归还表 where convert(varchar(4),归还时间,120)='2007' group by 归还单位,convert(varchar(4),归还时间,120)) B
on A.单位=B.单位 and A.外借时间=B.归还时间
insert 外借表格 select '2007-01-02', '单位1', 200
insert 外借表格 select '2007-01-02', '单位2', 200
insert 外借表格 select '2007-01-02', '单位3', 200
insert 外借表格 select '2007-01-03', '单位1', 200
insert 外借表格 select '2007-01-03', '单位2', 200
insert 外借表格 select '2007-01-03', '单位1', 200
insert 外借表格 select '2007-02-02', '单位3', 200
insert 外借表格 select '2007-02-02', '单位1', 200
insert 外借表格 select '2007-02-02', '单位2', 200
insert 外借表格 select '2007-02-03', '单位3', 200
insert 外借表格 select '2007-02-03', '单位1', 200
insert 外借表格 select '2007-02-03', '单位2', 200
gocreate table 归还表格(归还时间 datetime, 归还单位 nvarchar(10), 归还数量 int)
insert 归还表格 select '2007-01-02', '单位1', 100
insert 归还表格 select '2007-01-02', '单位2', 100
insert 归还表格 select '2007-01-02', '单位3', 100
insert 归还表格 select '2007-01-03', '单位1', 100
insert 归还表格 select '2007-01-03', '单位2', 100
insert 归还表格 select '2007-01-03', '单位1', 100
insert 归还表格 select '2007-02-02', '单位3', 100
insert 归还表格 select '2007-02-02', '单位1', 100
insert 归还表格 select '2007-02-02', '单位2', 100
insert 归还表格 select '2007-01-03', '单位3', 100
insert 归还表格 select '2007-01-03', '单位1', 100
insert 归还表格 select '2007-01-03', '单位2', 100
go
select
[时间]=外借时间,
[单位]=外借单位,
[数量]=sum(外借数量),
Flag=0
into #
from 外借表格 group by 外借时间,外借单位
union all
select
归还时间,
归还单位,
[数量]=sum(归还数量),
Flag=1
from
归还表格 t group by 归还时间,归还单位go--drop table #select
[单位],
[外借时间]=max(case when Flag=0 then convert(varchar(10),时间,120) else '' end),
[归还时间]=max(case when Flag=1 then convert(varchar(10),时间,120) else '' end),
[外借数量]=max(case when Flag=0 then 数量 else 0 end),
[归还数量]=max(case when Flag=1 then 数量 else 0 end),
[未还数量]=(select sum(case when Flag=0 then 数量 else -数量 end) from # where [单位]=t.[单位] and 时间!>t.时间)
,时间=convert(varchar(10),时间,120)into #1---生成临时#1按天统计
from
# t
group by [单位],convert(varchar(10),时间,120)--
select
[单位],
[外借时间]=max(case when Flag=0 then convert(varchar(7),时间,120)else '' end),
[归还时间]=max(case when Flag=1 then convert(varchar(7),时间,120) else '' end),
[外借数量]=max(case when Flag=0 then 数量 else 0 end),
[归还数量]=max(case when Flag=1 then 数量 else 0 end),
[未还数量]=(select sum(case when Flag=0 then 数量 else -数量 end) from # where [单位]=t.[单位] and convert(varchar(7),时间,120)!>convert(varchar(7),t.时间,120))
,时间=convert(varchar(7),时间,120)into #2---生成临时#1按月统计
from
# t
group by [单位],convert(varchar(7),时间,120)go
select
[单位],
[外借时间]=max(case when Flag=0 then rtrim(year(时间)) else '' end),
[归还时间]=max(case when Flag=1 then rtrim(year(时间)) else '' end),
[外借数量]=max(case when Flag=0 then 数量 else 0 end),
[归还数量]=max(case when Flag=1 then 数量 else 0 end),
[未还数量]=(select sum(case when Flag=0 then 数量 else -数量 end) from # where [单位]=t.[单位] and year(时间)!>year(t.时间))
,时间=year(时间)into #3---生成临时#1按年统计
from
# t
group by [单位],year(时间)go---每天
select
[单位]=isnull(单位, '合计'),
外借时间=case when grouping(单位)=1 then '' else max(convert(varchar(10),时间,120))end,
归还时间=case when grouping(单位)=1 then '' else max(convert(varchar(10),时间,120))end,
[外借数量]=sum(外借数量),
[归还数量]=sum(归还数量),
[未还数量]=max(未还数量),
[时间]=case when grouping(单位)=1 then '' else max(convert(varchar(10),时间,120))end
from
#1
where
时间='2007-01-02'
group by 单位 with rollup--每月
select
[单位]=isnull(单位, '合计'),
外借时间=case when grouping(单位)=1 then '' else max(时间)end,
归还时间=case when grouping(单位)=1 then '' else max(时间)end,
[外借数量]=sum(外借数量),
[归还数量]=sum(归还数量),
[未还数量]=max(未还数量),
[时间]=case when grouping(单位)=1 then '' else max(时间)end
from
#2
where
时间='2007-01'
group by 单位 with rollup-------------
select
[单位]=isnull(单位, '合计'),
外借时间=case when grouping(单位)=1 then '' else max(时间)end,
归还时间=case when grouping(单位)=1 then '' else max(时间)end,
[外借数量]=sum(外借数量),
[归还数量]=sum(归还数量),
[未还数量]=max(未还数量),
[时间]=case when grouping(单位)=1 then '' else max(时间)end
from
#3
where
时间='2007'
group by 单位 with rollup
go
单位 外借时间 归还时间 外借数量 归还数量 未还数量 时间
---------- ---------- ---------- ----------- ----------- ----------- ----------
单位1 2007-01-02 2007-01-02 200 100 100 2007-01-02
单位2 2007-01-02 2007-01-02 200 100 100 2007-01-02
单位3 2007-01-02 2007-01-02 200 100 100 2007-01-02
合计 600 300 100 (所影响的行数为 4 行)单位 外借时间 归还时间 外借数量 归还数量 未还数量 时间
---------- ------- ------- ----------- ----------- ----------- -------
单位1 2007-01 2007-01 400 300 200 2007-01
单位2 2007-01 2007-01 200 200 100 2007-01
单位3 2007-01 2007-01 200 100 0 2007-01
合计 800 600 200 (所影响的行数为 4 行)单位 外借时间 归还时间 外借数量 归还数量 未还数量 时间
---------- ----------- ----------- ----------- ----------- ----------- -----------
单位1 2007 2007 400 300 500 2007
单位2 2007 2007 200 200 400 2007
单位3 2007 2007 200 100 300 2007
合计 0 0 800 600 500 0(所影响的行数为 4 行)--drop table #,#1,#2,#3,归还表格,外借表格
create table ret(retdate datetime,broid varchar(12),retnum int)insert into bro values('2007-01-02', '单位1' ,'200')
insert into bro values('2007-01-02', '单位2' ,'200')
insert into bro values('2007-01-02', '单位3' ,'200')
insert into bro values('2007-01-03', '单位1' ,'200')
insert into bro values('2007-01-03', '单位2' ,'200')
insert into bro values('2007-01-03', '单位1' ,'200')
insert into bro values('2007-02-02', '单位3' ,'200')
insert into bro values('2007-02-02', '单位1' ,'200')
insert into bro values('2007-02-02', '单位2' ,'200')
insert into bro values('2007-02-03', '单位3' ,'200')
insert into bro values('2007-02-03', '单位1' ,'200')
insert into bro values('2007-02-03', '单位2' ,'200') insert into ret values('2007-01-02', '单位1' ,'100')
insert into ret values('2007-01-02', '单位2' ,'100')
insert into ret values('2007-01-02', '单位3' ,'100')
insert into ret values('2007-01-03', '单位1' ,'100')
insert into ret values('2007-01-03', '单位2' ,'100')
insert into ret values('2007-01-03', '单位1' ,'100')
insert into ret values('2007-02-02', '单位3' ,'100')
insert into ret values('2007-02-02', '单位1' ,'100')
insert into ret values('2007-02-02', '单位2' ,'100')
insert into ret values('2007-01-03', '单位3' ,'100')
insert into ret values('2007-01-03', '单位1' ,'100')
insert into ret values('2007-01-03', '单位2' ,'100')
select b.*,r.retdate,r.retnum,b.bronum-r.retnum as want from
(
select broid,convert(varchar(10),outdate,120)as outdate,sum(bronum)as bronum from bro
where outdate='2007-01-02'
group by broid,outdate
) as b,
(
select broid,convert(varchar(10),retdate,120)as retdate,sum(retnum)as retnum from ret
where retdate='2007-01-02'
group by broid,retdate
) as r
where b.broid=r.broidunion allselect '合计','--------',sum(b.bronum),'-------- ',sum(r.retnum),sum(b.bronum)-sum(r.retnum) from
(
select broid,convert(varchar(10),outdate,120)as outdate,sum(bronum)as bronum from bro
where outdate='2007-01-02'
group by broid,outdate
) as b,
(
select broid,convert(varchar(10),retdate,120)as retdate,sum(retnum)as retnum from ret
where retdate='2007-01-02'
group by broid,retdate
) as r
where b.broid=r.broid
--convert(varchar(7),outdate,120)='2007-01'drop table bro
drop table ret
年改称dateadd(year,1,@sDate)create table jtable(jdate datetime,company nvarchar(10),jcount int)
create table gtable(gdate datetime,company nvarchar(10),gcount int)insert into jtable select '2007-01-02', '单位1',100
insert into jtable select '2007-01-02', '单位2', 100
insert into jtable select '2007-01-02', '单位3', 100
insert into jtable select '2007-01-03', '单位1', 100
insert into jtable select '2007-01-03', '单位2', 100
insert into jtable select '2007-01-03', '单位1', 100
insert into jtable select '2007-02-02', '单位3', 100
insert into jtable select '2007-02-02', '单位1', 100
insert into jtable select '2007-02-02', '单位2', 100
insert into jtable select '2007-02-03', '单位3', 100
insert into jtable select '2007-02-03', '单位1', 100
insert into jtable select '2007-02-03', '单位2', 100 insert into gtable select '2007-01-02', '单位1',100
insert into gtable select '2007-01-02', '单位2', 100
insert into gtable select '2007-01-02', '单位3', 100
insert into gtable select '2007-01-03', '单位1', 100
insert into gtable select '2007-01-03', '单位2', 100
insert into gtable select '2007-01-03', '单位1', 100
insert into gtable select '2007-02-02', '单位3', 100
insert into gtable select '2007-02-02', '单位1', 100
insert into gtable select '2007-02-02', '单位2', 100
insert into gtable select '2007-02-03', '单位3', 100
insert into gtable select '2007-02-03', '单位1', 100
insert into gtable select '2007-02-03', '单位2', 100 declare @sDate datetime
declare @eDate datetime
set @eDate=(select max(jdate) from jtable)
set @sDate=(Select min(jdate) from jtable)
while @sDate <= @eDate
begin
if exists(select * from jtable where jdate=@sDate)
begin
select a.* from (select jtable.company as '单位', jtable.jdate as '外借时间', Sum(jtable.jcount) as '外借数量',
gtable.gdate as '归还时间', Sum(gtable.gcount) as '归还数量', Sum(jtable.jcount-gtable.gcount) as '未还数量'
from jtable,gtable
where jtable.jdate=gtable.gdate
and jtable.company=gtable.company
and jtable.jdate=@sDate
group by jtable.company, jtable.jdate,gtable.gdate WITH CUBE
) a
where
( 单位 is null
and 外借时间 is null
and 归还时间 is null)
or
(单位 is not null
and 外借时间 is not null
and 归还时间 is not null)
end
set @sDate = dateadd(day,1,@sDate)
end单位 外借时间 外借数量 归还时间 归还数量 未还数量
---------- ----------------------- ----------- ----------------------- ----------- -----------
单位1 2007-01-02 00:00:00.000 100 2007-01-02 00:00:00.000 100 0
单位2 2007-01-02 00:00:00.000 200 2007-01-02 00:00:00.000 100 100
单位3 2007-01-02 00:00:00.000 200 2007-01-02 00:00:00.000 100 100
NULL NULL 500 NULL 300 200(4 row(s) affected)单位 外借时间 外借数量 归还时间 归还数量 未还数量
---------- ----------------------- ----------- ----------------------- ----------- -----------
单位1 2007-01-03 00:00:00.000 800 2007-01-03 00:00:00.000 400 400
单位2 2007-01-03 00:00:00.000 200 2007-01-03 00:00:00.000 100 100
NULL NULL 1000 NULL 500 500(3 row(s) affected)单位 外借时间 外借数量 归还时间 归还数量 未还数量
---------- ----------------------- ----------- ----------------------- ----------- -----------
单位1 2007-02-02 00:00:00.000 200 2007-02-02 00:00:00.000 100 100
单位2 2007-02-02 00:00:00.000 200 2007-02-02 00:00:00.000 100 100
单位3 2007-02-02 00:00:00.000 200 2007-02-02 00:00:00.000 100 100
NULL NULL 600 NULL 300 300(4 row(s) affected)单位 外借时间 外借数量 归还时间 归还数量 未还数量
---------- ----------------------- ----------- ----------------------- ----------- -----------
单位1 2007-02-03 00:00:00.000 200 2007-02-03 00:00:00.000 100 100
单位2 2007-02-03 00:00:00.000 200 2007-02-03 00:00:00.000 100 100
单位3 2007-02-03 00:00:00.000 200 2007-02-03 00:00:00.000 100 100
NULL NULL 600 NULL 300 300(4 row(s) affected)