Reply to Re: T-SQL problem...selecting TOP 1 of each GROUP in GROUP BY?

Your name:

Reply:


Posted by Erland Sommarskog on 07/09/05 01:13

(throat.wobbler.mangrove@gmail.com) writes:
> One final thing...I have tried GROUPING BY all of the fields in s (in
> the wider expression), but if I have a record with identical
> RCRD_REFNOs and max MODIF_DTTM's, but one of the other fields differs,
> then I get two records for that unique ID...which brings me back once
> again to the TOP 1 thing...I'm now stuck!

There is a bit of lack examples that illustrates what you are up to,
but if I add your three posts together, my guess is that you have a
staging table that may contain junk.

Can't you can an IDENTITY column to the table? Without a unique key
for the table, it's going to be difficult to pick 1 out of 2 duplicates?

In such case you would do:

SELECT t.RCRD_REFNO, maxid = MAX(id)
FROM tbl t
JOIN (SELECT RCRC_RECNNO, MODIF_DTTM = MAX(MODIF_DTTM)
FROM tbl
GROUP BY RCRD_REFNO) AS u ON t.RCRD_REFNO = u.RCRD_REFNO
AND t.MODIF_DTTM = u.MODIF_DTTM
GROUP BY t.RCRD_RECNO


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация