SQL query help!

From: "Arcadius A(dot)" <ahouans(at)sh(dot)cvut(dot)cz>
To: pgsql-sql(at)postgresql(dot)org
Subject: SQL query help!
Date: 2002-11-23 22:09:37
Message-ID: arouav$2339$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-sql

Hello!

I hope that someone here could help.

I'm using PostgreSQL7.1.3

I have 3 tables in my DB: the tables are defined in the following way:

CREATE TABLE category(
id SERIAL NOT NULL PRIMARY KEY,
// etc etc

)
;

CREATE TABLE subcategory(
id SERIAL NOT NULL PRIMARY KEY,
categoryid int CONSTRAINT subcategory__ref_category
REFERENCES category (id)
// etc etc
)
;

CREATE TABLE entry(
entryid SERIAL NOT NULL PRIMARY KEY,
isapproved CHAR(1) NOT NULL DEFAULT 'n',
subcategoryid int CONSTRAINT entry__ref_subcategory
REFERENCES subcategory (id)
// atd
,
)
;

I have the following SQL query :

"SELECT * FROM entry where isapproved='y' AND subcategoryid IN (SELECT id
FROM subcategory WHERE
categoryid='"+catID+"') ORDER BY subcategoryid DESC";

For a given categoryid( catID), the query will return all entries in the
"entry" table
having a corresponding subcategoryid(s)[returned by the inned subquery].

But I want to return only a limited number of entries of each
subcategory..... let's say that I want to return at most 5 entries of each
subcategory type ( for instance if the inner subquery returns 3 results,
thus I will be having in total at most 15 entries as relust)....

How can this be achieved?

I'm aware of postgreSQL "LIMIT" and "GROUP BY" clause..... but so far, I'm
not able to put all this together...

Thanks in advance.

Arcadius.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Aleksei Wolff 2002-11-24 00:54:07 GUI for Postgres Management? PostgreSQL vs MySQL.
Previous Message Medi Montaseri 2002-11-23 18:46:59 Re: Changing the type of a column in an already populated

Browse pgsql-hackers by date

  From Date Subject
Next Message Christopher Kings-Lynne 2002-11-23 23:48:25 Help with ADD COLUMN
Previous Message Tom Lane 2002-11-23 21:41:37 Re: performance of insert/delete/update

Browse pgsql-sql by date

  From Date Subject
Next Message Kenneth Evans 2002-11-24 17:21:23 copy from command - quotes and header lines
Previous Message Arcadius A. 2002-11-23 12:33:03 LIMIT and/or GROUP BY help!