问题题面没有看懂,"按照d排序在用 s和m依次比较"表达个什么意思不明白.

解决方案 »

  1.   

    给大家解释下:
            上面有两个表@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
      

  2.   

    给大家解释下: 
            上面有两个表@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 
      

  3.   

    declare @a table (a int,b int,s int)
    declare @b table (a int,b int,c int,d int,m int)
    insert @a
    select 1,2,20
    union all
    select 2,3,15insert @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,10select * from @a
    select * from @b-- drop table #temp
    -- select * from #tempselect * from (
    select n1.a,n1.b,n1.c,n1.d
    ,(case when             
                ((select             
                  sum(s)             
                from @a             
                where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b     
                )-                      
                         isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))>=0               
                              then n1.m          
               else               
                                case when n1.m +((select             
                      sum(s)   from @a             
                 where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b           
                      ) -              
                                isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))>=0              
               then  n1.m +((select             
                      sum(s)   from @a             
                 where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b           
                      ) -              
                                         isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))               
                     else 0             
                   end               
                     end  )mm         
         from @b n1 ,@a n2              
         where     
          n1.a=n2.a
    and n1.b=n2.b 
    )bb
    where bb.mm>0  

         
      

  4.   

    declare @a table (a int,b int,s int)
    declare @b table (a int,b int,c int,d int,m int)
    insert @a
    select 1,2,20
    union all
    select 2,3,15insert @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,10select * from @a
    select * from @b-- drop table #temp
    -- select * from #tempselect * from (
    select n1.a,n1.b,n1.c,n1.d
    ,(case when             
                ((select             
                  sum(s)             
                from @a             
                where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b     
                )-                      
                         isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))>=0               
                              then n1.m          
               else               
                                case when n1.m +((select             
                      sum(s)   from @a             
                 where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b           
                      ) -              
                                isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))>=0              
               then  n1.m +((select             
                      sum(s)   from @a             
                 where  a=n1.a and b=n1.b
    and a=n2.a and b=n2.b           
                      ) -              
                                         isnull((select             
                   sum(m) from @b             
                 where a=n1.a and b=n1.b
    and a=n2.a and b=n2.b          
                 and c<=n1.c            
                  and d<=n1.d
    ),0))               
                     else 0             
                   end               
                     end  )mm         
         from @b n1 ,@a n2              
         where     
          n1.a=n2.a
    and n1.b=n2.b 
    )bb
    where bb.mm>0  

         结果不是按最大的开始比较的大家指点下::。