Solved

Is there a way to select no more than 4 rows in a Select statement even when there is more than 4 that fits the Select?

Posted on 2008-10-17
6
179 Views
Last Modified: 2010-03-20
Hi, I have a tabel with 2 columns.  Is there a way to write a SELECT statement to select both columns form this table but limit the return result to no more than 4 rows when there could be more than 4 or less than 4 rows that fits the filter?  Thank you.
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22739598
select top 4 col1, col2 from table name ;
0
 

Expert Comment

by:francasado
ID: 22739602
try this:

Select top 4 column1, column2 from table where...
0
 

Author Comment

by:lapucca
ID: 22739679
Sorry, didn't make my question clear.  I want to retrieve the top 4 rows of records that has the same col1 value and select them into a new table.  Is this possible?
col1                  col2
10                    a
10                    b
10                   c
10                    d
20                    x
20                     y

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22739783
this should do:;
SELECT col1, col2
  FROM ( select col1, col2, row_number() over(partition by col1 order by col2) r from yourtable ) as sq
 WHERE sq.r <= 4
 

Open in new window

0
 

Author Comment

by:lapucca
ID: 22739868
You are a genius.  Thank you very much!
0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22739870
select * from
(
select col1, col2, Rank() over (partition by col1 order by col2) as rank from table
) tmp
where rank <=4
 
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question