Question : use heirarchical relation for analytical reporting

I have 2 tables

Task
tid tname Project  parent   level
-----------------------------------------
1    t1        p1          -1         1
2    t2        p1          1          2
3    t3        p1          2          3
4    t4        p1          1          2
5    t5        p1          4          3

Timesheet
timeperiod    task    actual
------------------------------------
1                     t2        2
1                     t3        3
1                     t4        4

Output I required using SQL:
Proj   taskL1  taskL2  taskL3  taskL4  taskL5  Act
-------------------------------------------------------------
1         t1           t2        t3                                   5    
2         t1           t4                                              4

Row 1: t1 is not entered in timesheet, as it is in path of task 2 and 3 we need to show it  5 is sum of time of task 2 and 3
Row 2: same as above t1 is not entered in timesheet but it is in path of t4 so we need to show it.
Cond: if task level is 2 it should be displayed in taskL2 and if task level is 4 it should be in taskL4 with all its ancestor
I need to use sql query but at max I can have function if nothing works.

I understand this is not so simple logic. I appreciate your effort.

Answer : use heirarchical relation for analytical reporting

try this...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
SELECT project,
       CASE
           WHEN INSTR(tasks, ',', 1, 1) = 0 THEN tasks
           ELSE SUBSTR(tasks, 1, INSTR(tasks, ',', 1, 1) - 1)
       END
           taskl1,
       CASE
           WHEN INSTR(tasks, ',', 1, 1) = 0
           THEN
               NULL
           WHEN INSTR(tasks, ',', 1, 2) = 0
           THEN
               SUBSTR(tasks, INSTR(tasks, ',', 1, 1) + 1)
           ELSE
               SUBSTR(
                   tasks,
                   INSTR(tasks, ',', 1, 1) + 1,
                   INSTR(tasks, ',', 1, 2) - INSTR(tasks, ',', 1, 1) - 1
               )
       END
           taskl2,
       CASE
           WHEN INSTR(tasks, ',', 1, 2) = 0
           THEN
               NULL
           WHEN INSTR(tasks, ',', 1, 3) = 0
           THEN
               SUBSTR(tasks, INSTR(tasks, ',', 1, 2) + 1)
           ELSE
               SUBSTR(
                   tasks,
                   INSTR(tasks, ',', 1, 2) + 1,
                   INSTR(tasks, ',', 1, 3) - INSTR(tasks, ',', 1, 2) - 1
               )
       END
           taskl3,
       CASE
           WHEN INSTR(tasks, ',', 1, 3) = 0
           THEN
               NULL
           WHEN INSTR(tasks, ',', 1, 4) = 0
           THEN
               SUBSTR(tasks, INSTR(tasks, ',', 1, 3) + 1)
           ELSE
               SUBSTR(
                   tasks,
                   INSTR(tasks, ',', 1, 3) + 1,
                   INSTR(tasks, ',', 1, 4) - INSTR(tasks, ',', 1, 3) - 1
               )
       END
           taskl4,
       CASE
           WHEN INSTR(tasks, ',', 1, 4) = 0 THEN NULL
           ELSE SUBSTR(tasks, INSTR(tasks, ',', 1, 4) + 1)
       END
           taskl5,
       CASE
           WHEN INSTR(actuals, ',', 1, 1) = 0 THEN TO_NUMBER(actuals)
           ELSE TO_NUMBER(SUBSTR(actuals, 1, INSTR(actuals, ',', 1, 1) - 1))
       END
       + CASE
             WHEN INSTR(actuals, ',', 1, 1) = 0
             THEN
                 0
             WHEN INSTR(actuals, ',', 1, 2) = 0
             THEN
                 TO_NUMBER(SUBSTR(actuals, INSTR(actuals, ',', 1, 1) + 1))
             ELSE
                 TO_NUMBER(SUBSTR(
                               actuals,
                               INSTR(actuals, ',', 1, 1) + 1,
                               INSTR(actuals, ',', 1, 2) - INSTR(actuals, ',', 1, 1) - 1
                           ))
         END
       + CASE
             WHEN INSTR(actuals, ',', 1, 2) = 0
             THEN
                 0
             WHEN INSTR(actuals, ',', 1, 3) = 0
             THEN
                 TO_NUMBER(SUBSTR(actuals, INSTR(actuals, ',', 1, 2) + 1))
             ELSE
                 TO_NUMBER(SUBSTR(
                               actuals,
                               INSTR(actuals, ',', 1, 2) + 1,
                               INSTR(actuals, ',', 1, 3) - INSTR(actuals, ',', 1, 2) - 1
                           ))
         END
       + CASE
             WHEN INSTR(actuals, ',', 1, 3) = 0
             THEN
                 0
             WHEN INSTR(actuals, ',', 1, 4) = 0
             THEN
                 TO_NUMBER(SUBSTR(actuals, INSTR(actuals, ',', 1, 3) + 1))
             ELSE
                 TO_NUMBER(SUBSTR(
                               actuals,
                               INSTR(actuals, ',', 1, 3) + 1,
                               INSTR(actuals, ',', 1, 4) - INSTR(actuals, ',', 1, 3) - 1
                           ))
         END
       + CASE
             WHEN INSTR(actuals, ',', 1, 4) = 0 THEN 0
             ELSE TO_NUMBER(SUBSTR(actuals, INSTR(actuals, ',', 1, 4) + 1))
         END
           actual
  FROM (    SELECT LPAD(LEVEL, LEVEL * 3),
                   t.project,
                   t.tname,
                   s.actual,
                   LTRIM(SYS_CONNECT_BY_PATH(t.tname, ','), ',') tasks,
                   RTRIM(LTRIM(SYS_CONNECT_BY_PATH(s.actual, ','), ','), ',') actuals,
                   CONNECT_BY_ISLEAF isleaf
              FROM task t, timesheet s
             WHERE t.tname = s.task(+)
        CONNECT BY t.parent = PRIOR t.tid AND LEVEL <= 5
        START WITH parent = -1)
 WHERE isleaf = 1
Open in New Window Select All
Random Solutions  
 
programming4us programming4us