首页 新闻 会员 周边 捐助

请问个sql关联查询后,或者c#winfrom 思路

0
悬赏园豆:50 [已解决问题] 解决于 2015-09-23 17:01

数据库3个表,主表 (Pro_Inspecting ),副表(Pro_Inspecting_BadnessInfo,Pro_Inspecting_MoPVNo)

3表通过PVNO 进行关联,想得到总的展示,格式为PVNO(不重复),Entrytime ,MOPVNo(同一个PVNO对应多个MOPVNO,之间用,连接),拉丝(对应值根据Pro_Inspecting_BadnessInfo表 同一个PVNO加Badness为拉丝,将EndLength累加 减去

BeginLenth累加,值作为拉丝的值,若PVNO没有Badness为拉丝, 则值为0),划痕,晶点,接头, 涂头,杂质,脏污,色差,刺伤,吴胶,麻点,跑偏,压痕,停机印,划伤,

最终显示为  ( SP1507230109  )  ,2015-07-24 14:58:34.120,  (CT1507230037,CT1507230035,CT1507230025CT1507230026,CT1507240001,CT1507240003,CT1507230027,CT1507230029,CT1507240010),拉丝值划痕值,晶点值,接头值, 涂头值,杂质值,脏污值,色差值,刺伤值,吴胶值,麻点值,跑偏值,压痕值,停机印值,划伤值            为一行数据

rocky54321的主页 rocky54321 | 初学一级 | 园豆:133
提问于:2015-09-23 10:18
< >
分享
最佳答案
0

分三步走吧.

第一步: 先将MOPVNO 按PVNO 分组. 然后用逗号拼接起来.然后存到一个临时表里,代码如下:

SELECT PVNO,
(SELECT MOPVNO+',' FROM Pro_Inspecting_MoPVNo 
  WHERE PVNO=A.PVNO 
  FOR XML PATH('')) AS MOPVNO
INTO #tbMOPVNO
FROM Pro_Inspecting_MoPVNo A 
GROUP BY A.PVNO

第二步: 将Pro_Inspecting_BadnessInfo 表 行转列 并计算值, 代码如下:

     SELECT PVNo,
     SUM(CASE WHEN Badness=N'拉丝' THEN EndLength-BeginLength ELSE 0 END) AS N'拉丝',
     SUM(CASE WHEN Badness=N'划痕' THEN EndLength-BeginLength ELSE 0 END) AS N'划痕',
     SUM(CASE WHEN Badness=N'晶点' THEN EndLength-BeginLength ELSE 0 END) AS N'晶点',
     SUM(CASE WHEN Badness=N'接头' THEN EndLength-BeginLength ELSE 0 END) AS N'接头',
     SUM(CASE WHEN Badness=N'涂头' THEN EndLength-BeginLength ELSE 0 END) AS N'涂头',
     SUM(CASE WHEN Badness=N'杂质' THEN EndLength-BeginLength ELSE 0 END) AS N'杂质',
     SUM(CASE WHEN Badness=N'脏污' THEN EndLength-BeginLength ELSE 0 END) AS N'脏污',
     SUM(CASE WHEN Badness=N'色差' THEN EndLength-BeginLength ELSE 0 END) AS N'色差',
     SUM(CASE WHEN Badness=N'刺伤' THEN EndLength-BeginLength ELSE 0 END) AS N'刺伤',
     SUM(CASE WHEN Badness=N'吴胶' THEN EndLength-BeginLength ELSE 0 END) AS N'吴胶',
     SUM(CASE WHEN Badness=N'麻点' THEN EndLength-BeginLength ELSE 0 END) AS N'麻点',
     SUM(CASE WHEN Badness=N'跑偏' THEN EndLength-BeginLength ELSE 0 END) AS N'跑偏',
     SUM(CASE WHEN Badness=N'压痕' THEN EndLength-BeginLength ELSE 0 END) AS N'压痕',
     SUM(CASE WHEN Badness=N'停机印' THEN EndLength-BeginLength ELSE 0 END) AS N'停机印',
     SUM(CASE WHEN Badness=N'划伤' THEN EndLength-BeginLength ELSE 0 END) AS N'划伤'
     INTO #tbBadnessInfo
     FROM Pro_Inspecting_BadnessInfo
     GROUP BY PVNo

第三步: 

将这两个临时表和主表根据PVNO 进行连表查询.. 代码就不写了.

收获园豆:30
李丶GuanYao | 小虾三级 |园豆:1228 | 2015-09-23 12:44

SELECT PVNO, (SELECT MOPVNO+',' FROM Pro_Inspecting_MoPVNo WHERE PVNO=A.PVNO FOR XML PATH('')) AS MOPVNO INTO #tbMOPVNO FROM Pro_Inspecting_MoPVNo A GROUP BY A.PVNO    这个语句不满足, MOPVNO 相同的不能去除重复的, 待优化下 

rocky54321 | 园豆:133 (初学一级) | 2015-09-23 17:03

@rocky54321: 如果一个PVNO对应的MOPNO也有可能重复的话. 那就再Group By 一下.

例如:

SELECT PVNO,
(SELECT MOPVNO+',' FROM Pro_Inspecting_MoPVNo 
  WHERE PVNO=A.PVNO 
  GROUP BY PVNO,MOPVNO
  FOR XML PATH('')) AS MOPVNO
FROM Pro_Inspecting_MoPVNo A 
GROUP BY A.PVNO
李丶GuanYao | 园豆:1228 (小虾三级) | 2015-09-24 07:58
其他回答(1)
0

你这个建议分为多部走;

1、简单的表关联;

2、写一个自定义函数,生成(CT1507230037,CT1507230035,CT1507230025CT1507230026,CT1507240001,CT1507240003,CT1507230027,CT1507230029,CT1507240010)这串数据

3、用一个临时表,先查询出各个类型的数据,然后进行行列转换;

4、将1、2、3部连接起来,大概就是:

select t1.PvNo,t1.Entrytime, getMoPVNoByPvNo(t1.PvNo), #t2.*
from Pro_Inspecting t1 
left join #t2 on t1.PvNo = #t2.PvNo
where t1.PvNo = 'SP1507230109'

--其中getMoPVNoByPvNo是自定义函数,#t2是第三步中临时表。
收获园豆:20
幻天芒 | 园豆:37207 (高人七级) | 2015-09-23 10:52

我创建了视图 包含,PvNo,MoPVNo2列数据,这个自定义过程是干嘛用呢 ? 还有行转换列, 这个转换难度我写不出来。

支持(0) 反对(0) rocky54321 | 园豆:133 (初学一级) | 2015-09-23 11:07

@rocky54321: 自定义函数,专门来做拼接那个工作。那么多个MOPVNo,需要用逗号连接成一个列呢。

支持(0) 反对(0) 幻天芒 | 园豆:37207 (高人七级) | 2015-09-23 13:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册