declare @发放表 table (
起始号码 varchar(20),
终止号码 varchar(20),
执收单位 varchar(20),
稽查部门 varchar(20),
年号     varchar(10),
票据种类 int,
入库时间 datetime
)
insert @发放表
select 
'0000100','0001000','中天公司','第四部','2006',1,'2006-1-1'   
union all select
'0000001','0002000','中天公司','第四部','2006',2,'2006-1-2'   
union all select 
'0001001','0003000','北京现代','第三部','2006',1,'2006-1-3'   declare @核销表 table (
起始号码 varchar(20),
终止号码 varchar(20),
执收单位 varchar(20),
稽查部门 varchar(20),
年号     varchar(10),
票据种类 int,
发放时间 datetime
)
insert @核销表
select 
'0000325','0000600','中天公司','第四部','2006',1,'2006-1-1'   
union all select
'0000001','0000009','中天公司','第四部','2006',2,'2006-1-2'   
union all select
'0000900','0001200','中天公司','第四部','2006',2,'2006-1-3'  
union all select
'0001200','0001800','北京现代','第三部','2006',1,'2006-1-4'
union all select
'0002400','0002900','北京现代','第三部','2006',1,'2006-1-4'   select *,IDENTITY(int,1,1) as id
 into #t
from (
select distinct 起始号码 as 号码,执收单位,稽查部门,年号,票据种类  from @发放表
union 
select distinct 起始号码,执收单位,稽查部门,年号,票据种类 from @核销表
union 
select distinct right('00000'+cast(cast(起始号码 as int)-1 as varchar(10)),7),执收单位,稽查部门,年号,票据种类 from @核销表
union 
select distinct 终止号码,执收单位,稽查部门,年号,票据种类 from @发放表
union 
select distinct 终止号码,执收单位,稽查部门,年号,票据种类 from @核销表
union 
select distinct right('00000'+cast(cast(终止号码 as int)+1 as varchar(10)),7),执收单位,稽查部门,年号,票据种类 from @核销表
) as t
order by 执收单位,稽查部门,年号,票据种类,号码
select t.*
from (
select a.号码 as 起始号码,b.号码 as 终止号码,a.票据种类,a.执收单位,a.稽查部门,a.年号 from #t a,#t b
where a.票据种类=b.票据种类 and a.执收单位=b.执收单位 and a.稽查部门=b.稽查部门  and a.年号=b.年号
and b.id=a.id+1
and cast(b.号码 as int)-cast(a.号码 as int)>1
) as t
left join @发放表 r
on t.票据种类=r.票据种类 and t.执收单位=r.执收单位 and t.稽查部门=r.稽查部门  and t.年号=r.年号 and t.起始号码>=r.起始号码 and t.终止号码<=r.终止号码
left join @核销表 c
on t.票据种类=c.票据种类 and t.执收单位=c.执收单位 and t.稽查部门=c.稽查部门  and t.年号=c.年号 and t.起始号码>=c.起始号码 and t.终止号码<=c.终止号码
where r.票据种类 is not null
and c.票据种类 is null
and r.执收单位 is not null
and c.执收单位 is null
and r.稽查部门 is not null
and c.稽查部门 is null
and r.年号 is not null
and c.年号 is nulldrop table #t根据需要结合数据库写了这个SQL可以计算出我想要的数据,可是当我把表名和字段名换成实际的表名和字段名后就无法使用!
表名就是上面定义的拼音缩写,换了后算出来的东西就不是我想要的!

解决方案 »

  1.   

    不好意思来晚了!
    select *,IDENTITY(int,1,1) as id
     into #t
    from (
    select distinct qshm as 号码,zsdw,jcbm,nh,pjzl,ffsj from pjffb
    union 
    select distinct qshm,zsdw,jcbm,nh,pjzl,hxsj from pjhxb
    union 
    select distinct right('00000'+cast(cast(qshm as int)-1 as varchar(10)),7),zsdw,jcbm,nh,pjzl,hxsj from pjhxb
    union 
    select distinct zzhm,zsdw,jcbm,nh,pjzl,ffsj from pjffb
    union 
    select distinct zzhm,zsdw,jcbm,nh,pjzl,hxsj from pjhxb
    union 
    select distinct right('00000'+cast(cast(zzhm as int)+1 as varchar(10)),7),zsdw,jcbm,nh,pjzl,hxsj from pjhxb
    ) as t
    order by zsdw,jcbm,nh,pjzl,号码
    select t.*
    from (
    select a.号码 as qshm,b.号码 as zzhm,a.pjzl,a.zsdw,a.jcbm,a.nh,a.ffsj from #t a,#t b
    where a.pjzl=b.pjzl and a.zsdw=b.zsdw and a.jcbm=b.jcbm  and a.nh=b.nh
    and b.id=a.id+1
    and cast(b.号码 as int)-cast(a.号码 as int)>1
    ) as t
    left join pjffb r
    on t.pjzl=r.pjzl and t.zsdw=r.zsdw and t.jcbm=r.jcbm  and t.nh=r.nh and t.qshm>=r.qshm and t.zzhm<=r.zzhm
    left join pjhxb c
    on t.pjzl=c.pjzl and t.zsdw=c.zsdw and t.jcbm=c.jcbm  and t.nh=c.nh and t.qshm>=c.qshm and t.zzhm<=c.zzhm
    where r.pjzl is not null
    and c.pjzl is null
    and r.zsdw is not null
    and c.zsdw is null
    and r.jcbm is not null
    and c.jcbm is null
    and r.nh is not null
    and c.nh is nulldrop table #t这是修改后的.如果去掉JCBM这个字段就可以算出来了,可是这是个关键条件字段!并且如果仍是使用上面游标定义的表也可以算出来,就是不能用到实际的库里!
    那位高手能告诉我怎么回事!
    再三感谢!
      

  2.   

    什么出错信息都没有,就是查出来的数据不对。如果去掉JCBM就可以了,可是这个不能去呀!
    大哥,终于把你等来了!
      

  3.   

    0000001 0000022 ttt aa 第四部 2006 2006-08-08 15:51:54.000
    0000023 0000093 ttt bb 第四部 2006 2006-08-08 15:51:54.000
    0000235 0001100 ttt bb 第四部 2006 2006-08-28 12:04:40.000
    这是用上面语句查出来的。0000014 0000022 ttt aa 第四部   2006 2006-08-22 11:17:47.000
    0000023 0000055 ttt bb 第四部 2006 2006-08-08 15:51:54.000
    0000079 0000089 ttt bb 第四部   2006 2006-08-23 09:19:56.000
    0000235 0000653 ttt bb 第四部 2006 2006-08-28 12:04:40.000
    0000895 0001100 ttt bb 第四部   2006 2006-08-30 14:49:06.000这是去掉JCBM后查出来正确的!0 2006-08-08 15:51:54.000 bb 0 ttt 0000023 0001100 科长 科长 第四部 NULL NULL 1 20061 2006-08-08 15:51:54.000 aa 0 ttt 0000001 0000022 科长 科长 第四部 NULL NULL 22 2006这是发放表1 2006-08-22 11:13:15.000 aa ttt 0000001 0000005 稽查 zzj 第四部   2006
    2 2006-08-22 11:17:47.000 aa ttt 0000006 0000013 稽查 luo 第四部   2006
    5 2006-08-23 09:19:56.000 bb ttt 0000056 0000078 稽查 zzj 第四部   2006
    6 2006-08-23 09:20:33.000 bb ttt 0000090 0000093 稽查 zzj 第四部   2006
    9 2006-08-28 12:04:40.000 bb ttt 0000094 0000234 稽查 zzj 第四部 2006
    11 2006-08-30 14:49:06.000 bb ttt 0000654 0000894 稽查 zzj 第四部   2006
    这是核销表
    摆脱各位高手看看是怎么回事!谢谢各位了!
      

  4.   

    02006-08-08 15:51:54.000  bb  0 ttt 0000023 0001100 科长 科长 第四部 NULL NULL 1 2006
    发放表12006-08-22 11:13:15.000 aa ttt 0000001 0000005 稽查 zzj 第四部  2006
    核销表
    有点乱,主要是 ttt是票据种类 aa和bb是执收单位 第四部是稽查部门 2006是年号 还有起始号码和结束号码,其他无关紧要!
      

  5.   

    既然使用测试表结果正确,那么语句应该没问题.由于测试表中没有NULL值,则可能是实际数据表中的NULL值在作怪.
    请楼主检查一下实际数据表的NULL值,然后相应地调整一下SQL语句或实际数据表的NULL值试试.
      

  6.   

    我没有取表中有NULL值的字段,也没有对他们做任何操作。这也有关么?
      

  7.   

    实在搞不明白,我已经把有NULL值的字段都改了还是不行!
    为什么不能用呢?
      

  8.   

    我靠!是数据库的问题,最简单的查询都出错!
    select * from pjhxb
    1 2006-08-22 11:13:15.000 aa ttt 0000001 0000005 稽查 zzj aa   2006
    2 2006-08-22 11:17:47.000 aa ttt 0000006 0000013 稽查 luo aa   2006
    5 2006-08-23 09:19:56.000 bb ttt 0000056 0000078 稽查 zzj aa   2006
    6 2006-08-23 09:20:33.000 bb ttt 0000090 0000093 稽查 zzj aa   2006
    9 2006-08-28 12:04:40.000 bb ttt 0000094 0000234 稽查 zzj aa 2006
    11 2006-08-30 14:49:06.000 bb ttt 0000654 0000894 稽查 zzj aa   2006
    12 2006-08-30 14:49:06.000 cc ttt 0001230 0001280 稽查
             zzj aa 2006select * from pjhxb where jcbm='aa'9 2006-08-28 12:04:40.000 bb ttt 0000094 0000234 稽查 zzj aa 2006
    12 2006-08-30 14:49:06.000 cc ttt 0001230 0001280 稽查 zzj aa 2006
    这算什么意思?
      

  9.   

    select * from pjhxb where jcbm='aa'
    jcbm什么类型??
      

  10.   

    TMD原来是数据后面跟了一个看不见的东西,用DELETE向后删了几下就好了,不知道是什么但绝对不是空格!