请SQL高手帮忙,写个存储过程
表一:CZ_CPLYL
客户名称型号批号刻号供应商提供数实际到位数投料数次品数供应商材料利用率实际利用率良品率
1AAM1493E67999013EF844802.23469212812442.642.7396.88
2AATP30034K249374K24937-01-G73092309230632999.0610099.06
3AATP30034K249374K24937-08-B3306130613058399.910099.9
4ABTP3003090820-1090820-125252329210092
5ABTP3003090820-2090820-2302926386.6796.6789.66
6BBADMTV804Q64561.1Q64561.1239244238699.58102.0997.54
7BBADMTV804Q64566.1Q64566.19978562256.5778.7971.79
8AC21F384HW0FR912997AG912997-22D1770815414681.89294.81
9ACAP-96AAFA97-5324FA975324-07-A03338333633221499.5299.9499.58
10BCAP-96AAFA97-5324FA975324-08-C33425342534091699.5310099.53
视图二:CZ_BLPMX
刻号不良代码不良明细不良数量
167999013Z09调机报废3
267999013Z05晶粒来料不良1
34K24937-01-G7Z06晶粒刮伤20
44K24937-01-G7Z02晶粒裂痕6
54K24937-01-G7Z09调机报废5
64K24937-08-B3Z06晶粒刮伤3
表三:CZ_BLPDM
不良代码不良明细
Z01芯片抛料
Z02晶粒裂痕
Z03芯片沾污
Z04晶粒崩边
Z05晶粒来料不良
Z06晶粒刮伤
Z08误装报废
Z09调机报废
Z10Mapping错误
生成表四:CZ_BLP_HZ_MX
客户名称型号批号刻号次品数芯片抛料晶粒裂痕芯片沾污晶粒崩边晶粒来料不良晶粒刮伤误装报废调机报废Mapping错误
1AAM1493E67999013EF844802.23413
2AATP30034K249374K24937-01-G7296205
3AATP30034K249374K24937-08-B333
4ABTP3003090820-1090820-12
5ABTP3003090820-2090820-23
6BBADMTV804Q64561.1Q64561.16
7BBADMTV804Q64566.1Q64566.122
8AC21F384HW0FR912997AG912997-22D18
9ACAP-96AAFA97-5324FA975324-07-A014
10BCAP-96AAFA97-5324FA975324-08-C316
我是初学者,前台用的是delphi,把不良品明细汇总到具体的不良品数量后面,CZ_BLPDM这张表式动态的,因为随时会增加不良品的类型。请问我该怎么写。
[解决办法]
这种行列转换使用动态拼接吧,参照一下这个
如果项数不固定,使用存储过程动态SQL拼接表结构如下: t_fl: lb ---- a b c d e t_sj id lb sl ------------- 1 a 10 1 c 30 1 d 40 1 e 50 2 b 20 2 e 50 想弄出这样一个结果: id sl_a sl_b sl_c sl_d sl_e --------------------------------- 1 10 30 40 50 2 20 50 --1、如果t_fl表行数据不定的话可以写一个存储过程CREATE OR REPLACE PROCEDURE row2col(o OUT SYS_REFCURSOR) IS sqlstr VARCHAR2(4000) := '';BEGIN FOR cc IN (SELECT lb FROM t_fl) LOOP sqlstr := sqlstr || 'sum(decode(lb,''' || cc.lb ||''',sl)) as "sl_' || cc.lb || '",'; END LOOP; sqlstr := 'select id,' || rtrim(sqlstr,',') || ' from t_sj group by id'; OPEN o FOR sqlstr;END row2col;