pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Arcadius A. <ahouans@sh.cvut.cz>
To: pgsql-sql@postgresql.org
Subject: LIMIT and/or GROUP BY help!
Date: Sat, 23 Nov 2002 13:33:03 +0100
Message-ID: <arnse6$iia$1@main.gmane.org> (raw)

Hello!

 I hope that someone here could help.

I'm using PostgreSQL7.1.3

I have 3 tables: entry, subcategory and category.

The table "entry" has a foreign key "subcategoryid" with reference to the
table "subcategory", and the "subcategory" table has a foreign key
"categoryid" with reference to the table "category"

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, this will return all entries in the "entry" table
having a corresponding subcategoryid(s).

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 )....

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.








Message-ID: <arnse6$iia$1@main.gmane.org>
Permalink:  ../arnse6$iia$1@main.gmane.org/
Also on:    postgresql.org/message-id/arnse6$iia$1@main.gmane.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ahouans@sh.cvut.cz
  Subject: Re: LIMIT and/or GROUP BY help!
  In-Reply-To: <arnse6$iia$1@main.gmane.org>

* 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