Question

I have following data set

       a      b      c
      `1`     2      3
       3      6      9  
       9      2      11 

As you can see column a's first value is fixed (i.e. 1), but from second row it picks up the value of column c of previous record.

Column b's values are random and column c's value is calculated as c = a + b

I need to write a sql query which will select this data in above format. I tried writing using lag function but couldn't achieve.

Please help.

Edit : Column b exists in table only, a and c needs to calculated based on the values of b.

Hanumant

Was it helpful?

Solution

SQL> select a
  2       , b
  3       , c
  4    from dual
  5   model
  6         dimension by (0 i)
  7         measures (0 a, 0 b, 0 c)
  8         rules iterate (5)
  9         ( a[iteration_number] = nvl(c[iteration_number-1],1)
 10         , b[iteration_number] = ceil(dbms_random.value(0,10))
 11         , c[iteration_number] = a[iteration_number] + b[iteration_number]
 12         )
 13   order by i
 14  /

         A          B          C
---------- ---------- ----------
         1          4          5
         5          8         13
        13          8         21
        21          2         23
        23         10         33

5 rows selected.

Regards,
Rob.

OTHER TIPS

Without knowing the relation between the rows ,how can we calculate the sum of the previous row a and b column to current row a column .I have created two more column id and parent in the table to find the relation between the two rows.

parent is the column which tell us about the previous row ,and id is the primary key of the row .

create table test1 (a number ,b number ,c number ,id number ,parent number);

Insert into TEST1 (A, B, C, ID) Values (1, 2, 3, 1);
Insert into TEST1 (B, PARENT, ID) Values (6, 1, 2);
Insert into TEST1 (B, PARENT, ID) Values (4, 2, 3);

  WITH recursive (a, b, c,rn) AS
    (SELECT a,b,c,id rn
       FROM test1 
      WHERE parent IS NULL
    UNION ALL
     SELECT  (rec.a+ rec.b) a
             ,t1.b b
             ,(rec.a+ rec.b+t1.b) c
             ,t1.id rn
      FROM recursive rec,test1 t1
      WHERE t1.parent = rec.rn
    )
     SELECT a,b,c
      FROM recursive;

output

  1. The WITH keyword defines the name recursive for the subquery that is to follow

    WITH recursive (a, b, c,rn) AS

  2. Next comes the first part of the named subquery

    SELECT a,b,c,id rn FROM test1 WHERE parent IS NULL

The named subquery is a UNION ALL of two queries. This, the first query, defines the starting point for the recursion. As in my CONNECT BY query, I want to know what is the start with record.

Next up is the part that was most confusing :

SELECT  (rec.a+ rec.b) a
             ,t1.b b
             ,(rec.a+ rec.b+t1.b) c
             ,t1.id rn
  FROM recursive rec,test1 t1
  WHERE t1.parent = rec.rn

This is how it works :

  • WITH query: 1. The parent query executes:

    SELECT a,b,c FROM recursive;

    • This triggers execution of the named subquery. 2 The first query in the subquery's union executes, giving us a seed row with which to begin the recursion:

    SELECT a,b,c,id rn FROM test1 WHERE parent IS NULL

The seed row in this case will be for id =1 having parent is null. Let's refer to the seed row from here on out as the "new results", new in the sense that we haven't finished processing them yet.

  • The second query in the subquery's union executes:

    SELECT (rec.a+ rec.b) a ,t1.b b ,(rec.a+ rec.b+t1.b) c ,t1.id rn FROM recursive rec,test1 t1 WHERE t1.parent = rec.rn

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top