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
173 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
6 Comments
 
LVL 6

Expert Comment

by:divyeshhdoshi
Comment Utility
select top 4 col1, col2 from table name ;
0
 

Expert Comment

by:francasado
Comment Utility
try this:

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

Author Comment

by:lapucca
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
You are a genius.  Thank you very much!
0
 
LVL 6

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now