Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #334
| From | Erland Sommarskog <esquel@sommarskog.se> |
|---|---|
| Newsgroups | comp.databases.ms-sqlserver |
| Subject | Re: Query for record that is effective as of a certain date |
| Date | 2011-05-16 22:29 +0200 |
| Organization | Erland Sommarskog |
| Message-ID | <Xns9EE7E4D02A941Yazorman@127.0.0.1> (permalink) |
| References | <2d2ba1e2-e68b-4292-8bac-e8af30fe3ebb@q20g2000vbx.googlegroups.com> |
BobRoyAce (broy@omegasoftwareinc.com) writes:
> How could I construct such a query? Is this something I could do with
> Select...Over...Partition By? I am not very familiar with that
> construct, but it seems that it might be relevant.
row_number is indeed your guy.
WITH numbered AS (
SELECT fkLoanId, fkLoanStatusID, EffectiveDate,
rowno = row_number() OVER (PARTITION BY fkLoanId
ORDER BY EffectiveDate DESC)
FROM LoanStatusChanges
WHERE EffectiveDate <= @perdate
)
SELECT L.LoanNum, LS.LoanStatusDesc, n.EffectiveDate
FROM numbered n
JOIN Loans L ON n.LoanID = L.LoanID
JOIN LoanStatuses LS ON n.LoanStatusID = LS.LoanStatusID
WHERE rowno = 1
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Back to comp.databases.ms-sqlserver | Previous | Next — Previous in thread | Next in thread | Find similar
Query for record that is effective as of a certain date BobRoyAce <broy@omegasoftwareinc.com> - 2011-05-16 09:49 -0700
Re: Query for record that is effective as of a certain date Erland Sommarskog <esquel@sommarskog.se> - 2011-05-16 22:29 +0200
Re: Query for record that is effective as of a certain date BobRoyAce <broy@omegasoftwareinc.com> - 2011-05-17 07:34 -0700
csiph-web