Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2016 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/112bc2108fce15788fc9 to your computer and use it in GitHub Desktop.
Save anonymous/112bc2108fce15788fc9 to your computer and use it in GitHub Desktop.
SELECT
@id = CA.Id,
@Ed = CA.EffectiveDate,
@sid = CA.SequenceId,
@cid = CA.CustomerId,
@ano = CA.AccountNo
FROM
(
-- Per Id
SELECT DISTINCT Id
FROM dbo.TestTable
) AS TT
CROSS APPLY
(
-- Single row with the highest EffectiveDate then SequenceId
SELECT TOP (1) TT2.*
FROM dbo.TestTable AS TT2
WHERE TT2.Id = TT.Id
ORDER BY TT2.EffectiveDate DESC, TT2.SequenceId DESC
) AS CA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment