Search Postgresql Archives

How to distribute budget value to actual rows in Postgresql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Budget table contains jobs with loads:

   create temp table budget (
     job char(20) primary key,
     load numeric(4,1) not null check (load>0 )
     );
   insert into budget values ( 'programmer', 3 );
   insert into budget values ( 'analyst', 1.5 );

Actual table contains actual loads by employees:

   create temp table actual (
     job char(20),
     employee char(20),
     load numeric(4,1) not null check (load>0 ),
     contractdate date,
     primary key (job, employee)
     );

   insert into actual values ( 'programmer', 'John',  1, '2014-01-01' );
   -- half time programmer:
   insert into actual values ( 'programmer', 'Bill', 0.5, '2014-01-02' );

   insert into actual values ( 'analyst', 'Aldo', 1, '2014-01-03' );
   insert into actual values ( 'analyst', 'Margaret', 1, '2014-01-04' );

Result table should show difference between budget and actual jobs so that budget load is
distributed to employees in contract date order.

If budget load is greater than sum of job loads, separate budget line with empty employee
should appear.

In data above, 1.5 programmers are missing and 0.5 analysts are more.

Result should be

   Job        Employee  Budget  Actual  Difference

   programmer John      1       1       0
   programmer Bill      0.5     0.5     0
   programmer           1.5     0       1.5
   analyst    Aldo      1       1       0
   analyst    Margaret  0.5     1       -0.5

How to create such table in modern Postgresql ?
Can rank function with full join used or other idea ?

I tried

   select
    coalesce(budget.job, actual.job ) as job,
    employee,
    budget.load as budget,
    coalesce(actual.load,0) as actual,
    coalesce(budget.load,0)-coalesce( actual.load,0) as difference
   from budget full join actual on (job)
   order by contractdate

but this does not distribute budget load to employee rows.

I posted this also in

http://stackoverflow.com/questions/21664842/how-to-distribute-budget-value-to-actual-rows-in-postgresql

Andrus.


--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux