<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>[T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx</link><description>It's relatively easy to store data with a time dimension, but querying it is another matter. If you select from a temporal tables (one which includes historical information indicated by a timestamp or datetime column) based on your ID, you'll get a lot</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6943653</link><pubDate>Sat, 07 Mar 2009 03:38:14 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6943653</guid><dc:creator>...</dc:creator><author>...</author><description>&lt;p&gt;Gute Arbeit hier! Gute Inhalte.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6943653" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6810119</link><pubDate>Wed, 31 Dec 2008 21:47:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6810119</guid><dc:creator>paulains</dc:creator><author>paulains</author><description>&lt;p&gt;Thanks for your example... I would have been totally lost trying to figure out how to do this sort of thing with traditional t-sql statements.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6810119" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6744179</link><pubDate>Mon, 17 Nov 2008 19:50:31 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6744179</guid><dc:creator>Andrew Rimmer</dc:creator><author>Andrew Rimmer</author><description>&lt;p&gt;You can find some more options for this here (including your solution).&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://stackoverflow.com/questions/296525/what-is-the-best-query-to-get-the-current-records-in-an-archive-table-sql-serve"&gt;stackoverflow.com/.../what-is-the-best-query-to-get-the-current-records-in-an-archive-table-sql-serve&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6744179" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6573897</link><pubDate>Thu, 28 Aug 2008 12:44:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6573897</guid><dc:creator>Sandy</dc:creator><author>Sandy</author><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Here is the code with rownum....&lt;/p&gt;
&lt;p&gt;SELECT [column names] FROM(SELECT ROWNUM RES_ROWNUM,[column names] FROM(SELECT * FROM TL_FLEXCAB th where exists ( select 1 from TL_FLEXCAB thx where th.FL_FNN_JOIN = thx.FL_FNN_JOIN group by thx.FL_FNN_JOIN having max( thx.FL_DT_TM ) = th.FL_DT_TM )))A WHERE A.RES_ROWNUM &amp;gt; 0 and A.RES_ROWNUM &amp;lt;=100&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6573897" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6573689</link><pubDate>Thu, 28 Aug 2008 10:40:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6573689</guid><dc:creator>Sandeep</dc:creator><author>Sandeep</author><description>&lt;p&gt;Hi i tried your code. It worked but i want to use rownum with this query as well. Can you give me the code with rownum.&lt;/p&gt;
&lt;p&gt;following is the code...&lt;/p&gt;
&lt;p&gt;SELECT * FROM tl_flexcab th where exists ( select 1 &amp;nbsp;from tl_flexcab thx &amp;nbsp;where th.FL_FNN_JOIN = thx.FL_FNN_JOIN &amp;nbsp;and FL_OWN_CD = 'ZAG' group by &amp;nbsp;thx.FL_FNN_JOIN having max( thx.FL_DT_TM ) = th.FL_DT_TM )&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6573689" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#6493739</link><pubDate>Thu, 07 Aug 2008 03:49:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6493739</guid><dc:creator>Lavita</dc:creator><author>Lavita</author><description>&lt;p&gt;Hi Jon, &lt;/p&gt;
&lt;p&gt;The information above is a bit helpful in terms of starting to understand the logic of structuring a joint query. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, the example above is not working for me. &amp;nbsp;I need to structure a nested query for one table which contains historical data to pull various records with the most recent update date without pulling duplicate records and I cannot seem to structure the query correctly. &amp;nbsp;If possible, would you please provide an example of this type of query? &lt;/p&gt;
&lt;p&gt;- Lavita &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6493739" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#5869563</link><pubDate>Wed, 27 Feb 2008 11:10:39 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5869563</guid><dc:creator>Glen</dc:creator><author>Glen</author><description>&lt;p&gt;I am getting the error that RANK() is not a valid function. &amp;nbsp;I am using MSSQL2000. Anyways, here is my situation.&lt;/p&gt;
&lt;p&gt;I have a table Stories (ID, Heading, SubHeading,..., EffectiveDate)&lt;/p&gt;
&lt;p&gt;When a story is updated, the Heading and SubHeading never change and a new record is inserted and the old one is history.&lt;/p&gt;
&lt;p&gt;ie:&lt;/p&gt;
&lt;p&gt;ID | Heading &amp;nbsp; | SubHeading | EffectiveDate&lt;/p&gt;
&lt;p&gt;55 | testing &amp;nbsp; | testing123 | 22 Feb 2008&lt;/p&gt;
&lt;p&gt;56 | testing &amp;nbsp; | testing123 | 26 Feb 2008&lt;/p&gt;
&lt;p&gt;58 | different | testing456 | 26 Feb 2008&lt;/p&gt;
&lt;p&gt;I need to list all the lastest stories &amp;nbsp;only. &amp;nbsp;So I should get the last 2 records shown above. &amp;nbsp;There could be multiple entries for each &amp;quot;heading &amp;amp; subheading&amp;quot; and when I list, I need all the items where the effectivedate is the latest...&lt;/p&gt;
&lt;p&gt;What is the solution?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5869563" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#3262425</link><pubDate>Tue, 24 Jul 2007 05:44:48 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3262425</guid><dc:creator>guyishappy</dc:creator><author>guyishappy</author><description>&lt;p&gt;thanks i like your code fun time at me pants&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3262425" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#3131685</link><pubDate>Fri, 13 Jul 2007 14:43:47 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3131685</guid><dc:creator>David L. Penton</dc:creator><author>David L. Penton</author><description>&lt;p&gt;@Jon: If you start by joining to a subquery and need to add fields later, it can get out of control quickly.&lt;/p&gt;
&lt;p&gt;Can you provide an example of this? &amp;nbsp;Based on your statement of &amp;quot;The general idea is to write a query which gets the latest update date&amp;quot; then the RANK/PARTITION feature is likely overkill and possibly more confusing.&lt;/p&gt;
&lt;p&gt;SELECT&lt;/p&gt;
&lt;p&gt; &amp;nbsp;p.[Name],&lt;/p&gt;
&lt;p&gt; &amp;nbsp;p.ProductNumber,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;th.TransactionDate,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;th.TransactionID&lt;/p&gt;
&lt;p&gt;FROM&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Production.TransactionHistory th&lt;/p&gt;
&lt;p&gt;INNER JOIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Production.Product p&lt;/p&gt;
&lt;p&gt; &amp;nbsp;ON p.ProductID = th.ProductID&lt;/p&gt;
&lt;p&gt;where&lt;/p&gt;
&lt;p&gt; &amp;nbsp;exists (&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;select 1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;from Production.TransactionHistory thx&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;where&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;-- this is our local 'primary key'&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;th.ProductID = thx.ProductID&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;group by&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;-- group by our local 'primary key'&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;thx.ProductID&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;having&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;-- what predicate are we after?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;max( thx.TransactionDate )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= th.TransactionDate&lt;/p&gt;
&lt;p&gt; &amp;nbsp;)&lt;/p&gt;
&lt;p&gt;To me, based on your requirements, and any requirement that is based on a MAX/MIN/SUM (basically simple aggregates) then this particular format of query it far superior and likely a better performer. &amp;nbsp;Where the RANK/PARTITION would be superior would be cases where you need the TOP X of something over a range of data, or some other subset of that data, or possibly if your data in your actual search predicate could be duplicated as well (as having duplicate datetime values by ProductID in the example) - but the latter could be handled easily in the outer query.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3131685" width="1" height="1"&gt;</description></item><item><title>re: [T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#3129185</link><pubDate>Fri, 13 Jul 2007 09:30:23 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3129185</guid><dc:creator>BAlexandrov</dc:creator><author>BAlexandrov</author><description>&lt;p&gt;It seems that you wanted to use row_num instead of rank because if you have duplicate records by TransactionDate you would get duplicates if you dont have duplicates, rank is not useless...&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3129185" width="1" height="1"&gt;</description></item></channel></rss>