Solved

MS Access - Query - return 2 records for each

Posted on 2012-04-11
9
272 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

828 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