Re: [GENERAL] Combining two queries

2014-12-19 Thread Marc Mamin
>>I have a table called friends with a user_id and a friend_id (both of these >>relate to an id in a users table). >>For each friend relationship there are two rows. There are currently ONLY >>reciprocal relationships. So if user ids 1 and 2 are friends there will be >>two rows (1,2) and (2,1).

Re: [GENERAL] Combining two queries

2014-12-19 Thread Marc Mamin
>I have a table called friends with a user_id and a friend_id (both of these >relate to an id in a users table). >For each friend relationship there are two rows. There are currently ONLY >reciprocal relationships. So if user ids 1 and 2 are friends there will be two >rows (1,2) and (2,1). >For

Re: [GENERAL] Combining two queries

2014-12-18 Thread Robert DiFalco
The INNER JOIN to itself with a count turns out to have a lower cost query plan than the INTERSECT approach. On the ROW approach, it also seems to take longer than the simple EXISTS query. But I suppose I can put both of those into CTEs for convenience. I guess I was just hoping there was a lower c

Re: [GENERAL] Combining two queries

2014-12-18 Thread Patrick Krecker
On Thu, Dec 18, 2014 at 1:57 PM, Robert DiFalco wrote: > Thanks! So how would I combine them so that I would get a single row with > the mutual friend count and isFriends for a given pair of users? I can't > figure out how to modify what you've posted so that it gives the results > like the compou

Re: [GENERAL] Combining two queries

2014-12-18 Thread David Johnston
On Thu, Dec 18, 2014 at 3:02 PM, Robert DiFalco wrote: > Is the intersect any better than what I originally showed? On the ROW > approach, I'm not sure where the context for that is coming from since it > may not be in the intersection. Consider n1 and n2 are NOT friends but they > have >0 mutual

Re: [GENERAL] Combining two queries

2014-12-18 Thread Robert DiFalco
Is the intersect any better than what I originally showed? On the ROW approach, I'm not sure where the context for that is coming from since it may not be in the intersection. Consider n1 and n2 are NOT friends but they have >0 mutual friends between them. On Thu, Dec 18, 2014 at 1:29 PM, David G

Re: [GENERAL] Combining two queries

2014-12-18 Thread John McKown
Wow, I sure went overboard with the "friendship chain" thought. I don't know where I got the idea that was your question. On Thu, Dec 18, 2014 at 3:46 PM, John McKown wrote: > > On Thu, Dec 18, 2014 at 2:10 PM, Robert DiFalco > wrote: > >> I have a table called friends with a user_id and a frien

Re: [GENERAL] Combining two queries

2014-12-18 Thread John McKown
On Thu, Dec 18, 2014 at 2:10 PM, Robert DiFalco wrote: > I have a table called friends with a user_id and a friend_id (both of > these relate to an id in a users table). > > For each friend relationship there are two rows. There are currently ONLY > reciprocal relationships. So if user ids 1 and

Re: [GENERAL] Combining two queries

2014-12-18 Thread Patrick Krecker
On Thu, Dec 18, 2014 at 12:10 PM, Robert DiFalco wrote: > I have a table called friends with a user_id and a friend_id (both of these > relate to an id in a users table). > > For each friend relationship there are two rows. There are currently ONLY > reciprocal relationships. So if user ids 1 and

Re: [GENERAL] Combining two queries

2014-12-18 Thread David G Johnston
Robert DiFalco wrote > For 2 arbitrary ids, I need a query to get two pieced of data: >* Are the two users friends? This seems easy...ROW(u_id, f_id) = ROW(n1, n2) >* How many friends do the two users have in common. SELECT f_id FROM [...] WHERE u_id = n1 INTERSECT SELECT f_id FROM [...

[GENERAL] Combining two queries

2014-12-18 Thread Robert DiFalco
I have a table called friends with a user_id and a friend_id (both of these relate to an id in a users table). For each friend relationship there are two rows. There are currently ONLY reciprocal relationships. So if user ids 1 and 2 are friends there will be two rows (1,2) and (2,1). For 2 arbit

Re: [GENERAL] combining two queries?

2004-10-25 Thread Jeffrey Melloy
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: select b.name as viewer, count(*) from viewer_movies a, xenons b where b.id

Re: [GENERAL] combining two queries?

2004-10-23 Thread Eddy Macnaghten
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 |

Re: [GENERAL] combining two queries?

2004-10-22 Thread Duane Lee - EGOVX
Title: RE: [GENERAL] combining two queries? 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:[EMAIL PROTECTED]] Sent: Friday, October 22, 2004 4:55 PM To: [EMAIL

[GENERAL] combining two queries?

2004-10-22 Thread Mark Harrison
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