Re: SQL Question - Adding rows to the resultset

From: <stevedhoward_at_gmail.com>
Date: Mon, 9 Mar 2009 13:12:38 -0700 (PDT)
Message-ID: <b780d2be-aecb-49e3-8fde-d4fd8bad1f9f_at_r15g2000prh.googlegroups.com>



On Mar 7, 10:01 pm, Gokul <gokulkumar.go..._at_gmail.com> wrote:
> I am try to solve a migration problem in sql. Test scripts below if
> someone would
> like to try and give me a hand.
>
> The issue, how to carry over the result of the previous row to the
> next (well, not lag or lead exactly).
>
> In this example below, a particular transaction has a set of available
> set of other transaction ids it can use.
>
> Here, usedid has used 723, but can use only 48. So, the availid 1266
> has nothing to carry over to the next level and so on.
>
> The usedid 2333 has used 386 with nothing carried over from previous,
> but after using all of its available transactions, it can carry over
> 380 to the next level (68+48+50+600 - 386).
>
> The usedid 2654 has used 32, but can already make use of 380 carried
> from previous level. Also, the current trasactions 32 holds 2417,
> 2516, 2640,2652 must be made available to the next usedids (incuding
> the 380 - 32 carry over).
>
> USEDID  AVAILID    USED      AVAIL
> 1309    1266    723     48
> 2022    1387    347     47
> 2022    1497    347     49
> 2022    1610    347     40
> 2022    1716    347     62
> 2022    1827    347     43
> 2022    1937    347     44
> 2333    2067    386     68
> 2333    2175    386     48
> 2333    2291    386     50
> 2333    2304    386     600
> 2654    2417    32      52
> 2654    2516    32      42
> 2654    2640    32      43
> 2654    2652    32      250
> 3006    2771    308     41
> 3006    2880    308     44
> 3006    2992    308     40
> 3362    3116    217     43
> 3362    3223    217     41
> 3362    3339    217     61
> 3727    3480    647     15
> 3727    3578    647     15
> 3727    3699    647     15
> 3727    3703    647     600
>
> create table TEMP_TEST
> (
>   USED_ID  NUMBER,
>   AVAIL_ID NUMBER,
>   USED     NUMBER,
>   AVAIL    NUMBER
> );
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (1309, 1266, 723, 48);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1387, 347, 47);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1497, 347, 49);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1610, 347, 40);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1716, 347, 62);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1827, 347, 43);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2022, 1937, 347, 44);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2333, 2067, 386, 68);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2333, 2175, 386, 48);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2333, 2291, 386, 50);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2333, 2304, 386, 600);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2654, 2417, 32, 52);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2654, 2516, 32, 42);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2654, 2640, 32, 43);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (2654, 2652, 32, 250);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3006, 2771, 308, 41);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3006, 2880, 308, 44);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3006, 2992, 308, 40);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3362, 3116, 217, 43);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3362, 3223, 217, 41);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3362, 3339, 217, 61);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3727, 3480, 647, 15);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3727, 3578, 647, 15);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3727, 3699, 647, 15);
>
> insert into temp_test (USED_ID, AVAIL_ID, USED, AVAIL)
> values (3727, 3703, 647, 600);
>
> I have tried a few options but dont know how to make up new rows or to
> move few transactions to the next level.
>
> Is this possible in SQL ?
>
> Gokul

Hi Gokul,

Since this a migration project, I'll assume you inherited a crappy system over which you have no control. To get the data where you want it to be, why not just write a PL/SQL program to do the conversion to the new (hopefully better) form?

HTH, Steve Received on Mon Mar 09 2009 - 15:12:38 CDT

Original text of this message