数据库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),拉丝值,划痕值,晶点值,接头值, 涂头值,杂质值,脏污值,色差值,刺伤值,吴胶值,麻点值,跑偏值,压痕值,停机印值,划伤值 为一行数据
分三步走吧.
第一步: 先将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 进行连表查询.. 代码就不写了.
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: 如果一个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
你这个建议分为多部走;
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是第三步中临时表。
我创建了视图 包含,PvNo,MoPVNo2列数据,这个自定义过程是干嘛用呢 ? 还有行转换列, 这个转换难度我写不出来。
@rocky54321: 自定义函数,专门来做拼接那个工作。那么多个MOPVNo,需要用逗号连接成一个列呢。