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
175 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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