Solved

Limititing number of rows returned

Posted on 2002-06-14
10
378 Views
Last Modified: 2008-02-01
Hi

I am trying to achieve the following

I have a table with many records. Each record has an account number field.

I want to get the last 3 records entered for each of the accounts into a recordset.

Is there an sql select query which might achieve this.

thanks in advance
0
Comment
Question by:Paddyo
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:mmcmillen
Comment Utility
what will tell you the records are the "last" for this account number?  
0
 
LVL 5

Expert Comment

by:spcmnspff
Comment Utility
Yes this is possible I need some more info about the table.  Can you send me the layout.  Is there an Identity field, if so this would work:


Select Distinct *
FROM Table T2
WHERE ID IN
(Select TOP 3 ID
from table T1
Where T1.AccountNum = T2.AccountNum
Order By ID Desc)
0
 
LVL 1

Expert Comment

by:johan_brohn
Comment Utility
Replace the myTable with the real table name
and replace SortColumn with the real column to sort on.

SELECT *
FROM myTable A
WHERE (SELECT Count(*) FROM myTable B WHERE B.SortColumn < A.SortColumn AND A.Account = B.Account) IN (1,2,3)
ORDER BY Account, SortColumn
0
 
LVL 2

Expert Comment

by:mmcmillen
Comment Utility
what will tell you the records are the "last" for this account number?  
0
 

Author Comment

by:Paddyo
Comment Utility
mmcmillen

I have an identity field.
__________________-

I am entering a range of accounts on a from

i.e. txtLwrAccCode
     txtUprAccCode

I then want to select the last ??? number of records for each of the account codes in the range.

The last ??? number will be entered at run time by the user.




0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:johan_brohn
Comment Utility
Sorry

To get the three last records (sorted by sortColumn) for each account code.

SELECT *
FROM myTable A
WHERE (SELECT Count(*) FROM myTable B WHERE B.SortColumn > A.SortColumn AND A.Account = B.Account) IN
(0,1,2)
ORDER BY Account, SortColumn
0
 
LVL 35

Expert Comment

by:David Todd
Comment Utility
Hi,

What about

Select top 3 *
from myTableA
where Account = <account in question>
order by Account asc, LastSavedDate desc

Regards
  David

0
 

Author Comment

by:Paddyo
Comment Utility
Thanks for all the suggestions

I have tried some of them without achieving the required results.

Johan Brohn:

I dont fully understand what to do with your suggestion. I am only reading from one table yet you are suggesting that I compare and sort fields from 2 tables. I do understand the IN (0,1,2) element of the statement.

dtodd:
I need the the last few records for several accounts. You suggestion would work perfectly for one account

Thanks again for the suggestions.
0
 
LVL 1

Accepted Solution

by:
johan_brohn earned 300 total points
Comment Utility
>> I am only reading from one table yet you are
suggesting that I compare and sort fields from 2 tables.

No, one table to alias. The table is "myTable" and the alias are "A" and "B".

>> I do understand the IN (0,1,2) element of the
statement.

Example:

myTable
id, account
-------
1,'a'
2,'a'
3,'a'
4,'a'
5,'a'
6,'b'
7,'c'
8,'c'
9,'c'

Result:
id, account
-------
3,'a' there exists 2 with higher id for 'a'
4,'a' there exists 1 with higher id for 'a'
5,'a' there exists 0 with higher id for 'a'
6,'b' there exists 0 with higher id for 'b'
7,'c' there exists 2 with higher id for 'c'
8,'c' there exists 1 with higher id for 'c'
9,'c' there exists 0 with higher id for 'c'

Not within the result:
1,'a' there exists 4 with higher id for 'a'
2,'a' there exists 3 with higher id for 'a'

--get rows from myTable
SELECT *
FROM myTable A
--for each row, count the numbers of rows with a higher
--id but the very same account "B.id > A.id".
--if there only exists 0, 1 or 2 "IN (0,1,2)" records with higher id for the very same account then get it, otherwise just ignore it.
WHERE (SELECT Count(*) FROM myTable B WHERE B.id > A.id AND A.Account = B.Account) IN (0,1,2)
ORDER BY Account, id

0
 

Author Comment

by:Paddyo
Comment Utility
Thank you very much Johan Brohn

I think its obvious that I am not the worlds greatest expert on SQL but I am learning and with help such as I have received I will quickly become competent.

Thank you again.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

743 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

17 Experts available now in Live!

Get 1:1 Help Now