dbTalk Databases Forums  

[SQL] Getting top 2 by Category

mailing.database.pgsql-sql mailing.database.pgsql-sql


Discuss [SQL] Getting top 2 by Category in the mailing.database.pgsql-sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ozer, Pam
 
Posts: n/a

Default [SQL] Getting top 2 by Category - 01-11-2011 , 01:00 PM






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

Reply With Quote
  #2  
Old   
Peter Steinheuser
 
Posts: n/a

Default Re: [SQL] Getting top 2 by Category - 01-11-2011 , 01:51 PM






Well, if yoi have PG 8.4 and above -

select categoryid, magazineid from (
select row_number() over (partition by categoryid order by
categoryid,magazineid asc) as row_number,
categoryid, magazineid from magazinecategory) foo
where row_number < 3;
categoryid | magazineid
------------+------------
3 | 2
3 | 8
4 | 10
4 | 11
(4 rows)


On Tue, Jan 11, 2011 at 2:00 PM, Ozer, Pam <pozer (AT) automotive (DOT) com> wrote:

Quote:
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*



--
Peter Steinheuser
psteinheuser (AT) myyearbook (DOT) com

Reply With Quote
  #3  
Old   
Ozer, Pam
 
Posts: n/a

Default Re: [SQL] Getting top 2 by Category - 01-11-2011 , 01:53 PM



Perfect. Thank You. I knew there had to be something simple.



From: Peter Steinheuser [mailtosteinheuser (AT) myyearbook (DOT) com]
Sent: Tuesday, January 11, 2011 11:52 AM
To: Ozer, Pam
Cc: pgsql-sql (AT) postgresql (DOT) org
Subject: Re: [SQL] Getting top 2 by Category



Well, if yoi have PG 8.4 and above -

select categoryid, magazineid from (
select row_number() over (partition by categoryid order by
categoryid,magazineid asc) as row_number,
categoryid, magazineid from magazinecategory) foo
where row_number < 3;
categoryid | magazineid
------------+------------
3 | 2
3 | 8
4 | 10
4 | 11
(4 rows)



On Tue, Jan 11, 2011 at 2:00 PM, Ozer, Pam <pozer (AT) automotive (DOT) com> wrote:

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




--
Peter Steinheuser
psteinheuser (AT) myyearbook (DOT) com

Reply With Quote
  #4  
Old   
Carla
 
Posts: n/a

Default Re: [SQL] Getting top 2 by Category - 01-19-2011 , 02:11 PM



2011/1/11 Peter Steinheuser <psteinheuser (AT) myyearbook (DOT) com>

Quote:
Well, if yoi have PG 8.4 and above -

select categoryid, magazineid from (
select row_number() over (partition by categoryid order by
categoryid,magazineid asc) as row_number,
categoryid, magazineid from magazinecategory) foo
where row_number < 3;
categoryid | magazineid
------------+------------

3 | 2
3 | 8
4 | 10
4 | 11
(4 rows)


How can I do it in PG 8.3?


Quote:

On Tue, Jan 11, 2011 at 2:00 PM, Ozer, Pam <pozer (AT) automotive (DOT) com> wrote:

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*




--
Peter Steinheuser
psteinheuser (AT) myyearbook (DOT) com

Reply With Quote
  #5  
Old   
Peter Steinheuser
 
Posts: n/a

Default Re: [SQL] Getting top 2 by Category - 01-19-2011 , 02:59 PM



There's probably several ways - not saying this is best/optimal.

SELECT
categoryid, magazineid
FROM
magazinecategory a
WHERE (
SELECT
COUNT(*)
FROM
magazinecategory
WHERE
categoryid = a.categoryid
AND
magazineid <= a.magazineid
) < 3
order by categoryid, magazineid;



On Wed, Jan 19, 2011 at 3:11 PM, Carla <cgourofino (AT) hotmail (DOT) com> wrote:

Quote:
2011/1/11 Peter Steinheuser <psteinheuser (AT) myyearbook (DOT) com

Well, if yoi have PG 8.4 and above -

select categoryid, magazineid from (
select row_number() over (partition by categoryid order by
categoryid,magazineid asc) as row_number,
categoryid, magazineid from magazinecategory) foo
where row_number < 3;
categoryid | magazineid
------------+------------

3 | 2
3 | 8
4 | 10
4 | 11
(4 rows)


How can I do it in PG 8.3?




On Tue, Jan 11, 2011 at 2:00 PM, Ozer, Pam <pozer (AT) automotive (DOT) com> wrote:

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*




--
Peter Steinheuser
psteinheuser (AT) myyearbook (DOT) com




--
Peter Steinheuser
psteinheuser (AT) myyearbook (DOT) com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.