pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Join help, please
4+ messages / 3 participants
[nested] [flat]

* Join help, please
@ 2020-03-18 18:50  stan <stanb@panix.com>
  0 siblings, 2 replies; 4+ messages in thread

From: stan @ 2020-03-18 18:50 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I am confused. given this view:


AS
SELECT
	employee.id ,
	work_type.type ,
	permit ,
    	work_type.overhead ,
   	work_type.descrip  
from
	permitted_work
inner join employee on
	employee.employee_key = permitted_work.employee_key
inner join work_type on
	work_type.work_type_key = work_type.work_type_key
	;

Why do I have 38475 rows, when the base table only has 855?

My thinking was that the inner joins would constrain this view to the rows
that exist in the base (permitted_work) table. 

Clearly I am misunderstanding something basic here.


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





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Join help, please
@ 2020-03-18 18:55  Michael Lewis <mlewis@entrata.com>
  parent: stan <stanb@panix.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Michael Lewis @ 2020-03-18 18:55 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

work_type.work_type_key = work_type.work_type_key

You've done a cross join.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Join help, please
@ 2020-03-18 18:57  Guyren Howe <guyren@gmail.com>
  parent: stan <stanb@panix.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Guyren Howe @ 2020-03-18 18:57 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; stan <stanb@panix.com>

The three types of thing (permitted_work; employee; work_type) don’t stand in a 1:1 relationship with each other. You might have multiple work_types or permitted_work for each employee, I’m guessing.

Each existing combination produces one row in the result. So an employee with three permitted_works and 4 work types will produce 12 rows in the joined result.

If you want one row per employee, you might consider using array_agg with group_by to collapse the multiple work_types or permitted_works into arrays alongside the employee information.
On Mar 18, 2020, 11:51 -0700, stan <stanb@panix.com>, wrote:
> I am confused. given this view:
>
>
> AS
> SELECT
> employee.id ,
> work_type.type ,
> permit ,
> work_type.overhead ,
> work_type.descrip
> from
> permitted_work
> inner join employee on
> employee.employee_key = permitted_work.employee_key
> inner join work_type on
> work_type.work_type_key = work_type.work_type_key
> ;
>
> Why do I have 38475 rows, when the base table only has 855?
>
> My thinking was that the inner joins would constrain this view to the rows
> that exist in the base (permitted_work) table.
>
> Clearly I am misunderstanding something basic here.
>
>
> --
> "They that would give up essential liberty for temporary safety deserve
> neither liberty nor safety."
> -- Benjamin Franklin
>
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Join help, please
@ 2020-03-18 19:36  Michael Lewis <mlewis@entrata.com>
  parent: Michael Lewis <mlewis@entrata.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Lewis @ 2020-03-18 19:36 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

On Wed, Mar 18, 2020, 12:55 PM Michael Lewis <mlewis@entrata.com> wrote:

> work_type.work_type_key = work_type.work_type_key
>
> You've done a cross join.
>


You meant to do permitted_work.work_type_key = work_type.work_type_key I
expect

>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2020-03-18 19:36 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-18 18:50 Join help, please stan <stanb@panix.com>
2020-03-18 18:55 ` Michael Lewis <mlewis@entrata.com>
2020-03-18 19:36   ` Michael Lewis <mlewis@entrata.com>
2020-03-18 18:57 ` Guyren Howe <guyren@gmail.com>

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