wwwwww
create table t1(A varchar(10),B varchar(50),C varchar(50),D int )insert t1 select
'aa','aa0001','aa0100',100 insert t1 select
'aa','aa0501','aa0800',300 insert t1 select
'aa','aa0401','aa0450',50 insert t1 select
'aa','aa0801','aa0950',150 insert t1 select
'aa','aa0200','aa0300',101
create table t2 (da samlldatetime ,A varchar(10),B varchar(50),C varchar(50),D int )
insert t2 select
'2007-10-1','aa','aa0001','aa1000',1000 insert t2 select
'2007-10-2','aa','aa1001','aa2000',1000 insert
我在t2表中加了一列日期列,所以我要的结果是:
da,A,B,C,D
2007-10-1,aa,aa00101,aa00199,99
2007-10-1,aa,aa00301,aa00400,400
2007-10-1,aa,aa00451,aa00500,50
2007-10-1,aa,aa00951,aa01000,50
2007-10-2,aa,aa01001,aa02000,1000
能看懂么?
--------------------编程问答--------------------
--恩,將da加上去就可以了
create table t1(A varchar(10),B varchar(50),C varchar(50),D int )
insert t1 select 'aa ', 'aa0001', 'aa0100',100
insert t1 select 'aa ', 'aa0501', 'aa0800',300
insert t1 select 'aa ', 'aa0401', 'aa0450',50
insert t1 select 'aa ', 'aa0801', 'aa0950',150
insert t1 select 'aa ', 'aa0200', 'aa0300',101
create table t2 (da datetime ,A varchar(10),B varchar(50),C varchar(50),D int )
insert t2 select '2007-10-1 ', 'aa ', 'aa0001', 'aa1000',1000
insert t2 select '2007-10-2 ', 'aa ', 'aa1001', 'aa2000',1000
select top 10000 id=identity(int,1,1) into #
from syscolumns a,syscolumns b,syscolumns c
select D.da,D.id,D.a,stuff(D.b,len(D.b)-3,4,right(100000+id,4)) as b,stuff(D.c,len(D.c)-3,4,right(100000+id,4)) as c,0 as d
into #t
from
(
select *
from #
left join t2
on #.id<=convert(int,right(t2.c,4)) and #.id>=convert(int,right(t2.b,4))
where a is not null
) D
left join t1
on D.id<=convert(int,right(t1.c,4)) and D.id>=convert(int,right(t1.b,4)) and D.a=t1.a
where t1.a is null
create index temp_ix on #t(a,id)
select *, convert(int,right(c,4))-convert(int,right(b,4))+1 as d
from
(select da, a,min(b) as b,max(c) as c
from
(
select tmp=(select count(*) from #t a where a.a=#t.a and a.id<=#t.id),* from #t
) T
group by da,a,(id-tmp)) X
order by a
/*
2007-10-01 00:00:00.000 aa aa0101 aa0199 99
2007-10-01 00:00:00.000 aa aa0301 aa0400 100
2007-10-01 00:00:00.000 aa aa0451 aa0500 50
2007-10-01 00:00:00.000 aa aa0951 aa1000 50
2007-10-02 00:00:00.000 aa aa1001 aa2000 1000
*/
drop table t1,t2
drop table #t ,#
补充:VB , 资源