dbTalk Databases Forums  

combining two queries?

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss combining two queries? in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mark Harrison
 
Posts: n/a

Default combining two queries? - 10-22-2004 , 06:55 PM






How can I combine these two queries?

# select viewerid,count(*) from viewer_movies group by viewerid order by viewerid;
viewerid | count
----------+--------
22964835 | 3055
22964836 | 1291
22964837 | 3105
22964838 | 199


planb=# select name from xenons where id = 23500637;
name
---------
x.moray

I would like to end up with a query result like this:

viewer | count
----------+--------
x.surf | 3055
x.dream | 1291
x.moray | 3105
x.sleepy | 199

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #2  
Old   
Duane Lee - EGOVX
 
Posts: n/a

Default Re: combining two queries? - 10-22-2004 , 07:26 PM






Try

select a.name,count(*) from
xenons as a,
viewer_movies as b
where a.id = b.viewerid
group by a.name order by a.name;

-----Original Message-----
From: Mark Harrison [mailto:mh (AT) pixar (DOT) com]
Sent: Friday, October 22, 2004 4:55 PM
To: pgsql-general (AT) postgresql (DOT) org
Subject: [GENERAL] combining two queries?

How can I combine these two queries?

# select viewerid,count(*) from viewer_movies group by viewerid order by
viewerid;
viewerid | count
----------+--------
22964835 | 3055
22964836 | 1291
22964837 | 3105
22964838 | 199


planb=# select name from xenons where id = 23500637;
name
---------
x.moray

I would like to end up with a query result like this:

viewer | count
----------+--------
x.surf | 3055
x.dream | 1291
x.moray | 3105
x.sleepy | 199

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #3  
Old   
Eddy Macnaghten
 
Posts: n/a

Default Re: combining two queries? - 10-23-2004 , 09:18 AM



select b.name as viewer, count(*)
from viewer_movies a, xenons b
where b.id = a.viewerid
group by b.name


On Sat, 2004-10-23 at 00:55, Mark Harrison wrote:
Quote:
How can I combine these two queries?

# select viewerid,count(*) from viewer_movies group by viewerid order by viewerid;
viewerid | count
----------+--------
22964835 | 3055
22964836 | 1291
22964837 | 3105
22964838 | 199


planb=# select name from xenons where id = 23500637;
name
---------
x.moray

I would like to end up with a query result like this:

viewer | count
----------+--------
x.surf | 3055
x.dream | 1291
x.moray | 3105
x.sleepy | 199

Many TIA!
Mark
--
Edward A. Macnaghten
http://www.edlsystems.com


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #4  
Old   
Jeffrey Melloy
 
Posts: n/a

Default Re: combining two queries? - 10-25-2004 , 02:18 PM



If you want to return rows with zeros, you may need to do something like
this:

select b.name as viewer, count(viewerid)
from xenons b left join viewer_movies a on (b.id = a.viewerid)
group by b.name

Eddy Macnaghten wrote:

Quote:
select b.name as viewer, count(*)
from viewer_movies a, xenons b
where b.id = a.viewerid
group by b.name


On Sat, 2004-10-23 at 00:55, Mark Harrison wrote:


How can I combine these two queries?

# select viewerid,count(*) from viewer_movies group by viewerid order by viewerid;
viewerid | count
----------+--------
22964835 | 3055
22964836 | 1291
22964837 | 3105
22964838 | 199


planb=# select name from xenons where id = 23500637;
name
---------
x.moray

I would like to end up with a query result like this:

viewer | count
----------+--------
x.surf | 3055
x.dream | 1291
x.moray | 3105
x.sleepy | 199

Many TIA!
Mark



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.