Solved

Return a select # records in an SQL

Posted on 1998-09-26
11
279 Views
Last Modified: 2010-03-19
Is there a way to return the first x number of records for an SQL using MS SQL 6.5?  For example, in MS Access, I can request 'select Top 10 from Authors'.  
0
Comment
Question by:EVision
  • 5
  • 4
  • 2
11 Comments
 
LVL 1

Expert Comment

by:kdimmock
ID: 1090278
SET ROWCOUNT 10
SELECT * FROM AUTHORS
SET ROWCOUNT 0
0
 

Author Comment

by:EVision
ID: 1090279
You are correct, but I did not give enough information.  I'm writing a TSQL statement and need to request

WHERE Person_ID not in ('Top 10 people ids from another SQL')

.therefore ROWCOUNT does not work.
0
 

Author Comment

by:EVision
ID: 1090280
I need to ask...

WHERE Person_ID not in ('Top 10 person ids from another SQL')
ROWCOUNT will not work.
0
 

Author Comment

by:EVision
ID: 1090281
Is it possible to use a server-side open cursor within an IN statement?
0
 
LVL 1

Expert Comment

by:kdimmock
ID: 1090282
couldn't you create a temp table corresponding to the sub query with only 10 rows?
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.

 

Author Comment

by:EVision
ID: 1090283
Possible, but inefficient.  It's for a bid agent on auction-type site.  For each insert in BIDS, I need to see if anyone has a MAX_ALLOWED_BID greater than the current lowest winning bid and not already a winner.  I'm trying to determine if the person is already a winner (TOP n Persons).  It's possible we may have 100 inserts a minute.  Too many times a temp table is created & deleted.  Any ideas???
0
 
LVL 1

Expert Comment

by:kdimmock
ID: 1090284
Now I'm losing the plot!  Show me a cut down (but now pseudo) version of the SQL as you'd have written in access
0
 
LVL 1

Expert Comment

by:kdimmock
ID: 1090285
typo:
Now I'm losing the plot!  Show me a cut down (but NOT pseudo) version of the SQL as you'd have written in access

0
 

Expert Comment

by:doubas
ID: 1090286
in english, how would you define a winner?  eg, does "the top N persons" mean the same thing as "the N highest bidders"?  please give the names of the pertinent tables/columns.
0
 

Author Comment

by:EVision
ID: 1090287
There is a table Bids that has Auction_Code, Bidding_Member_Code and Price_Per_Unit.  I want to know the N highest bids for Auction_Code X.
0
 

Accepted Solution

by:
doubas earned 100 total points
ID: 1090288
will something like this work?

.
where person_id not in(select bidding_member_code
                         from bids a
                        where a.auction_code = X
                          and N >= (select count(*)
                                      from bids b
                                     where b.auction_code = X
                                       and b.price_per_unit >= a.price_per_unit)
.


0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

20 Experts available now in Live!

Get 1:1 Help Now