pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: [SQL] [Q] Merging two queries?
2+ messages / 2 participants
[nested] [flat]
* [Q] Merging two queries?
@ 1999-12-18 23:00 angelko@saw.net
1999-12-18 22:48 ` Re: [SQL] [Q] Merging two queries? Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: angelko@saw.net @ 1999-12-18 23:00 UTC (permalink / raw)
To: pgsql-sql
Hello,
I'm working on a query that confuses me. This is a database of vehicles,
and dealers. four tables are involved.
Dealership, UsedCar, Members, Ads
Dealership contains the information about various car dealerships.
UsedCar contains the information about the cars those dealerships sell.
Members is similar to Dealership, except it is for car owners who
are sellilng their own vehicle. Each owner from Members has one or
more entries in Ads table. Ads table contains the information about
the used cars that members want to sell.
Here is a sample query involving Dealership and UsedCar
SELECT DISTINCT UsedCar.Make, UsedCar.Model
FROM Dealership, UsedCar
WHERE Dealership.DealerID = UsedCar.DealerID
AND Dealership.States Like '%Oregon%'
And the sample output is:
Pontiac |Grand prix se sedan 4d
Pontiac |Sunfire se sedan 4d
Saturn |Sc1 coupe 2d
Saturn |Sc2 coupe 2d
Here is another query involving Members and Ads
SELECT DISTINCT Ads.Make, Ads.Model
FROM Members, Ads
WHERE Members.Member_ID = Ads.Member_ID
AND Members.Addr_State = 'OR'
And the sample output is:
Pontiac |Sunfire se sedan 2d
Saturn |Sc2 coupe 4d
I would like to create a single query that will generate output like:
Pontiac |Grand prix se sedan 4d
Pontiac |Sunfire se sedan 2d
Pontiac |Sunfire se sedan 4d
Saturn |Sc1 coupe 2d
Saturn |Sc2 coupe 2d
Saturn |Sc2 coupe 4d
Anyone has any suggestions?
Thanks,
-angelko
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: [SQL] [Q] Merging two queries?
1999-12-18 23:00 [Q] Merging two queries? angelko@saw.net
@ 1999-12-18 22:48 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 1999-12-18 22:48 UTC (permalink / raw)
To: angelko@saw.net; +Cc: pgsql-sql
That's an easy one: SELECT ... UNION SELECT ... does exactly what
you need. You can leave DISTINCT off the individual SELECTs because
UNION implies a DISTINCT pass over the merged output. (Use UNION ALL
if you don't want a final DISTINCT pass.)
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~1999-12-18 23:00 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-12-18 23:00 [Q] Merging two queries? angelko@saw.net
1999-12-18 22:48 ` Tom Lane <tgl@sss.pgh.pa.us>
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