pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
2 Table Select
4+ messages / 3 participants
[nested] [flat]

* 2 Table Select
@ 1999-05-27 16:23  Andy Lewis <alewis@themecca.net>
  0 siblings, 2 replies; 4+ messages in thread

From: Andy Lewis @ 1999-05-27 16:23 UTC (permalink / raw)
  To: pgsql-sql

I am trying to get the _total_ number of jobs
that each company has in the db.

In other words I want to get each distinct company
that has jobs in the jobs table and the _total_ number 
of jobs that it has in the jobs table.

What would be the best query that I could use?

I'm using 6.4.2.

Table = jobs
+-------------+---------------+-------+
| Field       | Type          | Length|
+-------------+---------------+-------+
| job_id      | int4 not null |     4 |
| company_id  | int4          |     4 |
| title       | varchar()     |    50 |
...
+-------------+---------------+-------+

Table = company
+--------------+---------------+-------+
|Field         |Type           | Length|
+--------------+---------------+-------+
| company_id   | int4 not null |     4 |
| company_name | varchar()     |   100 |
| email        | varchar()     |   100 |
...
+--------------+---------------+-------+

Thanks

Andy




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

* Re: [SQL] 2 Table Select
@ 1999-05-27 19:34  Oliver Elphick <olly@lfix.co.uk>
  parent: Andy Lewis <alewis@themecca.net>
  1 sibling, 1 reply; 4+ messages in thread

From: Oliver Elphick @ 1999-05-27 19:34 UTC (permalink / raw)
  To: Andy Lewis <alewis@themecca.net>; +Cc: pgsql-sql

Andy Lewis wrote:
  >I am trying to get the _total_ number of jobs
  >that each company has in the db.
  >
  >In other words I want to get each distinct company
  >that has jobs in the jobs table and the _total_ number 
  >of jobs that it has in the jobs table.
  >
  >What would be the best query that I could use?
  >
  >I'm using 6.4.2.
  >
  >Table = jobs
  >+-------------+---------------+-------+
  >| Field       | Type          | Length|
  >+-------------+---------------+-------+
  >| job_id      | int4 not null |     4 |
  >| company_id  | int4          |     4 |
  >| title       | varchar()     |    50 |
  >...
  >+-------------+---------------+-------+
  >
  >Table = company
  >+--------------+---------------+-------+
  >|Field         |Type           | Length|
  >+--------------+---------------+-------+
  >| company_id   | int4 not null |     4 |
  >| company_name | varchar()     |   100 |
  >| email        | varchar()     |   100 |
  >...
  >+--------------+---------------+-------+
 
select c.company_id, count(j.job_id)
   from company as c, jobs as j
   where c.company_id = j.company_id
   group by c.company_id;

-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "All that the Father giveth me shall come to me; and
      him that cometh to me I will in no wise cast out."
                                      John 6:37 





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

* Re: [SQL] 2 Table Select
@ 1999-05-27 20:37  Andy Lewis <alewis@themecca.net>
  parent: Oliver Elphick <olly@lfix.co.uk>
  0 siblings, 0 replies; 4+ messages in thread

From: Andy Lewis @ 1999-05-27 20:37 UTC (permalink / raw)
  To: Oliver Elphick <olly@lfix.co.uk>; +Cc: pgsql-sql

Thanks, I have it now....

Andy

On Thu, 27 May 1999, Oliver Elphick wrote:

> Andy Lewis wrote:
>   >I am trying to get the _total_ number of jobs
>   >that each company has in the db.
>   >
>   >In other words I want to get each distinct company
>   >that has jobs in the jobs table and the _total_ number 
>   >of jobs that it has in the jobs table.
>   >
>   >What would be the best query that I could use?
>   >
>   >I'm using 6.4.2.
>   >
>   >Table = jobs
>   >+-------------+---------------+-------+
>   >| Field       | Type          | Length|
>   >+-------------+---------------+-------+
>   >| job_id      | int4 not null |     4 |
>   >| company_id  | int4          |     4 |
>   >| title       | varchar()     |    50 |
>   >...
>   >+-------------+---------------+-------+
>   >
>   >Table = company
>   >+--------------+---------------+-------+
>   >|Field         |Type           | Length|
>   >+--------------+---------------+-------+
>   >| company_id   | int4 not null |     4 |
>   >| company_name | varchar()     |   100 |
>   >| email        | varchar()     |   100 |
>   >...
>   >+--------------+---------------+-------+
>  
> select c.company_id, count(j.job_id)
>    from company as c, jobs as j
>    where c.company_id = j.company_id
>    group by c.company_id;
> 
> -- 
> Oliver Elphick                                Oliver.Elphick@lfix.co.uk
> Isle of Wight                              http://www.lfix.co.uk/oliver
>                PGP key from public servers; key ID 32B8FAA1
>                  ========================================
>      "All that the Father giveth me shall come to me; and
>       him that cometh to me I will in no wise cast out."
>                                       John 6:37 
> 
> 




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

* Re: [SQL] 2 Table Select
@ 1999-05-30 11:24  Herouth Maoz <herouth@oumail.openu.ac.il>
  parent: Andy Lewis <alewis@themecca.net>
  1 sibling, 0 replies; 4+ messages in thread

From: Herouth Maoz @ 1999-05-30 11:24 UTC (permalink / raw)
  To: pgsql-sql

At 22:34 +0300 on 27/05/1999, Oliver Elphick wrote:


>
> select c.company_id, count(j.job_id)
>    from company as c, jobs as j
>    where c.company_id = j.company_id
>    group by c.company_id;

If it's only the company_id that interests you, and not the company name or
other details in the company table, you can give up the company table
altogether. The company_id appears in the jobs table, and there is no point
in selecting c.company_id when the condition was that c.company_id is equal
to j.company_id.

In other words, if all you need is company_id, you can use:

select company_id, count(job_id)
from jobs
group by company_id;

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





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


end of thread, other threads:[~1999-05-30 11:24 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-05-27 16:23 2 Table Select Andy Lewis <alewis@themecca.net>
1999-05-27 19:34 ` Oliver Elphick <olly@lfix.co.uk>
1999-05-27 20:37   ` Andy Lewis <alewis@themecca.net>
1999-05-30 11:24 ` Herouth Maoz <herouth@oumail.openu.ac.il>

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