根据类别自动匹配相同金额的问题
- SQL code
前面一贴:http://topic.csdn.net/u/20120516/21/aa23e8d3-9255-48ec-ae18-3dcaa8931e4e.html已经解决了目前的问题,在实际应用过程中又出现了一个新问题就是前面再加上“类别”一列,类别a与b 如果两个类别金额相同自动匹配为1 不匹配为0如果有多个a与b a与b的金额也要一一对应才能自动顺延匹配实际要显示的效果如下:类别 金额 备注a 10 1b 10 1a 20 0a 20 0a 20 0a 30 0a 40 1b 40 1a 40 0a 50 1b 50 1a 50 1b 50 1
[解决办法]
- SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([类别] varchar(1),[金额] int,[备注] int)insert [test]select 'a',10,null union allselect 'b',10,null union allselect 'a',20,null union allselect 'a',20,null union allselect 'a',20,null union allselect 'a',30,null union allselect 'a',40,null union allselect 'b',40,null union allselect 'a',40,null union allselect 'a',50,null union allselect 'b',50,null union allselect 'a',50,null union allselect 'b',50,nullalter table test add id int identitygoupdate testset [备注]=case when id in (select * from(select a.id from test a full join test bon a.id=b.id+1where a.类别<>b.类别 and a.金额=b.金额unionselect b.id from test a full join test bon a.id=b.id+1where a.类别<>b.类别 and a.金额=b.金额)m) then 1 else 0 endalter table test drop column idgoselect * from test/*类别 金额 备注----- -------- -------------a 10 1b 10 1a 20 0a 20 0a 20 0a 30 0a 40 1b 40 1a 40 1--这里我不明白你的为什么是0??a 50 1b 50 1a 50 1b 50 1*/解释一下嘛
[解决办法]
- SQL code
alter table tb add px int identitygoupdate tb set 备注=1where 类别='a' and exists(select 1 from tb t where t.类别='b' and t.px=tb.px+1)or类别='b' and exists(select 1 from tb t where t.类别='a' and t.px=tb.px-1)goalter table tb drop column pxgoselect * from tb/**类别 金额 备注---- ----------- -----------a 10 1b 10 1a 20 0a 20 0a 20 0a 30 0a 40 1b 40 1a 40 0a 50 1b 50 1a 50 1b 50 1(13 行受影响)**/