ERP中组套的问题
表一:zutaotbl
dalei xiaolei
1 11
1 12
1 13
2 12
2 13
3 11
3 12
3 14
表二:shuliangtbl
cinvcode iquantity
11 100
12 200
13 200
14 200
怎么写一个查询语句,对产品1,2,3按顺序进行组套,即先组套产品1,再组套2,最后剩下的小类产品组套产品3。最终结果为:
产品 数量
1 100
2 100
3 0
小类 数量
11 0
12 0
13 0
14 200
请高手赐教。
------解决方案--------------------
- SQL code
--原始数据:@1declare @1 table(dalei int,xiaolei int)insert @1select 1,11 union allselect 1,12 union allselect 1,13 union allselect 2,12 union allselect 2,13 union allselect 3,11 union allselect 3,12 union allselect 3,14--原始数据:@2declare @2 table(cinvcode int,iquantity int)insert @2select 11,100 union allselect 12,200 union allselect 13,200 union allselect 14,200select *,cn=cast(null as int) into #T from @1declare @dalei intdeclare cur_dalei cursor for select dalei from #T group by daleiopen cur_daleifetch next from cur_dalei into @daleiwhile @@fetch_status=0begin update #T set cn=(select min(b.iquantity) from #T a,@2 b where [email protected] and a.xiaolei=b.最大的值所在行的数据