pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
RE: Two table select (fwd)
3+ messages / 2 participants
[nested] [flat]

* RE: Two table select (fwd)
@ 1999-02-03 19:09  Jackson, DeJuan <djackson@cpsgroup.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Jackson, DeJuan @ 1999-02-03 19:09 UTC (permalink / raw)
  To: Andy <alewis@mpsi.net>; djackson@cpsgroup.com; +Cc: pgsql-sql

You could use unions, but I must ask why do you have all those rbldg#
columns in your table?
What's the basic thought behind your database layout?
	-DEJ

> -----Original Message-----
> From: Andy [mailto:alewis@mpsi.net]
> Sent: Wednesday, February 03, 1999 1:00 PM
> To: djackson@cpsgroup.com
> Subject: Two table select (fwd)
> 
> 
> The apts table is at the bottom.
> 
> I guess what I am trying to do is to pick each rbldg[x] from 
> the referral query
> and then take that and query the apts table for the bldgname.
> 
> rbldg[x] is the apt building number and bldgname is the building name.
> 
> Thanks
> 
> Andy
> 
> 
> 
> I have two queries that I'd like to make as one. Here are the queries:
> 
> select * from referral where oid = $some_oid
> 
> select bldgname from apts where bldgnum = 'rbldg1'
> 
> 
> I am trying to get the bldgname from the apts table and want 
> to get all of the
> bldgname's including rbldg2, rbldg3, rbldg4. Trying to do it 
> in one select
> statment, is this possible?
> 
> 
> 
> table = referral
> +----------------------------------+--------------------------
> --------+-------+
> | rstate                           | varchar()                
>         |     2 |
> | rrif                             | varchar()                
>         |    10 |
> | rphone                           | varchar()                
>         |    20 |
> | rfirstname                       | varchar()                
>         |    20 |
> | rlastname                        | varchar()                
>         |    25 |
> | rbldg1                           | varchar()                
>         |    10 |
> | rbldgcomment1                    | text                     
>         |   var |
> | rbldg2                           | varchar()                
>         |    10 |
> | rbldgcomment2                    | text                     
>         |   var |
> | rbldg3                           | varchar()                
>         |    10 |
> | rbldgcomment3                    | text                     
>         |   var |
> | rbldg4                           | varchar()                
>         |    10 |
> ....
> +----------------------------------+--------------------------
> --------+-------+
> 
> Table    = apts
> +----------------------------------+--------------------------
> --------+-------+
> |              Field               |              Type        
>         | Length|
> +----------------------------------+--------------------------
> --------+-------+
> | bldgnum                          | varchar()                
>         |    10 |
> | bldgname                         | varchar()                
>         |    35 |
> ...
> +----------------------------------+--------------------------
> --------+-------+
> 
> Thanks in advance
> 
> Andy
> 
> 
> 



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

* RE: Two table select (fwd)
@ 1999-02-03 19:11  Andy <alewis@mpsi.net>
  parent: Jackson, DeJuan <djackson@cpsgroup.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Andy @ 1999-02-03 19:11 UTC (permalink / raw)
  To: Jackson, DeJuan <djackson@cpsgroup.com>; +Cc: pgsql-sql

On Wed, 3 Feb 1999, Jackson, DeJuan wrote:

>You could use unions, but I must ask why do you have all those rbldg#
>columns in your table?
>What's the basic thought behind your database layout?
>	-DEJ

The rbldg#'s are holding Apts building numbers and the rbldgcomment#'s are
holding the comments for those buildings.

Andy


>
>> -----Original Message-----
>> From: Andy [mailto:alewis@mpsi.net]
>> Sent: Wednesday, February 03, 1999 1:00 PM
>> To: djackson@cpsgroup.com
>> Subject: Two table select (fwd)
>> 
>> 
>> The apts table is at the bottom.
>> 
>> I guess what I am trying to do is to pick each rbldg[x] from 
>> the referral query
>> and then take that and query the apts table for the bldgname.
>> 
>> rbldg[x] is the apt building number and bldgname is the building name.
>> 
>> Thanks
>> 
>> Andy
>> 
>> 
>> 
>> I have two queries that I'd like to make as one. Here are the queries:
>> 
>> select * from referral where oid = $some_oid
>> 
>> select bldgname from apts where bldgnum = 'rbldg1'
>> 
>> 
>> I am trying to get the bldgname from the apts table and want 
>> to get all of the
>> bldgname's including rbldg2, rbldg3, rbldg4. Trying to do it 
>> in one select
>> statment, is this possible?
>> 
>> 
>> 
>> table = referral
>> +----------------------------------+--------------------------
>> --------+-------+
>> | rstate                           | varchar()                
>>         |     2 |
>> | rrif                             | varchar()                
>>         |    10 |
>> | rphone                           | varchar()                
>>         |    20 |
>> | rfirstname                       | varchar()                
>>         |    20 |
>> | rlastname                        | varchar()                
>>         |    25 |
>> | rbldg1                           | varchar()                
>>         |    10 |
>> | rbldgcomment1                    | text                     
>>         |   var |
>> | rbldg2                           | varchar()                
>>         |    10 |
>> | rbldgcomment2                    | text                     
>>         |   var |
>> | rbldg3                           | varchar()                
>>         |    10 |
>> | rbldgcomment3                    | text                     
>>         |   var |
>> | rbldg4                           | varchar()                
>>         |    10 |
>> ....
>> +----------------------------------+--------------------------
>> --------+-------+
>> 
>> Table    = apts
>> +----------------------------------+--------------------------
>> --------+-------+
>> |              Field               |              Type        
>>         | Length|
>> +----------------------------------+--------------------------
>> --------+-------+
>> | bldgnum                          | varchar()                
>>         |    10 |
>> | bldgname                         | varchar()                
>>         |    35 |
>> ...
>> +----------------------------------+--------------------------
>> --------+-------+
>> 
>> Thanks in advance
>> 
>> Andy
>> 
>> 
>> 
>




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

* RE: Two table select (fwd)
@ 1999-02-03 20:57  Jackson, DeJuan <djackson@cpsgroup.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Jackson, DeJuan @ 1999-02-03 20:57 UTC (permalink / raw)
  To: Andy <alewis@mpsi.net>; +Cc: pgsql-sql

I thought as much... the best way to do a single select, with what you
currently have,  would be to use a union all clause.
SELECT r.bldg1, ..., a.bldgname 
FROM refferal r, apts a
WHERE r.bldg1 = a.bldgnum
UNION ALL
SELECT r.bldg2, ..., a.bldgname 
FROM refferal r, apts a
WHERE r.bldg2 = a.bldgnum
UNION ALL
.
.
.
Now if you normalize your data into three table you could make it a
single select (w/o UNION) with a three table join.  BTW it's a good idea
to place non-variable length fields before variable length ones in a
table definition.
i.e.
table = referral
+----------------------------------+----------------------------------+-
------+
| ref_id                           | int4                             |
4 |
| rstate                           | varchar()                        |
2 |
| rrif                             | varchar()                        |
10 |
| rphone                           | varchar()                        |
20 |
| rfirstname                       | varchar()                        |
20 |
| rlastname                        | varchar()                        |
25 |
+----------------------------------+----------------------------------+-
------+

table = refbldg
+----------------------------------+----------------------------------+-
------+
|              Field               |              Type                |
Length|
+----------------------------------+----------------------------------+-
------+
| ref_id                           | int4                             |
4 |
| rbldg                            | varchar()                        |
10 |
| rbldgcomment                     | text                             |
var |
+----------------------------------+----------------------------------+-
------+

table = apts
+----------------------------------+----------------------------------+-
------+
|              Field               |              Type                |
Length|
+----------------------------------+----------------------------------+-
------+
| bldgnum                          | varchar()                        |
10 |
| bldgname                         | varchar()                        |
35 |
 ...
+----------------------------------+----------------------------------+-
------+

SELECT r.*, rb.refbldg, rb.rbldgcomment, a.bldgname
FROM referral r. refbldg rb, apts a
WHERE r.ref_if = rb.ref_if AND 
      rb.rbldg = a.bldgnum



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


end of thread, other threads:[~1999-02-03 20:57 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-03 19:09 RE: Two table select (fwd) Jackson, DeJuan <djackson@cpsgroup.com>
1999-02-03 19:11 ` Andy <alewis@mpsi.net>
1999-02-03 20:57 RE: Two table select (fwd) Jackson, DeJuan <djackson@cpsgroup.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