Solved

Access SQL 2007 - How Do I Return First Match For Each Item in a List

Posted on 2009-06-28
5
491 Views
Last Modified: 2012-05-07
I am trying to create a query that will return the first matching record for each GlobalId from in a list.  In the table I usually have more than one record per GlobalID but only need to pull back the first record encountered per GlobalID match.

I have tried using TOP but it returns all records related to each global id as my query below is currently written.


SELECT TOP 25 *
FROM [Reconciliation-Working Copy]
WHERE GlobalID IN (SELECT Distinct GlobalId FROM [Reconciliation-Working Copy] WHERE GlobalID <> 'NULL')

Open in new window

0
Comment
Question by:NimBot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24732330
Can you post your table structure and some sample data?
P.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24732733
And a definition of what you mean by "the first match"?  There is no guaranteed order for things when one is working with SQL unless one forces some sort of ordering.
0
 

Author Comment

by:NimBot
ID: 24732767
Sample of Table Structure is pasted below.  When the values are returned based upon GlobalId I only need one row per Global Id to be returned.  It does not matter if it is the first or second row per Global Id match is returned.




Site	Sys	LegId	MtmSource	MtmDate	MtmCurr	MtmExp	MtmHigh	MtmLow	UpdUserID	TradeId	GlobalId	Trade Type	ExternalRefID	Filepath	Comments
YZ	XXXX	123L.A	System	20080912	USD	5000			Fred	123L	1	Vanilla Swap		C:\	
YZ	XXXX	123L.B	System	20080912	USD	5000			Fred	123L	1	Vanilla Swap		C:\	
YZ	XXXX	456C.A	System	20080912	USD	-7000			Sam	456C	2	Vanilla Swap		C:\	
YZ	XXXX	456C.B	System	20080912	USD	-7000			Sam	456C	2	Vanilla Swap		C:\	
YZ	XXXX	678M.A	System	20080912	USD	12000			Harold	678M	3	Vanilla Swap		C:\	
YZ	XXXX	678M.B	System	20080912	USD	12000			Harold	678M	3	Vanilla Swap		C:\	

Open in new window

0
 
LVL 17

Accepted Solution

by:
pssandhu earned 250 total points
ID: 24732938
I cannot test this but please see if this works:


select r.*
From tablename r inner join ( select globalid, min(legid) Id from tablename group by globalid) t
On r.globalid = t.globalid and r.legid =t.Id

Open in new window

0
 

Author Closing Comment

by:NimBot
ID: 31597709
Thanks.  The solution worked.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 65
Union & Crosstab qrys 101! 6 68
How do I partition this table on date? 5 47
TSQL remove duplicates from different columns 14 50
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

738 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