请教这个SQL怎么写?
create table test
(
提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int
)
insert into test select '120 ', '050 ',1
union all select '120 ', '051 ',2
union all select '120 ', '052 ',3
union all select '120 ', '053 ',4
/*
说明:提出行号和提入行号是指银行的编号,笔数是指票据的数量
就是说:从编号为120的银行提出的票据分别存入了编号为050,051,052,053的银行中
存入的票据数量分别是1,2,3,4
*/
--要得到以下报表
/*
提入行行号 笔数 提入行行号 笔数 提出行行号
050 1 052 3 120
051 2 053 4 120
*/
drop table test
[解决办法]
--排序可能不同,不知道是否能接受
declare @test table
(
提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int
)
insert into @test select '120 ', '050 ',1
union all select '120 ', '051 ',2
union all select '120 ', '052 ',3
union all select '120 ', '053 ',4
union all select '121 ', '053 ',1
union all select '121 ', '051 ',2
union all select '121 ', '054 ',5
union all select '122 ', '053 ',6
select
a.提入银行行号,a.笔数,b.提入银行行号,b.笔数,a.提出银行行号
from @test a
left join @test b
on a.提出银行行号=b.提出银行行号
and (select count(distinct 提入银行行号) from @test where 提出银行行号=b.提出银行行号 and 提入银行行号 <=b.提入银行行号 and 提入银行行号> a.提入银行行号) =1
where (select count(distinct 提入银行行号) from @test where 提出银行行号=a.提出银行行号 and 提入银行行号 <=a.提入银行行号) % 2 =1
--结果
提入银行行号 笔数 提入银行行号 笔数 提出银行行号
---------- ----------- ---------- ----------- ----------
050 1 051 2 120
052 3 053 4 120
051 2 053 1 121
054 5 NULL NULL 121
053 6 NULL NULL 122
(所影响的行数为 5 行)
[解决办法]
create table test(提出银行行号 varchar(10),提入银行行号 varchar(10),笔数 int)
insert into test
select '120 ', '050 ',1
union all select '120 ', '051 ',2
union all select '120 ', '052 ',3
union all select '120 ', '053 ',4
go
select
max(case num%2 when 0 then 提入银行行号 end) as 提入行行号,
max(case num%2 when 0 then 笔数 end) as 笔数,
max(case num%2 when 1 then 提入银行行号 end) as 提入行行号,
max(case num%2 when 1 then 笔数 end) as 笔数,
提出银行行号
from
(select
t.*,
isnull((select count(*) from test
where 提出银行行号=t.提出银行行号 and 提入银行行号 <t.提入银行行号),0) as num
from
test t) a
group by
提出银行行号,(num/2)
go
/*
提入行行号 笔数 提入行行号 笔数 提出银行行号
---------- ----------- ---------- ----------- ----------
050 1 051 2 120
052 3 053 4 120
*/
drop table test
go
[解决办法]
create table test1
(
提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int
)
insert into test1 select '120 ', '050 ',1
union all select '120 ', '051 ',2
union all select '120 ', '052 ',3
union all select '120 ', '053 ',4
select * from test1
declare @t table(提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int,
id int IDENTITY(1,1))
declare @t2 table(提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int,
id int IDENTITY(1,1))
insert @t select * from test1 where 笔数%2 = 0
insert @t2 select * from test1 where 笔数%2 = 1
select a.提入银行行号,a.笔数,b.提入银行行号,b.笔数,a.提出银行行号
from @t b,@t2 a
where a.id = b.id
drop table test1
/*
提入银行行号 笔数 提入银行行号 笔数 提出银行行号
---------- ----------- ---------- ----------- ----------
050 1 051 2 120
052 3 053 4 120
(所影响的行数为 2 行)
*/
[解决办法]
select distinct A.提入银行行号,A.笔数,B.提入银行行号,B.笔数,A.提出银行行号 from test A
left join (select distinct 提出银行行号,提入银行行号,笔数 from test where 笔数%2=1 ) B on A.提出银行行号 =B.提出银行行号
where A.笔数%2=1 and B.笔数%2=1 and B.提入银行行号> A.提入银行行号 and B.笔数> A.笔数
union all
select distinct A.提入银行行号,A.笔数,B.提入银行行号,B.笔数,A.提出银行行号 from test A
left join (select distinct 提出银行行号,提入银行行号,笔数 from test where 笔数%2=0) B on A.提出银行行号 =B.提出银行行号
where A.笔数%2=0 and B.笔数%2=0 and B.提入银行行号> A.提入银行行号 and B.笔数> A.笔数
[解决办法]
create table test
(
提出银行行号 varchar(10),
提入银行行号 varchar(10),
笔数 int
)
insert into test select '120 ', '050 ',1
union all select '120 ', '051 ',2
union all select '120 ', '052 ',3
union all select '120 ', '053 ',4
union all select '120 ', '053 ',5
select identity(int,0,1) id,* into #t from test
select min([提入银行行号])[提入银行行号],
min(case when ([id])%2=0 then [笔数] end)[笔数],
max(case when ([id])%2=1 then [提入银行行号] end)[提入银行行号],
max(case when ([id])%2=1 then [笔数] end)[笔数],[提出银行行号]
from #t a group by 提出银行行号, id/2
drop table #t
drop table test