Solved

Limititing number of rows returned

Posted on 2002-06-14
10
380 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
ID: 7078209
what will tell you the records are the "last" for this account number?  
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7078210
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
ID: 7078233
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
ID: 7078252
what will tell you the records are the "last" for this account number?  
0
 

Author Comment

by:Paddyo
ID: 7078266
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Expert Comment

by:johan_brohn
ID: 7078293
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
ID: 7085796
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
ID: 7087791
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
ID: 7089133
>> 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
ID: 7089561
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

911 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

22 Experts available now in Live!

Get 1:1 Help Now