Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access - Query - return 2 records for each

Posted on 2012-04-11
9
Medium Priority
?
278 Views
Last Modified: 2012-04-11
I need to return only 2 records for EACH [ExpenseAccount] in @groupedStrings from [Source_Data].  [Source_Data] has thousands of matches for each value in @groupedStrings

The query I have below returns all.  I only want 2 records for each.  How do I do that?


SELECT [@groupedStrings].ExpenseAccount, Source_Data.AssetNumber
FROM ([@groupedStrings] INNER JOIN ExpenseAccount_RAMBO ON [@groupedStrings].ExpenseAccount = ExpenseAccount_RAMBO.[Payable Expense Number]) INNER JOIN Source_Data ON ExpenseAccount_RAMBO.lessor = Source_Data.LessorCode;

Open in new window

0
Comment
Question by:keschuster
  • 5
  • 3
9 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 37834212
SELECT TOP 2 [@groupedStrings].ExpenseAccount, Source_Data.AssetNumber
FROM ([@groupedStrings] INNER JOIN ExpenseAccount_RAMBO ON [@groupedStrings].ExpenseAccount = ExpenseAccount_RAMBO.[Payable Expense Number]) INNER JOIN Source_Data ON ExpenseAccount_RAMBO.lessor = Source_Data.LessorCode;
0
 

Author Comment

by:keschuster
ID: 37834221
That only returns 2 records TOTAL.  I need 2 records from the joined table [Source_Data] for EACH matching record in @groupedStrings
0
 

Author Comment

by:keschuster
ID: 37834235
@groupedStrings contains the values

1
2
3

Source_Data contains

1
1
1
1
2
2
2
2
2
2
3
3
3
3
3

I want back just 2 records for each match.  So from source data I want

1
1
2
2
3
3
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37834455
among the numerous records of 1 's (for example)  what criteria are applied to get the two records?
0
 

Author Comment

by:keschuster
ID: 37834490
Let me better illustrate

The more I think about it my original stab at the sql may be too complicated.

The table looks like this

ExpenseAccount     | VIN
1                             | 10
1                             | 11
1                             | 12
2                             | 20
2                             | 21
2                             |22


So for a single ExpenseAccount there can be many VIN's.  Vins are unique

What I want to return is for each unique ExpenseAccount any 2 Vins
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37834675
where is the field VIN coming from?

better if you can upload a sample db..
0
 

Author Comment

by:keschuster
ID: 37834783
see atttached
sample.accdb
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 37834952
try this query

SELECT tblFeed_Rambo.ExpenseAccount, Max(tblFeed_Rambo.VIN) AS MaxOfVIN
FROM tblFeed_Rambo
GROUP BY tblFeed_Rambo.ExpenseAccount
Union ALL
SELECT tblFeed_Rambo.ExpenseAccount, Min(tblFeed_Rambo.VIN) AS MaxOfVIN
FROM tblFeed_Rambo
GROUP BY tblFeed_Rambo.ExpenseAccount
Order By 1
0
 

Author Comment

by:keschuster
ID: 37835000
Interesting approach....  you win.  Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

926 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