具体描述不出来,看图片:
附上建表语句:
create table TAB1
(
id VARCHAR2(6),
iname VARCHAR2(20),
sno VARCHAR2(6)
)
insert into tab1(id,iname,sno) values('1001','abcde','s001');
insert into tab1(id,iname,sno) values('1002','hijk','s002');
insert into tab1(id,iname,sno) values('1002','hijk','s003');
insert into tab1(id,iname,sno) values('1003','xyz','s001');
insert into tab1(id,iname,sno) values('1003','xyz','s003');
insert into tab1(id,iname,sno) values('1003','xyz','s004');
insert into tab1(id,iname,sno) values('1004','abc','s005');
insert into tab1(id,iname,sno) values('1004','abc','s006');
insert into tab1(id,iname,sno) values('1005','def','s002');
insert into tab1(id,iname,sno) values('1005','def','s004');
需要实现的是
根据上一个blog写的方法,可以一步一步的想
第一步,明显需要的结果中是根据ID 或者INAME分组且按窗口排序,这时想到如下sql:
SELECT 'SELECT id,iname,' ||
WMSYS.WM_CONCAT('MAX(DECODE(sn,''' || SN || ''',sno)) AS "供应商' || SN || '"') ||
' FROM (SELECT id, iname, sno, ROW_NUMBER() OVER(PARTITION BY id ORDER BY sno) sn FROM tab1)
GROUP BY id,iname'
FROM (SELECT ID,
INAME,
SNO,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY SNO) SN
FROM TAB1);
SELECT ID,
INAME,
MAX(DECODE(SN, '1', SNO)) AS "供应商1",
MAX(DECODE(SN, '1', SNO)) AS "供应商1",
MAX(DECODE(SN, '2', SNO)) AS "供应商2",
MAX(DECODE(SN, '1', SNO)) AS "供应商1",
MAX(DECODE(SN, '2', SNO)) AS "供应商2",
MAX(DECODE(SN, '3', SNO)) AS "供应商3"
FROM (SELECT ID,
INAME,
SNO,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY SNO) SN
FROM TAB1)
GROUP BY ID, INAME
但是显然不是我们所需要的,因为列数重复了,且多了
于是,又想到第二步,可以在最外面的表中,只查到唯一的sn,所以有如下的SQL:
SELECT 'SELECT id,iname,' ||
WMSYS.WM_CONCAT('MAX(DECODE(sn,''' || SN || ''',sno)) AS "供应商' || SN || '"') ||
' FROM (SELECT id, iname, sno, ROW_NUMBER() OVER(PARTITION BY id ORDER BY sno) sn FROM tab1)
GROUP BY id,iname
ORDER BY id;'
FROM (SELECT DISTINCT ROW_NUMBER() OVER(PARTITION BY ID ORDER BY SNO) SN
FROM TAB1);
SELECT ID,
INAME,
MAX(DECODE(SN, '1', SNO)) AS "供应商1",
MAX(DECODE(SN, '2', SNO)) AS "供应商2",
MAX(DECODE(SN, '3', SNO)) AS "供应商3"
FROM (SELECT ID,
INAME,
SNO,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY SNO) SN
FROM TAB1)
GROUP BY ID, INAME
ORDER BY ID;
至此,达到我们的需求
这是我上篇blog的活学活用,虽然例子是我朋友的,但是也给他解决了很久之前的一个问题,吼吼