Sometimes, it is required to display detail rows as a single column in master row.
For example in scott schema you have two tables ( DEPT, EMP) which relation is 1-M .
If required to display Employees names separated by comma as one column per every DEPTNO, ÷In order to the final output like the following.
The easiest way to get output of previous diagram is the following SQL query.
SELECT d.deptNo,
MAX (SUBSTR (SYS_CONNECT_BY_PATH (d.eName, ','), 2)) employees
FROM (SELECT deptNo,
eName,
ROW_NUMBER ()
OVER (PARTITION BY deptNo ORDER BY deptNo, eName)
rnum
FROM scott.emp) d
START WITH d.rnum = 1
CONNECT BY d.rnum = PRIOR d.rnum + 1 AND PRIOR d.deptNo = d.deptNo
GROUP BY d.deptNo
For example in scott schema you have two tables ( DEPT, EMP) which relation is 1-M .
If required to display Employees names separated by comma as one column per every DEPTNO, ÷In order to the final output like the following.
The easiest way to get output of previous diagram is the following SQL query.
SELECT d.deptNo,
MAX (SUBSTR (SYS_CONNECT_BY_PATH (d.eName, ','), 2)) employees
FROM (SELECT deptNo,
eName,
ROW_NUMBER ()
OVER (PARTITION BY deptNo ORDER BY deptNo, eName)
rnum
FROM scott.emp) d
START WITH d.rnum = 1
CONNECT BY d.rnum = PRIOR d.rnum + 1 AND PRIOR d.deptNo = d.deptNo
GROUP BY d.deptNo
No comments:
Post a Comment