all groups > sql server mseq > august 2005 >
You're in the

sql server mseq

group:

Query returning multiple rows


Query returning multiple rows Jig Bhakta
8/11/2005 10:30:02 AM
sql server mseq:
Hi,

I have a query that returns back rows that have multiple entries for a given
UniqueID. I want the query to be expanded so that it then only returns 1 of
the multiple rows based on the maximum date. Thefore, my return set looks
like this:

UniqueID Name Date
---------------------------------
123 ABC 20041104
123 ABC 20041105
456 ABC 20031221
456 ABC 20031222
789 ABC 20050430
789 ABC 20050429

The query is this:

select *
from table A
where (select count(*) from table where UniqueID = A.UniqueID) > 1
Re: Query returning multiple rows Hugo Kornelis
8/11/2005 9:19:39 PM
[quoted text, click to view]

Hi Jig,

Try

SELECT UniqueID, Name, [Date]
FROM [table] AS A
WHERE EXISTS
(SELECT *
FROM [table] AS B
WHERE B.UniqueID = A.UniqueID
AND B.[Date] > A.[Date])
(untested)

Best, Hugo
--

Re: Query returning multiple rows Vishal Parkar
8/12/2005 8:43:29 AM
a small correction, to get the max date:

SELECT UniqueID, Name, [Date]
FROM [table] AS A
WHERE EXISTS
(SELECT *
FROM [table] AS B
WHERE B.UniqueID = A.UniqueID
AND B.[Date] < A.[Date]) --CHANGE HERE


[quoted text, click to view]
AddThis Social Bookmark Button