其实这也不是一个算法,自己总结了下把它作为一个算法。

在很多地方很广泛的应用。多应用于逐条比较,自上而下或是自下而上。

这种可以拥游标实现但是却非常的慢。

个人总结了几种情况的应用附带例子说明:

declare @a table (a int,b int,s int)declare @b table (a int,b int,c int,d int,m int)insert @aselect 1,2,20union allselect 2,3,15

insert @bselect 1,2,3,9,5union allselect 1,2,4,10,12union allselect 1,2,5,11,10union allselect 1,2,6,12,5union allselect 2,3,4,6,10union allselect 2,3,5,7,5union allselect 2,3,6,8,10

@a表a           b           s           ----------- ----------- ----------- 1           2           202           3           15

@b表

a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           3           9           51           2           4           10          121           2           5           11          101           2           6           12          52           3           4           6           102           3           5           7           52           3           6           8           10

现在做以下说明:具体要求如下:参照@a表中的字段 a,b去@b表中找相同的数据例:@a表中a           b           s           ----------- ----------- ----------- 1           2           20

在@b表中相对对应的数据有a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           3           9           51           2           4           10          121           2           5           11          101           2           6           12          5

然后再以@b标为基准,按照d从小到大,用@a中的s=20逐条和@b表中的m进行比较第一条   20>5   该数据不取出第二条   15>12该数据不取出第三条   3<10该数据取出,m被更新成7第四条    数据直接取出

结果为:a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           5           11          71           2           6           12          5

例子如上;;;;;;;;;;;;;;

整个过程执行完最后的结果为:a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           5           11          71           2           6           12          52           3           6           8           10

解决方法一:

select * from (select n1.a,n1.b,n1.c,n1.d        ,(case when                         ((select             sum(s)             from @a             where     a=n1.a and b=n1.band a=n2.a and b=n2.b                 )-                      isnull((select             sum(m) from @b             where a=n1.a and b=n1.band a=n2.a and b=n2.b          and c<=n1.c            and d<=n1.d            ),0))>=0               then 0          else               case when n1.m +((select             sum(s)   from @a             where     a=n1.a and b=n1.band a=n2.a and b=n2.b                             ) -              isnull((select             sum(m) from @b             where a=n1.a and b=n1.band a=n2.a and b=n2.b          and c<=n1.c            and d<=n1.d            ),0))>=0              then  -((select             sum(s)   from @a             where     a=n1.a and b=n1.band a=n2.a and b=n2.b                             ) -              isnull((select             sum(m) from @b             where a=n1.a and b=n1.band a=n2.a and b=n2.b          and c<=n1.c            and d<=n1.d            ),0))               else n1.m             end               end  )mm         from @b n1 ,@a n2              where           n1.a=n2.aand n1.b=n2.b     )bbwhere bb.mm>0

解决方法二:

select b.a,b.b,b.c,b.d,case when b.summ-b.m>a.s then b.m else b.summ-a.s end as mfrom @a a,(select x.*,(select sum(m) from @b where a=x.a and b=x.b and d<=x.d) as Summfrom @b x    ) bwhere a.a=b.aand a.b=b.band b.summ>a.s

例二:

declare @a table (a int,b int,s int)declare @b table (a int,b int,c int,d int,m int)insert @aselect 1,2,20union allselect 2,3,15

insert @bselect 1,2,3,9,5union allselect 1,2,4,10,12union allselect 1,2,5,11,10union allselect 1,2,6,12,5union allselect 2,3,4,6,10union allselect 2,3,5,7,5union allselect 2,3,6,8,10

@a表a           b           s           ----------- ----------- ----------- 1           2           202           3           15

@b表

a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           3           9           51           2           4           10          121           2           5           11          101           2           6           12          52           3           4           6           102           3           5           7           52           3           6           8           10

给大家解释下:         上面有两个表@a,@b,大家都能看出来共同的字段是 a,b       但是下面的@b表中字段还有c,d 操作过程为         以a,b为连接条件将两表连接,明显看出在@b表中的数据会有多条 

        然后以@b标为基准把两表中a,b字段相同的数据依次按照d字段降序从大到小,逐条进行比较         最后小于0的去掉。 

举例 @a表 中 a          b          s          ----------- ----------- ----------- 1          2          20 表@b中 与其相关的数据为 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          9          5 1          2          4          10          12 1          2          5          11          10 1          2          6          12          5 

按照d降序@b中数据为 a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          12          5 1          2          4          11          10 1          2          5          10          12 1          2          6          9          5 组条比较用@a中的s和@b中的m比较 则第一条        20>5第一条保留   比较第二条    15>10 第二条保留   比较第三条    5 <12  第三条仍然把保留  但是m数量标为5   第四条      就直接不用要了 

结果就是下面 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          12          5 1          2          4          11          10 1          2          5          10          5 

最终结果:a           b           c           d           m           ----------- ----------- ----------- ----------- ----------- 1           2           4           10          51           2           5           11          101           2           6           12          52           3           5           7           52           3           6           8           10

解决方法一:

select b.a,b.b,b.c,b.d,case when b.summ>a.s then a.s-b.summ+b.m else b.m end as mfrom @a a,(select x.*,(select sum(m) from @b where a=x.a and b=x.b and d>=x.d) as Summfrom @b x    ) bwhere a.a=b.aand a.b=b.band b.summ-b.m<a.sorder by b.a,b.b,b.d desc

解决方法二:

select * into # from (select b.a,b.b,c,d,m=case when (select sum(m) from @b where a=b.a and d<=b.d)>a.sthen (select sum(m) from @b where a=b.a and d<=b.d)-a.s else 0 end from @b b,@a awhere a.a=b.a)a where m!=0

select a,b,c,d,m=a.m-(select isnull(sum(m),0) from # where a=a.a and d<a.d) from # a

解决方法三:

Select     M.a,M.b,M.c,M.d,    m=Case When ISNULL(N.s-(Select SUM(m) From @b Where a=M.a AND b=M.b AND d>M.d),M.m)>=M.m Then M.m Else ISNULL(N.s-(Select SUM(m) From @b Where a=M.a AND b=M.b AND d>M.d),M.m)EndFrom @a N Left Join @b MOn N.a =M.a And N.b=m.b Where ISNULL(N.s-(Select SUM(m) From @b Where a=M.a AND b=M.b AND d>M.d),M.m)>0Order By m.d Desc

例三:

declare @a table (a int,b int,d int,s int) declare @b table (a int,b int,c int,d int,m int) insert @a select 1,2,10,-20 union all select 2,3,7,-15 union all select 3,8,6,-50 

insert @b select 1,2,3,9,5 union all select 1,2,4,10,12 union all select 1,2,5,11,10 union all select 1,2,6,12,5 union all select 2,3,4,6,10 union all select 2,3,5,7,5 union all select 2,3,6,8,10  

@a表 

a          b          d          s          ----------- ----------- ----------- ----------- 1          2          10          -20 2          3          7          -15 3          8          6          -50  

@b表 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          9          5 1          2          4          10          12 1          2          5          11          10 1          2          6          12          5 2          3          4          6          10 2          3          5          7          5 2          3          6          8          10    

现做以下说明: 还是和上面的一样,开始参照@a表中的 a,b两个字段 在@b表中找到与其相关的列 不同的是@a表中加上了一列d 那么在@b表中找相关列时就要找在@b表中d字段>=@a表中的d字段的记录。 

然后逐条进行比较 举例: @a表 

a          b          d          s          ----------- ----------- ----------- ----------- 1          2          10          -20 

@b表 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          9          5 1          2          4          10          12 1          2          5          11          10 1          2          6          12          5 

先找符合条件看的列  @a表中d=10a那么 @b表中符合条件的列为 @b表 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          9          5 1          2          4          10          12        

先用@a表中的s列=-20与@b表中的m列逐条进行比较。 结果为: 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          4          10          -3 1          2          5          11          10 1          2          6          12          5 ----------------------------------------------------------------------------------------------------------------------- 但是如果把 @a表中改为 a          b          d          s          ----------- ----------- ----------- ----------- 1          2          11          -20 结果将变为 a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          5          11          7 1          2          6          12          5 

特别注意一种情况: 像@a表中在@b表中没有相关项的情况 应该插入到b表中。 例 @a表 

a          b          d          s          ----------- ----------- ----------- ----------- 3          8          6          -50 

结果为 a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 3          8                      6          -50 

------------------------------------------------------------------------------------------------------------------ 最终计算结果如下 @a表      

a          b          d          s                        ----------- ----------- ----------- ----------- 1          2          10          -20 2          3          7          -15 3          8          6          -50  

@b表 

a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          3          9          5 1          2          4          10          12 1          2          5          11          10 1          2          6          12          5 2          3          4          6          10 2          3          5          7          5 2          3          6          8          10 || || 结果为 a          b          c          d          m          ----------- ----------- ----------- ----------- ----------- 1          2          4          10          -3 1          2          5          11          10 1          2          6          12          5 2          3          6          8          10 3          8                      6          -50 

解答一:

select b.a,b.b,b.c,b.d,case when b.summ+a.s>b.m then b.m else b.summ+a.s end as minto #tempfrom @a a,(select x.*,(select sum(m) from @b where a=x.a and b=x.b and d<=x.d) as Summfrom @b x    ) bwhere a.a=b.aand a.b=b.b--and b.summ>a.sand b.d<=a.d

select * from #temp awhere not exists(select 1 from #temp where a=a.a and b=a.b and m>a.m)and a.m<>0union allselect n.* from @b n,@a mwhere         n.a=m.aand         n.b=m.band         n.d>m.dunion allselect         m.a        ,m.b        ,null        ,m.d        ,m.sfrom @a mwhere not exists(select 1 from #temp where a=m.a and b=m.b)order by a        ,dasc

drop table #temp

转载于:https://www.cnblogs.com/wequst/archive/2008/10/09/1307472.html

先进先出或是先进后出算法相关推荐

  1. 操作系统 FIFO 先进先出页面置换算法

    FIFO 先进先出页面置换算法 根据作业序列判断置换,先进先置换的原则. 实现过程: 用vector简单模拟这个过程,不用直接queue模拟,是因为,当判断是否需要置换的时候,queue不好判断在队列 ...

  2. 队列和堆栈 --- 先进先出和先进后出

    private static void queue()//  队列 { Queue<String> queue  = new LinkedList<>(); queue.off ...

  3. JavaScript模拟实现先进先出、先进后出效果

    JavaScript模拟实现先进先出.先进后出效果 JavaScript模拟实现先进先出.先进后出效果

  4. 先进先出页面置换算法详解

    *先进先出(First In first Out,FIFO) 页面置换算法的基本思想: **每次置换最先调入内存的页面,即将内存中等待时间最长的页面进行置换.此算法的适用范围是顺序结构程序. 基本原理 ...

  5. Java:实现先进先出缓存FIFO算法(附完整源码)

    Java:实现先进先出缓存FIFO算法 import java.util.HashMap; import java.util.Map.Entry; public class FIFOCache {pu ...

  6. 先进先出(FIFO)置换算法

    定义 这是最早出现的置换算法.该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰.该算法实现简单,只需把一个进程已调入内存的页面,按先后次序链接成一个队列,并设置一个指针,称为 ...

  7. 单向链表的创建与遍历(先进先出和先进后出)

    先进先出:输入任意一串不为零的数,并建立和前一方向不同的单向链表,并按照先进先出的原则依次输出. #include <iostream> #include <cstdio> # ...

  8. 【学习笔记】数据结构之单链表(先进先出、先进后出)

    先看下数据结构中一种重要的数据存储形式,链表,下面两段是来自百度百科:        链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列 ...

  9. 先进先出页面置换算法的模拟(c++实现)

    实验要求 1)设计模拟实现OPT.FIFO和LRU页面置换算法中的任意一种. OPT算法:需要发生页面置换时,算法总是选择在将来最不可能访问的页面进行置换. FIFO算法:算法总是选择在队列中等待时间 ...

最新文章

  1. golang reflect
  2. 强大Jquery插件,table排序之二
  3. Asp.net動態添加控件(转)
  4. 过滤html标签的代码
  5. 第13讲nbsp;日期和时间nbsp;EXCEL2010…
  6. 十五、MySQL变量(系统变量、自定义变量)相关知识总结
  7. 微盟合作,重磅推出全免费的H5专业营销平台,快速创建第一个H5活动(捷微H5)
  8. EasyUI权限系列(一星权限)
  9. 通过 Azure 媒体管理门户开始使用直播流媒体
  10. 常见的几种最优化方法总结
  11. 使用Jquery.flot插件时出现“例外被抛出且未被接住”
  12. Kali渗透Windows Server 2003
  13. 织梦系统参数设置出现Token mismatch!提示的解决办法
  14. 视频封面如何消重 修改视频md5 百度网盘
  15. 基于Keras2《面向小数据集构建图像分类模型》——Kaggle猫狗数据集
  16. 下载没有种子链接用特征码下片的方法
  17. 优雅地从浏览器打开本地应用
  18. 【锁屏】 Ubuntu20.04 锁屏快捷键无反应的解决方法
  19. 基础//页面布局——三栏布局1
  20. 全能IDE VsCode

热门文章

  1. org.aspectj.weaver.ResolvedType$Array cannot be cast to org.aspectj.weaver.ReferenceType
  2. linux有桌面有的没桌面_Linux桌面如何成长
  3. 《小狗钱钱》 读后感
  4. 使用水印云,一键去除图片去水印!
  5. 建站四部曲之后端接口篇(SpringBoot+上线)
  6. 第四部分:特殊用途的句子——第一章:强调
  7. 【包】R语言rvest包简介
  8. springboot添加多个properties或者yml配置文件
  9. Java实践(十一)——五子棋
  10. sxs.exe病毒手动删除方法