对于要求,我必须创建一个查询来显示每周的员工时间表。查询是这样的:
select weekday, sched_hrs
from table
where emplid = '12345'
and weekday_name= 1
此查询的输出如下:
Weekday | Sched_hrs
--------------------
1 | 7.6
1 | 7.6
1 | 7.6
1 | 7.6
1 | 7.6
1 | OFF
1 | OFF
我想要以下格式的输出:
1 7.6 7.6 7.6 7.6 7.6 OFF OFF
如何实现?
如果您同意串联列表,请使用Oracle 11g Release 2
中介绍的
LISTAGG。
SELECT weekday, LISTAGG(Sched_hrs, ',') WITHIN GROUP (ORDER BY weekday) AS Sched_hrs
FROM table
WHERE emplid = '12345' AND weekday_name= 1
GROUP BY weekday;
例如,
SQL> column employees format a50
SQL> SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
2 FROM emp
3 GROUP BY deptno;
DEPTNO EMPLOYEES
---------- --------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
SQL>
在11g中您可以使用PIVOT功能。 请参阅文档:
http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html