pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Yury Don <yura@vpcit.ru>
To: pgsql-sql@postgresql.org
Subject: Re: Trouble with subqueries
Date: Fri, 19 Jan 2001 18:05:42 +0500
Message-ID: <11131831464.20010119180542@vpcit.ru> (raw)
In-Reply-To: <3A67EE1A.B7319298@cc.hut.fi>
References: <3A67EE1A.B7319298@cc.hut.fi>

Hello Jussi,

Once, Friday, January 19, 2001, 12:34:50 PM, you wrote:

JV> I have the following two tables:

JV> create table movies (
JV>   name varchar(80),
JV>   info varchar(80),
JV>   length int,
JV>   primary key (name)
JV> );

JV> create table ratings (
JV>   name varchar(80),
JV>   userid varchar(10),
JV>   rating char(1),
JV>   foreign key (name) references movies,
JV>   primary key(name, userid)
JV> );

JV> The tables contain movies and users' ratings of the movies.
JV> I would like to get a listing of all the movies along with how many
JV> users have given the movie some particular rating. The first solution
JV> that I came up with was this:

JV> SELECT name, length, fives
JV>   FROM movies,
JV>        (SELECT name as rname,
JV>                count(*) as fives
JV>           FROM ratings
JV>           WHERE rating='5'
JV>           GROUP BY name)
JV>   WHERE name=rname;

JV> but in PostgreSQL 7 it just gives me this error message:
JV> ERROR:  parser: parse error at or near "("
JV> I have previously used similar queries in Oracle where they have worked,
JV> so it would seem to me that PostgreSQL doesn't support subselects after
JV> all despite all the claims.
JV> Am I doing something wrong or/and is there some another way of making
JV> this query that would work in PostgreSQL?


If I understand correctly it must looks like this:
SELECT name, length,
        (SELECT count(*)
           FROM ratings
           WHERE rating='5'
           and rating.name=movies.name) as fives
FROM movies
WHERE name=rname;

-- 
Best regards,
 Yury





view thread (4+ messages)  latest in thread

Message-ID: <11131831464.20010119180542@vpcit.ru>
Permalink:  ../11131831464.20010119180542@vpcit.ru/
Also on:    postgresql.org/message-id/11131831464.20010119180542@vpcit.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: yura@vpcit.ru
  Subject: Re: Trouble with subqueries
  In-Reply-To: <11131831464.20010119180542@vpcit.ru>
  References: <3A67EE1A.B7319298@cc.hut.fi> <11131831464.20010119180542@vpcit.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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