| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 554 人关注过本帖
标题:SQL记录拆分
只看楼主 加入收藏
jimmy178293
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
SQL记录拆分
图片附件: 游客没有浏览图片的权限,请 登录注册

请问大家能否把图中两条记录用SQL语句,拆分为4条,也就是查询出来有12条记录,谢谢!
(拆分的依据是S1,S2,S3,S4中有没有数量,有的话独立开来),求救!!!
拆分前:              arrival_id  manual_id    supply_no warehouse_no    goods_no   colorid    long   s1  s2  s3 s4
                      123456      abcd         123a      kkk             fff        ddd         0     0    5   9  0
                      123456      abcd         123a      kkk             zzz        ddd         0     0    4   1  0


这两条记录拆分后如下:arrival_id  manual_id    supply_no warehouse_no    goods_no   colorid    long   s1  s2  s3 s4
                      123456      abcd         123a      kkk             fff        ddd         0     0    5   0  0
                      123456      abcd         123a      kkk             fff        ddd         0     0    0   9  0
                      123456      abcd         123a      kkk             zzz        ddd         0     0    4   0  0
                      123456      abcd         123a      kkk             zzz        ddd         0     0    0   1  0
搜索更多相关主题的帖子: 记录 
2014-12-20 10:43
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:7 
先看看
2014-12-21 19:08
jimmy178293
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-13
收藏
得分:0 
回复 2楼 tlliqi
大哥求救!
2014-12-22 09:18
volte
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:69
帖 子:1167
专家分:1316
注 册:2004-12-19
收藏
得分:7 
select arrival_id, manual_id,supply_no,,warehouse_no,goods_no,colorid,long,s1, 0 as s2, 0 as s3,0 as s4 from Table where s1>0 and s2=0 and s3=0 and s4=0
union all
select arrival_id, manual_id,supply_no,,warehouse_no,goods_no,colorid,long,0 as s1, s2, 0 as s3,0 as s4 from Table where s1=0 and s2>0 and s3=0 and s4=0
union all
select arrival_id, manual_id,supply_no,,warehouse_no,goods_no,colorid,long,0 as s1, 0 as s2, s3,0 as s4 from Table where s1=0 and s2=0 and s3>0 and s4=0
union all
select arrival_id, manual_id,supply_no,,warehouse_no,goods_no,colorid,long,0 as s1, 0 as s2, 0 as s3,s4 from Table where s1=0 and s2=0 and s3=0 and s4>0


大家都是朋友,有空就来坐坐!
2014-12-24 13:07
antjl
Rank: 2
等 级:论坛游民
威 望:1
帖 子:21
专家分:71
注 册:2012-8-16
收藏
得分:7 
---个人习惯,首字母大写,便于阅读;
---算法:
---     1、S1列到S4列不管有没有0都要求结果有是0的行;
---     2、S1列到S4如果非0的话,单独列一行;
---     3、Union会自动剔除重复行
Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],0 As S1,S2,S3,S4 From Arrival
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,0 As S2,S3,S4 From Arrival
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,0 As S3,S4 From Arrival
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,S3,0 As S4 From Arrival
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,S3,S4 From Arrival Where S1<>0
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,S3,S4 From Arrival Where S2<>0
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,S3,S4 From Arrival Where S3<>0
Union Select Arrival_ID,Manual_ID,Supply_No,Warehouse_No,Goods_No,ColorID,[Long],S1,S2,S3,S4 From Arrival Where S4<>0
2014-12-26 16:57
快速回复:SQL记录拆分
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034628 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved