Re: Help me with this multi-table query

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help me with this multi-table query
Date: 2010-03-26 06:45:29
Message-ID: 20100326064528.GB6817@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In response to Nilesh Govindarajan :
> Hi,
>
> I want to find out the userid, nodecount and comment count of the userid.
>
> I'm going wrong somewhere.
>
> Check my SQL Code-

Check my example:

test=*# select * from u;
id
----
1
2
3
(3 rows)

test=*# select * from n;
uid
-----
1
1
1
2
2
3
(6 rows)

test=*# select * from c;
uid
-----
1
2
2
3
3
3
(6 rows)

test=*# select foo.id, foo.n_count, count(c) from (select u.id, count(n)
as n_count from u right join n on (n.uid=u.id) group by 1) foo left join
c on foo.id=c.uid group by id, n_count;
id | n_count | count
----+---------+-------
1 | 3 | 1
2 | 2 | 2
3 | 1 | 3
(3 rows)

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jayadevan M 2010-03-26 06:56:54 Re: SQL syntax rowcount value as an extra column in the result set
Previous Message A. Kretschmer 2010-03-26 06:23:50 Re: SQL syntax rowcount value as an extra column in the result set