Getting top 2 by Category - Mailing list pgsql-sql

From Ozer, Pam
Subject Getting top 2 by Category
Date
Msg-id 216FFB77CBFAEE4B8EE4DF0A939FF1D1018440@mail-001.corp.automotive.com
Whole thread Raw
Responses Re: Getting top 2 by Category
Re: Getting top 2 by Category
List pgsql-sql

This is probably very simple but I am drawing a blank.  Do I need to create a cursor to iterate through a table to grab the top 2 magazines per category?  Here is my table and some data .  The results I need are at the  bottom.  Any help would be greatly appreciated:

 

CREATE TABLE magazinecategory

(

  magazinecategoryid smallint NOT NULL ,

  magazineid smallint,

  categoryid smallint

);

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (1, 2, 3);

 

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (2, 8, 3);

 

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (3 9, 3);

 

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (4, 10, 4);

 

 

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (5, 11, 4);

 

INSERT INTO magazinecategory(

            magazinecategoryid, magazineid, categoryid)

    VALUES (6, 12,4);

 

 

 

The results I want are

CategoryID  MagazineID

3 2

3 8

4 10

4 11

 

 

 

Pam Ozer

pgsql-sql by date:

Previous
From: "Amar Dhole"
Date:
Subject: help needs in converting db2 function in postgresql.
Next
From: "Ozer, Pam"
Date:
Subject: Re: Getting top 2 by Category