If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > 3 reviews per postal code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-19-12, 16:18
pavan_test pavan_test is offline
Registered User
 
Join Date: Oct 2005
Posts: 64
3 reviews per postal code

hello,

I have 2 tables reviews and postal code.

table reviews: has 2 column party ID and review
table postal code: also has 2 column party id and zip code.

I have to retrieve 3 reviews per postal code.

can someone please tell me how do i write a sql query.

Thanks
pavan
Reply With Quote
  #2 (permalink)  
Old 07-19-12, 17:10
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 7,137
Where rownum < 4
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #3 (permalink)  
Old 07-19-12, 17:16
pavan_test pavan_test is offline
Registered User
 
Join Date: Oct 2005
Posts: 64
3 reviews per postal code

I tried rownum<4 it retured first 3 records in the table.

select postal_cd, review
from postal_code, reviews
where postal_code.pty_id = reviews.pty_id
and rownum <4


I am looking for 3 reviews for each postal code in the 2 tables.

Thanks
Pavan
Reply With Quote
  #4 (permalink)  
Old 07-19-12, 17:25
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 7,137
>select postal_cd, review
above is interesting since POSTAL_CD did not exist in orginal post.

>I tried rownum<4 it retured first 3 records in the table.
OK, but this only means the SQL needs to be changed to actually meet the requirements.
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #5 (permalink)  
Old 07-19-12, 17:34
pavan_test pavan_test is offline
Registered User
 
Join Date: Oct 2005
Posts: 64
3 reviews per postal code

Can somoone please tell me how can I retreive 3 rows per group.

I have 2 tables reviews and postal code.

table reviews: has 2 column party ID and review
table postal code: also has 2 column party id and zip code. (or postal code)

I have to retrieve 3 reviews per postal code.

Thanks
Pavan
Reply With Quote
  #6 (permalink)  
Old 07-20-12, 01:13
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,871
Here's an example based on Scott's schema.

Employees, sorted by department numbers and their names:
Code:
SQL> select deptno, ename from emp
  2  order by deptno, ename;

    DEPTNO ENAME
---------- ----------
        10 CLARK
        10 KING
        10 MILLER
        20 ADAMS
        20 FORD
        20 JONES
        20 SCOTT
        20 SMITH
        30 ALLEN
        30 BLAKE
        30 JAMES
        30 MARTIN
        30 TURNER
        30 WARD

14 rows selected.

SQL>
I want to select 2 employees per each department; here's one option:
Code:
SQL> select deptno, ename
  2  from
  3    (select deptno,
  4            ename,
  5            row_number() over (partition by deptno order by deptno, ename) rn
  6     from emp
  7    )
  8  where rn <= 2;

    DEPTNO ENAME
---------- ----------
        10 CLARK
        10 KING
        20 ADAMS
        20 FORD
        30 ALLEN
        30 BLAKE

6 rows selected.

SQL>
Now, apply that to your data.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On