pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: stan <stanb@panix.com>
To: pgsql-general@lists.postgresql.org
Cc: docrtp@gmail.com
Subject: A 3  table join question
Date: Fri, 16 Aug 2019 07:39:39 -0400
Message-ID: <20190816113939.GA29708@panix.com> (raw)

First let me say a huge THANK YOU to all the helpful people that hanging out
on this.

I am changing from one type of work, going back to some database work for a
project, as my old job was eliminated. I have made great progress on this,
thanks to the time and effort of lots of folks from this list.

Now, here is my latest stumbling block. I have three "data streams" that all
contribute to the total costs of a project:

* labor cost
* material cost
* expense report cost

I have a view that summarizes the burdened cost from each of these 3
streams, and i am trying to create a view that shows the total project cost.

Here is the test data from each of the 3 streams:

stan=> select * from labor_cost_sum_view ;
 proj_no | labor_bill_sum | labor_cost_sum 
 ---------+----------------+----------------
   45 |     10810.0000 |  3133.17500000
  764 |      8712.0000 |   810.75000000
  789 |     46335.5400 |  7015.57500000
 (3 rows)

stan=> select * from material_cost_sum_view ;
 proj_no | mtrl_cost 
 ---------+-----------
   45 | 5394.6800
  764 | 7249.4800
 7456 | 4007.3000
(3 rows)

stan=> select * from expense_report_cost_sum_view ;
 proj_no | incured_sum | burdened_cost 
 ---------+-------------+---------------
   45 |     2564.98 |   2564.980000
 7456 |     1747.11 |   1747.110000
(2 rows)

And here is the clause for creating the summary table that I presently have:


DROP VIEW overall_cost_sum_view ;

CREATE view overall_cost_sum_view as 
select 
	material_cost_sum_view.proj_no as l_proj_vo ,
	labor_cost_sum_view.proj_no as m_proj_vo , 
	expense_report_cost_sum_view.proj_no as x_proj_vo , 
	cast (labor_cost_sum_view.labor_cost_sum as money) as l_burdened_cost,
	cast (material_cost_sum_view.mtrl_cost as money)as m_burdened_cost,
	cast (expense_report_cost_sum_view.burdened_cost as money)as x_burdened_cost ,
	cast (
			coalesce( labor_cost_sum_view.labor_cost_sum, 0) 
			+
      		 	coalesce(material_cost_sum_view.mtrl_cost, 0)
			+
      		 	coalesce(expense_report_cost_sum_view.burdened_cost, 0)
       	as money)  as ttl_cost
from 
	labor_cost_sum_view 
full join    material_cost_sum_view  on
	material_cost_sum_view.proj_no = labor_cost_sum_view.proj_no
full join    expense_report_cost_sum_view  on
	expense_report_cost_sum_view.proj_no = labor_cost_sum_view.proj_no
;

Which results in the following:

stan=> select * from overall_cost_sum_view ;
-----------+-----------+-----------+-----------------+-----------------+-------- ---------+------------
        45 |        45 |        45 |       $3,133.18 |       $5,394.68 |       $ 2,564.98 | $11,092.84
       764 |       764 |           |         $810.75 |       $7,249.48 |                  |  $8,060.23
           |       789 |           |       $7,015.58 |                 |                  |  $7,015.58
      7456 |           |           |                 |       $4,007.30 |                  |  $4,007.30
           |           |      7456 |                 |                 |       $ 1,747.11 |  $1,747.11
(5 rows)


As you can see this statement seems to work correctly on the join of the
labor and material costs, but fails when I add the expense report stream.

What am I doing wrong here?

-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





view thread (7+ messages)  latest in thread

Message-ID: <20190816113939.GA29708@panix.com>
Permalink:  ../20190816113939.GA29708@panix.com/
Also on:    postgresql.org/message-id/20190816113939.GA29708@panix.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: stanb@panix.com, docrtp@gmail.com
  Subject: Re: A 3  table join question
  In-Reply-To: <20190816113939.GA29708@panix.com>
  References: <20190816113939.GA29708@panix.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox