?
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
Medium Priority
?
180 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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