用SQL如何实现这样的对减呢?

czx33859066 2013-02-18 10:45:00
有两个表如下
表A
ID TYPE PH1 PH2
11 01 00000001 00000010
21 02 00100001 00200010
31 03 00000001 00000001

表B
ID TYPE PH1 PH2
11 01 00000001 00000005
21 02 00100101 00100110
31 02 00100201 00100210

两个表的票号(PH1、PH2)根据类型(TYPE)去对减得到如下结果:
TYPE PH1 PH2
01 00000006 00000010
02 00100001 00100100
02 00100111 00100200
02 00100211 00200010
03 00000001 00000001

求高手支招啊。。。
...全文
305 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2013-02-19
  • 打赏
  • 举报
回复
不想写了 。太多了。
--小F-- 2013-02-19
  • 打赏
  • 举报
回复
budong0000 2013-02-19
  • 打赏
  • 举报
回复

create table #tba (id int identity(1,1),type int,p1 int,p2 int) 
create table #tbb (id int identity(1,1),type int,p1 int,p2 int)  
 insert into #tba select 1,1,10 union all select 2,2,100    
 insert into #tbb select 1,1,5 union all select 2,6,9  union all select 2,11,22 
 
 DECLARE @n AS BIGINT;
SET @n = 1000;

WITH
L0   AS(SELECT 1 AS c UNION ALL SELECT 1),
L1   AS(SELECT 1 AS c FROM L0 AS A, L0 AS B),
L2   AS(SELECT 1 AS c FROM L1 AS A, L1 AS B),
L3   AS(SELECT 1 AS c FROM L2 AS A, L2 AS B),
L4   AS(SELECT 1 AS c FROM L3 AS A, L3 AS B),
L5   AS(SELECT 1 AS c FROM L4 AS A, L4 AS B),
Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY c) AS n FROM L5)
SELECT n into #Nums FROM Nums WHERE n <= @n;

CREATE index #ix_n_Nums on #Nums(n)
GO
  
 
 --DELETE from   #tba
 --DELETE from  #tbb
 
 select * FROM #tba
 select * FROM #tbb
  
	SELECT type, n.n
	from #tba a
	inner join #Nums n
	on n.n BETWEEN a.p1 and a.p2
	EXCEPT 
	SELECT type, n.n
	from #tbb b
	inner join #Nums n
	on n.n BETWEEN b.p1 and b.p2
-------------------------只能帮到这了
type	n
1	6
1	7
1	8
1	9
1	10
2	2
2	3
2	4
2	5
2	10
2	23
2	24
2	25

czx33859066 2013-02-19
  • 打赏
  • 举报
回复
引用 10 楼 vv75251455 的回复:
SQL code ? 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 create table tba (id int identity(……
我运行了存储过程后,提示有错误哦 服务器: 消息 8101,级别 16,状态 1,过程 P_tbatbb,行 3 仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#' 中为标识列指定显式值。 服务器: 消息 8101,级别 16,状态 1,过程 P_tbatbb,行 18 仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#' 中为标识列指定显式值。 服务器: 消息 209,级别 16,状态 1,过程 P_tbatbb,行 18 列名 'type' 不明确。
發糞塗牆 2013-02-19
  • 打赏
  • 举报
回复
有空看看,现在有事做。
czx33859066 2013-02-19
  • 打赏
  • 举报
回复
引用 13 楼 DBA_Huangzj 的回复:
楼主,说白了你这个就是找出不连号的数据而已。。。
嗯,可以这么理解。。有没有好的方法啊?
czx33859066 2013-02-19
  • 打赏
  • 举报
回复
引用 19 楼 fredrickhu 的回复:
不想写了 。太多了。
看起来有点类似,但还是不一样的。
czx33859066 2013-02-18
  • 打赏
  • 举报
回复
坐等大牛啊。。。
發糞塗牆 2013-02-18
  • 打赏
  • 举报
回复
楼主,说白了你这个就是找出不连号的数据而已。。。
vv75251455 2013-02-18
  • 打赏
  • 举报
回复
type p1 p2 ----------- ----------- ----------- 1 1 1 1 4 5 1 10 10 2 2 5 2 10 100 (5 行受影响)
vv75251455 2013-02-18
  • 打赏
  • 举报
回复
简化了你的问题
vv75251455 2013-02-18
  • 打赏
  • 举报
回复

create table tba
(id int identity(1,1),type int,p1 int,p2 int)
create table tbb
(id int identity(1,1),type int,p1 int,p2 int)

insert into tba
select 1,1,10
union all select 2,1,100 

insert into tbb
select 1,2,3
union all select 1,6,9 

create proc P_tbatbb
as 
begin
  create table #
  (
    id int identity(1,1),
    type int,
    p1 int,
    p2 int
  )
  insert  into #
          select  type,p1,p2
          from    (
                    select  a.id fid,b.id,a.type,a.p1,b.p1 - 1 [p2]
                    from    tba a
                    join    tbb b
                            on b.type = a.type
                    union all
                    select  a.id,b.id,a.type,b.p2 + 1,a.p2
                    from    tba a
                    join    tbb b
                            on b.type = a.type
                  ) aa
          order by type,fid,id,p1,p2
  insert  into #
          select  a.type,a.p1,a.p2
          from    tba a
          left join tbb b
                  on b.type = a.type
          where   b.id is null
          order by type

  declare @p1 int,
    @p2 int,
    @type int,
    @p int
  select  @p1 = 0,@p2 = 0,@type = 1,@p = 0

  update  aa
  set     @p = case when @type <> type
                         or (@type = type and @p1 < p1) then p1
                    else @p1
               end,@p1 = p1,@p2 = p2,@type = type,p1 = @p
  from    # aa

  select  type,p1,min(p2) [p2]
  from    #
  where   p1 <= p2
  group by type,p1
  order by type

end

go

P_tbatbb


--insert into tbb
--select 2,1,1
--union all select 2,6,9 


czx33859066 2013-02-18
  • 打赏
  • 举报
回复
引用 7 楼 sz_haitao 的回复:
引用 4 楼 czx33859066 的回复: 引用 3 楼 sc273607742 的回复:想试试,但真没看懂是怎么对减的... 表A ID TYPE PH1 PH2 11 01 00000001 00000010 表B ID TYPE PH1 PH2 11 01 00000001 000000……
表A里面跟表B里面有交叉了。。把交叉的部分排除掉, A:1-10 B:1-5 结果是:6-10这个就不在B表里了 这样说能看懂了吗?
czx33859066 2013-02-18
  • 打赏
  • 举报
回复
引用 6 楼 sc273607742 的回复:
引用 4 楼 czx33859066 的回复: 引用 3 楼 sc273607742 的回复: 想试试,但真没看懂是怎么对减的... 表A ID TYPE PH1 PH2 11 01 00000001 00000010 表B ID TYPE PH1 PH2 11 01 00000001 0000……
表A ID TYPE PH1 PH2 11 01 1 10 表B ID TYPE PH1 PH2 11 01 1 5 表A里面跟表B里面有交叉了。。把交叉的部分排除掉, A:1-10 B:1-5 结果是:6-10这个就不在B表里了 这样说能看懂了吗?
haitao 2013-02-18
  • 打赏
  • 举报
回复
引用 4 楼 czx33859066 的回复:
引用 3 楼 sc273607742 的回复:想试试,但真没看懂是怎么对减的... 表A ID TYPE PH1 PH2 11 01 00000001 00000010 表B ID TYPE PH1 PH2 11 01 00000001 00000005 结果就剩下 ……
1 1 => 6 10 5 => 10 ?
哥眼神纯洁不 2013-02-18
  • 打赏
  • 举报
回复
引用 4 楼 czx33859066 的回复:
引用 3 楼 sc273607742 的回复: 想试试,但真没看懂是怎么对减的... 表A ID TYPE PH1 PH2 11 01 00000001 00000010 表B ID TYPE PH1 PH2 11 01 00000001 00000005 结果就剩下 ID TYPE ……
还是没看懂...
czx33859066 2013-02-18
  • 打赏
  • 举报
回复
引用 3 楼 sc273607742 的回复:
想试试,但真没看懂是怎么对减的...
就是把表A里面跟表B里面重复的给去掉
czx33859066 2013-02-18
  • 打赏
  • 举报
回复
引用 3 楼 sc273607742 的回复:
想试试,但真没看懂是怎么对减的...
表A ID TYPE PH1 PH2 11 01 00000001 00000010 表B ID TYPE PH1 PH2 11 01 00000001 00000005 结果就剩下 ID TYPE PH1 PH2 11 01 00000006 00000010
哥眼神纯洁不 2013-02-18
  • 打赏
  • 举报
回复
想试试,但真没看懂是怎么对减的...
czx33859066 2013-02-18
  • 打赏
  • 举报
回复
大牛们,帮帮忙啊

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧