首页 新闻 会员 周边

sql 行转列问题 急!

0
悬赏园豆:10 [已关闭问题] 关闭于 2010-03-31 09:48

sql 行转列问题 急!


情况是这样的:
我有两张表:TABLE1,TABLE2(用以举例,就不细述结构了)

TABLE1:(主表)

name pkg lotId(主键) startdate enddate ……
张三 LQEP 1704251.10000 2010-03-02 2010-03-06
李四 LQEP2 6770086.01600 2010-03-03 2010-03-08
王五 LQEP3 1705932.1B000 2010-03-03 2010-03-06

张三 PBGA Z0025V76B0000 2010-03-05 2010-03-10
. . . . .
. . . . .
. . . . .

TABLE2:
lotId(外键) oper equipment defect qty ……
1704251.10000 1800 BWM002 3FMD 20
1704251.10000 1900 3D013 22
1704251.10000 8000 QCF001 3PDM 30

6770086.01600 2500 FLM002 28
. . . .
. . . .
. . . .


目前想要实现以下功能:
查询某段时间内(如:startdate=‘2010-03-02’ 到 enddate=‘2010-03-06’) 的所有客户的 oper信息并分组

显示结果集如下:

name pkg lotId startdate enddate oper1(1800) oper2(1900) oper3(2500) oper4(8000) ……
张三 LQEP 1704251.10000 2010-03-02 2010-03-06 sum(qty)[张三1800] sum(qty)[张三1900] sum(qty)[张三2500] sum(qty)[张三8000] ……
李四 LQEP 6770086.01600 2010-03-02 2010-03-06 sum(qty)[李四1800] sum(qty)[李四1900] sum(qty)[李四2500] sum(qty)[李四8000] ……
王五 LQEP 1705932.1B000 2010-03-02 2010-03-06 sum(qty)[王五1800] sum(qty)[王五1900] sum(qty)[王五2500] sum(qty)[王五8000] ……

说明:TABLE1和TABLE2关联键lotId(TABLE1.lotId = TABLE2.lotId),不同的lotId 拥有的oper 可能相同 也可能不同,
如lotId:1704251.10000 有oper(1800,1900,8000……),而lotId:6770086.01600 有oper(2500,1800,7500……).
最终显示出来的oper 会以查询数据的最大集合显示(1800,1900,2500,7500,8000……),某lotId 有该oper 就计算sum(qty),否则显示空。
程序还要求:
1.以name、pkg、…… 分组,此处忽略,只以name 分组;
2.对oper 排序并显示description,如(8000→BG 此处忽略),(排序并非大小排序,另有表与之对应),此处忽略,以oper大小排序(1800,1900,2500,7500,8000……),
此程序运行在ORACLE 环境


分虽少还望大虾们支招啊!急!!!

专注C#的主页 专注C# | 初学一级 | 园豆:187
提问于:2010-03-16 12:36
< >
分享
所有回答(2)
0

select *,(select STUFF(
   replace(
   REPLACE(
   (select dept_id from YFV_Dept where dept_id between 1 and 10 for xml raw,elements),'<row><dept_id>',','),'</dept_id></row>',''),1,1,'')) as spirt
from YFV_Dept where dept_id between 1 and 10

 

自己琢磨琢磨。。。这是例子

like%'远远'% | 园豆:635 (小虾三级) | 2010-03-16 13:10
这个好像不行滴额~
支持(0) 反对(0) 专注C# | 园豆:187 (初学一级) | 2010-03-18 13:28
0

我的想法是使用游标,先把分组数据取出放到一个变量中,再循环此变量取得具体数值

小丁 | 园豆:301 (菜鸟二级) | 2010-03-17 10:57
这样效率会很低的哈
支持(0) 反对(0) 专注C# | 园豆:187 (初学一级) | 2010-03-18 13:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册