首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 各行分配数值的问题之再提问 [已结贴,结贴人:fyming]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fyming
    • 等级:
    发表于:2008-05-16 15:33:46 楼主
    表1:
    流水ID  col_1  col_2 
    1        30   
    2        20
    3        40
    -------------------------------
    表2:
    s_ID  col_1
    1        100
    2,3      50   
    8        300
    9        400
    -------------------------------
    要求:
    流水ID  col_1  col_2 
    1        30      30
    2        20      20
    3        40      30
    ============================
    说明:
    根据表达2中的s_ID与表1关联,这种关联分两种情况,一种是直接关联,比如表2的s_id中的1与表1的流水ID的1对应;另一种是间接关联,比如表2的s_id中的2,3,字符串拆分后为两行的一个结果集(分别是2和3),再与表1的流水ID进行关联。关联之后,根据表2的col_1值,分配给表1,分配原则同俺上一个问题一样(http://topic.csdn.net/u/20080516/13/46d38e03-81eb-407a-85e4-5d33dd013d4c.html)


    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:40:161楼 得分:10
    SQL code
    declare @t table(id int,col_1 int,col_2 int) insert into @t select 1,30,0 insert into @t select 2,20,0 insert into @t select 3,60,0 insert into @t select 4,40,0 declare @num int set @num=100 update @t set col_2=(case when a.id<c.id then a.col_1 else @num-(select sum(col_1) from @t where id<c.id) end) from @t a, (select top 1 * from (select id,cnum=(select sum(col_1) from @t where id<=a.id) from @t a)b where cnum>=@num order by cnum)c where a.id<=c.id select * from @t
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:41:352楼 得分:0
    贴错。。不好意思
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:46:253楼 得分:0
    这个比较麻烦。。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fyming
    • 等级:
    发表于:2008-05-16 15:48:184楼 得分:0
    顺便说一下,如果一条SQL解决不了,分几个语句块亦可,或者配套存储过程、自定义函数(比如折分2,3就可建一个返回表纪录的自定义函数),只求最终解决问题就好。
    另外强调一下,无论是表1还是表2,纪录数都是不一定的,表1并不一定只有3行。

    谢谢兄弟们!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:50:385楼 得分:10
    交给马桶哥做一定ok
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:51:496楼 得分:0
    得10分...白费劲了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:52:127楼 得分:0
    疯子再现
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 15:53:348楼 得分:10
    下面的代码,一条搞定:
    SQL code
    declare @t table(id int identity,c1 int,c2 int) insert @t(c1) select 30 insert @t(c1) select 20 insert @t(c1) select 60 declare @i int set @i=100 update @t set c2 = case when @i-c1>=0 then c1 else c1+@i end,@i = @i - c1 select * from @t /* id c1 c2 ----------- ----------- ----------- 1 30 30 2 20 20 3 60 50 (3 row(s) affected) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fyming
    • 等级:
    发表于:2008-05-16 15:58:589楼 得分:0
    引用 6 楼 wzy_love_sly 的回复:
    得10分...白费劲了

    --------------------------

    您如果帮我搞定,我单独给您100分都行啊,分不是问题。帮忙看看吧
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 16:02:2710楼 得分:10
    SQL code
    if object_id('ta') is not null drop table ta create table ta (ID int,col_1 int,col_2 int) insert into ta select 1,30,null union all select 2,20,null union all select 3,40,null if object_id('tb') is not null drop table tb create table tb (s_ID varchar(10),col_1 int) insert into tb select '1',100 union all select '2,3',50 union all select '8',300 union all select '9',400 update a set col_2 = case when (select sum(col_1) from ta where id <= a.id and charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0) < (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0) then col_1 else (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0) - (select sum(col_1) from ta where id <= a.id and charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)+col_1 end from ta a select * from ta /* ID col_1 col_2 ----------- ----------- -------------- 1 30 30 2 20 20 3 40 30 (所影响的行数为 3 行) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 16:05:5611楼 得分:0
    引用 9 楼 fyming 的回复:
    引用 6 楼 wzy_love_sly 的回复:
    得10分...白费劲了

    --------------------------

    您如果帮我搞定,我单独给您100分都行啊,分不是问题。帮忙看看吧


    Herb2的代码是确是有问题,8楼是我修改过的,应该是没问题了,,,
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 16:12:5412楼 得分:0
    10楼的可以,太强了。小枪。。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ojuju10
    • 等级:
    发表于:2008-05-16 16:26:4813楼 得分:10
    SQL code
    create table t1(Id int,col_1 int,col_2 int) insert into t1(id,col_1) select 1,30 insert into t1(id,col_1) select 2,20 insert into t1(id,col_1) select 3,40 create table t2(Id varchar(10),col_1 int) insert into t2(id,col_1) select 1,100 insert into t2(id,col_1) select '2,3',50 insert into t2(id,col_1) select 8,300 insert into t2(id,col_1) select 9,400 with cet as ( select a.*,b.col_1 as b_col_1 ,num=row_number() over(partition by b.id order by a.id) from t1 a,t2 b where charindex(','+ltrim(a.id)+',',','+b.id+',')>0 ) select a.id,a.col_1 ,col_2= case when (select sum(b_col_1)- sum(col_1) from cet where a.b_col_1=b_col_1 and a.num-1>=num) is null then a.col_1 else (select sum(b_col_1)- sum(col_1) from cet where a.b_col_1=b_col_1 and a.num-1>=num) end from cet a /* id col_1 col_2 ----------- ----------- ----------- 1 30 30 2 20 20 3 40 30 (3 行受影响) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-16 16:32:2614楼 得分:10
    学习
    关注
    帮顶
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ojuju10
    • 等级:
    发表于:2008-05-16 16:34:5415楼 得分:0
    SQL code
    --修正一下 create table t1(Id int,col_1 int,col_2 int) insert into t1(id,col_1) select 1,30 insert into t1(id,col_1) select 2,20 insert into t1(id,col_1) select 3,40 create table t2(Id varchar(10),col_1 int) insert into t2(id,col_1) select 1,100 insert into t2(id,col_1) select '2,3',50 insert into t2(id,col_1) select 8,300 insert into t2(id,col_1) select 9,400 with cet as ( select a.*,b.col_1 as b_col_1 ,num=row_number() over(partition by b.id order by a.id) from t1 a,t2 b where charindex(','+ltrim(a.id)+',',','+b.id+',')>0 ) select a.id,a.col_1 ,col_2= case when (select isnull((sum(b_col_1)- sum(col_1)),0) from cet where a.b_col_1=b_col_1 and a.num-1>=num) >=0 then a.col_1 else (select isnull((sum(b_col_1)- sum(col_1)),0) from cet where a.b_col_1=b_col_1 and a.num-1>=num) end from cet a /* id col_1 col_2 ----------- ----------- ----------- 1 30 30 2 20 20 3 40 30 (3 行受影响) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fyming
    • 等级:
    发表于:2008-05-17 00:02:4116楼 得分:0

    刚从外面回来,看到兄弟们的回贴,谢谢啊!

    15楼[永州之野产异蛇—永州异蛇!]的回贴看不懂,with cet as  ---是什么?

    10楼[无枪狙击手]的答案我放到查询分析器中执行了一下,能通过。就是SQL写得太长了,没看懂。我要慢慢消化一下才行……


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fyming
    • 等级:
    发表于:2008-05-17 09:00:4917楼 得分:0
    TO:10楼[无枪狙击手]
    ========================================================================
    update a
    set col_2 = case when (select sum(col_1) from ta where id <= a.id and
                          charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)
                          < (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)
                    then col_1
                    else (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0) -
                          (select sum(col_1) from ta where id <= a.id and
                          charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)+col_1
                    end
    from ta a
    ===========================================================================

    您的思路我很佩服,但还是有一些问题亚,请帮忙再看看

    比如:
    (1)表1的ID号有可能重复

    drop table ta
    create table ta (ID int,col_1 int,col_2 int)
    insert into ta
    select 1,30,null union all
    select 1,100,null union all
    select 2,20,null union all
    select 3,40,null

    if object_id('tb') is not null
    drop table tb
    create table tb (s_ID varchar(10),col_1 int)
    insert into tb
    select '1',100 union all
    select '2,3',50 union all
    select '8',300 union all
    select '9',400

    (2)表2的s_ID亦有可能重复

    if object_id('ta') is not null
    drop table ta
    create table ta (ID int,col_1 int,col_2 int)
    insert into ta
    select 1,30,null union all
    select 1,100,null union all
    select 2,20,null union all
    select 3,40,null

    if object_id('tb') is not null
    drop table tb
    create table tb (s_ID varchar(10),col_1 int)
    insert into tb
    select '1',100 union all
    select '1',30 union all
    select '2,3',50 union all
    select '8',300 union all
    select '9',400

    ------------------------------------------------
    也就是说,不管表1和表2的数据情况如何,只要通过表2的s_ID 找到表1的ID关联,就可以对表1的关联纪录进行逐行核销。这样的要求,不用游标,能够实现吗?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-17 09:46:5218楼 得分:0
    思路和1楼帖错的那个相似

    复杂的是这个条件比较牛
    charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-17 09:48:1519楼 得分:0
    可以参照一楼代码的来读无枪老大的代码
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-17 09:53:3720楼 得分:0
    不好意思
    看错了

    不是一楼的

    是你上一个贴子里 wzy_love_sly 回复的
    可以参照着读

    http://topic.csdn.net/u/20080516/13/46d38e03-81eb-407a-85e4-5d33dd013d4c.html
    三楼

    then (select sum(col1) from #1 where id <t.id )
    改成
    then (select @i-sum(col1) from #1 where id <t.id )


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-17 10:07:1921楼 得分