<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: data code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 30 Aug 2008 03:21:12 GMT</pubDate>
    <description>DZone Snippets: data code</description>
    <item>
      <title>Pull Date-Effective Records that Overlap Each Other</title>
      <link>http://snippets.dzone.com/posts/show/5590</link>
      <description>Basic framework and examples of how to pull records that overlap each other from a temporal (aka date-effective) database.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- PK is a placeholder for the Primary Key column(s)&lt;br /&gt;-- SoftPK is a placeholder for columns that need to match for overlap, but are not the PK&lt;br /&gt;-- Sometimes SoftPK is the PK, in which case you remove the &lt;&gt; PK line and match the SoftPK.&lt;br /&gt;-- The idea is to check for overlap on all rows except for yourself, since that always overlaps.&lt;br /&gt;--   This snippet assumes the EndDate column has a NULL to represent infinity.&lt;br /&gt;--   There are alternate syntaxes for this (compare or EndDate is null) but I've found&lt;br /&gt;--   the Coalesce to provide the best query plan usually. The constant itself is debatable and&lt;br /&gt;--   implementation dependent.&lt;br /&gt;select outer.*&lt;br /&gt;from DateEffectiveTable as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from DateEffectiveTable as inner&lt;br /&gt;  where inner.PK &lt;&gt; outer.PK&lt;br /&gt;    -- and inner.SoftPK = outer.SoftPK&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- Example Usage&lt;br /&gt;-- Assume we have a table containing the address history for a person&lt;br /&gt;-- A person may have multiple addresses at once (own multiple houses).&lt;br /&gt;-- A person may move away then back (same address, different time ranges).&lt;br /&gt;-- ie, multiple simultaneous unique records are possible, but not&lt;br /&gt;-- multiple simultaneous non-unique records (no self-overlap).&lt;br /&gt;-- Yes, it is usually an odd design to have an AddressID, but go with it.&lt;br /&gt;select outer.*&lt;br /&gt;from AddressHistory as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from AddressHistory as inner&lt;br /&gt;  where outer.AddressID &lt;&gt; inner.AddressID&lt;br /&gt;    and outer.PersonID = inner.PersonID&lt;br /&gt;    and outer.Street = inner.Street&lt;br /&gt;    and outer.Zip = inner.Zip&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- Same example, but assume PK is a more "appropriate" one of&lt;br /&gt;-- { PersonID, Street, Zip, StartDate }&lt;br /&gt;select outer.*&lt;br /&gt;from AddressHistory as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from AddressHistory as inner&lt;br /&gt;  where &lt;br /&gt;    not (&lt;br /&gt;     outer.PersonID = inner.PersonID&lt;br /&gt;     and outer.Street = inner.Street&lt;br /&gt;     and outer.Zip = inner.Zip&lt;br /&gt;     and outer.StartDate = inner.StartDate&lt;br /&gt;    )&lt;br /&gt;    and outer.PersonID = inner.PersonID&lt;br /&gt;    and outer.Street = inner.Street&lt;br /&gt;    and outer.Zip = inner.Zip&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 04 Jun 2008 19:54:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5590</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Pull Date-Effective Records that Intersect A Given Range</title>
      <link>http://snippets.dzone.com/posts/show/5503</link>
      <description>Basics of how to pull records that intersect a date range from a temporal (aka date-effective) database.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- @rangeStart is the start of the date range of interest&lt;br /&gt;-- @rangeEnd is the end of the date range of interest&lt;br /&gt;-- If you use null to signify infinity, then use the commented line instead of the one above it.&lt;br /&gt;select *&lt;br /&gt;from DateEffectiveTable&lt;br /&gt;where StartDate &lt; @rangeEnd&lt;br /&gt;  and EndDate &gt;= @rangeStart&lt;br /&gt;  -- and coalesce(endDate, '9999-12-31') &gt;= @rangeStart&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 16 May 2008 14:50:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5503</guid>
      <author>jokeyxero (xero)</author>
    </item>
  </channel>
</rss>
