Oracle listagg查询

时间:2015-04-09 18:48:26

标签: oracle11g pivot listagg

这是我的查询结果:

WEEKENDDATE CCNAME PHNAME RATIO 08-Feb-15 Apple Line 1 - Day L&I Work 0.45 08-Feb-15 Apple Line 1 - Day Sorter 6.85 08-Feb-15 Apple Line 1 - Day Tray Fill 12.93 08-Feb-15 Apple Line 1 - Day WH General Labor 5.6 08-Feb-15 Apple Line 1 - Day WH Supervisor 1.48 15-Feb-15 Apple Line 1 - Day L&I Work 0.42 15-Feb-15 Apple Line 1 - Day Sorter 6.09 15-Feb-15 Apple Line 1 - Day Tray Fill 11.9 15-Feb-15 Apple Line 1 - Day WH General Labor 5.42 15-Feb-15 Apple Line 1 - Day WH Supervisor 3.46 22-Feb-15 Apple Line 1 - Day L&I Work 0.43 22-Feb-15 Apple Line 1 - Day Sorter 6.01 22-Feb-15 Apple Line 1 - Day Tray Fill 12.09 22-Feb-15 Apple Line 1 - Day WH General Labor 4.9 22-Feb-15 Apple Line 1 - Day WH Supervisor 1.71

我的老板想要数据横向 有没有办法让我的查询结果格式如下:

CCNAME PHNAME 08-Feb-15 15-Feb-15 22-Feb-15 Apple Line 1 - Day L&I Work 0.45 0.42 0.43 Apple Line 1 - Day Sorter 6.85 6.09 6.01 Apple Line 1 - Day Tray Fill 12.93 11.9 12.09 Apple Line 1 - Day WH General Labor 5.6 5.42 4.9 Apple Line 1 - Day WH Supervisor 1.48 3.46 1.71

或者像这样:

CCNAME PHNAME 08-FEB-15,15-FEB-15,22-FEB-15 Apple Line 1 - Day L&I Work .45,.42,.43 Apple Line 1 - Day Sorter 6.85,6.09,6.01 Apple Line 1 - Day Tray Fill 12.93,11.9,12.09 Apple Line 1 - Day WH General Labor 5.6,5.42,4.9 Apple Line 1 - Day WH Supervisor 1.48,3.46,1.71

以下是我能够弄清楚的问题:
请注意我事先不知道日期范围。
这取决于用户要求的周数。

select ccname, phname, listagg(weekenddate||','||ratio,'; ') 
within group(order by ccname, phname) ratio
from
(...leaving out this code...)
group by ccname, phname

但是下面的结果还不够好:我可以使用' listagg'或者我应该研究' pivot'代替?

CCNAME PHNAME RATIO Apple Line 1 - Day L&I Work 08-FEB-15,.45;15-FEB-15,.42;22-FEB-15,.43 Apple Line 1 - Day Sorter 08-FEB-15,6.85;15-FEB-15,6.09;22-FEB-15,6.01 Apple Line 1 - Day Tray Fill 08-FEB-15,12.93;15-FEB-15,11.9;22-FEB-15,12.09 Apple Line 1 - Day WH General Labor 08-FEB-15,5.6;15-FEB-15,5.42;22-FEB-15,4.9 Apple Line 1 - Day WH Supervisor 08-FEB-15,1.48;15-FEB-15,3.46;22-FEB-15,1.71

0 个答案:

没有答案