Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hyd9r-00052r-Qc for pgsql-general@arkaria.postgresql.org; Fri, 16 Aug 2019 14:24:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hyd9q-0003Ym-Go for pgsql-general@arkaria.postgresql.org; Fri, 16 Aug 2019 14:24:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hyd9q-0003L2-13 for pgsql-general@lists.postgresql.org; Fri, 16 Aug 2019 14:24:46 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hyd9j-0002QO-Bz for pgsql-general@lists.postgresql.org; Fri, 16 Aug 2019 14:24:45 +0000 Received: by mail-pg1-x542.google.com with SMTP id o13so3017134pgp.12 for ; Fri, 16 Aug 2019 07:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=7xgKCn85L3hxshTiNNdJ3R4jXu3TcvS7/62JcOvhHXo=; b=dp3Ziz1trfdPEL0jParAgiqWQtotzG5WsxP++oOd8tXqJvbX7ytWbWnEMs6c/yT2Wl 090x9c6A28/Y/9dj82yP0W3LzNVOFR5Gep8mx2US9icNq7czUvg4HBEJtXQQP464nLVr YF8AAa47W8ZK2Jm/ThqbaQ5X11em9EJIvo9lzGZ/LbWQ/s5GnjzPoWti0L4LU5pMehpB JWM+wcc9JrpMnuo+46tFEjvOPQqJuzqwQdhAE+5WWbkeTRQSd9FcAzvnb+G2v1eOo8DX Cd9AyGZDmWWETLuTJhqzW4MoFjKgTHbH7d+KmM56H/5M8Uz3sFCgKAXec6kDMDR5k9C1 S/FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=7xgKCn85L3hxshTiNNdJ3R4jXu3TcvS7/62JcOvhHXo=; b=HcncCreT7BwDZaHO3sJC/7dSFlA505Fs9hZKgW0oZneCT1jEadcH64gfewNN3L9Bh1 8VIycmoEr2B1SqiX6/Qpmpq4kyL/WKoUYb4CKlL/JEM8U3uQUAqXp2LBqW1dYd+MJAFj fbIMfxJqfBlgW8u0PtbHoYaDdIX51bPGvFpc6TOqvCQhNFEIJPqyUxmyqbMPxpFE3ueb zWeGPQrtB08DzX2ZsFEruKflwiCOlIvtP+M9VwWq0T/ySpfzFm8bOnqafzv5J52IYHNk +JEvh0deXus8kkN9xqHI1UL1lBAUs1fefbO0hLNcV5FDDQ0QuQpFikgQYsZeiem1/lJV Imvg== X-Gm-Message-State: APjAAAWwqHjo8JzyyU/Kjd2Y+Ih3Sa8uk4llfJruHQocVQtSeSIn1CuE kN/faJJUVv/gylGmLfLWHQY= X-Google-Smtp-Source: APXvYqxoIgpPYQyNmeWAbob44v5Dvbt4IxI0m2bjrcPaL5Donep3T3HTzPf91nWMKRCGhodhIBcgBQ== X-Received: by 2002:a17:90a:8d85:: with SMTP id d5mr6795152pjo.137.1565965476334; Fri, 16 Aug 2019 07:24:36 -0700 (PDT) Received: from roblaptop ([202.53.33.82]) by smtp.gmail.com with ESMTPSA id c70sm8492562pfb.163.2019.08.16.07.24.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2019 07:24:35 -0700 (PDT) Message-ID: <2c4327dfb71736d32cefb19fde7820fc3a36569e.camel@gmail.com> Subject: Re: A 3 table join question From: rob stone To: stan , pgsql-general@lists.postgresql.org Cc: docrtp@gmail.com Date: Sat, 17 Aug 2019 00:24:31 +1000 In-Reply-To: <20190816113939.GA29708@panix.com> References: <20190816113939.GA29708@panix.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Fri, 2019-08-16 at 07:39 -0400, stan wrote: > 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.burdene > d_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? > Your view assumes that all three "streams" contain all the proj_no's whereas your test data for expense_report_cost_sum_view has no proj_no = 764. How do you know which of the three "streams" contains all proj_no's? Maybe you should consider the crosstab code so you end up with something like this;- proj_no | Labour | Material | Expenses 45 | 10810 | 5394 | 2564 764 | 8712 | 7249 | 0 789 | 46335 | 4007 | 1747