<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.infosupport.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Mark Streutker</title><subtitle type="html" /><id>http://blogs.infosupport.com/blogs/marks/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.infosupport.com/blogs/marks/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.infosupport.com/blogs/marks/atom.aspx" /><generator uri="http://communityserver.org" version="4.1.31106.3070">Community Server</generator><updated>2009-06-01T18:38:00Z</updated><entry><title>Handling EAV-data: Timelines</title><link rel="alternate" type="text/html" href="/blogs/marks/archive/2010/02/22/handling-eav-data-timelines.aspx" /><id>/blogs/marks/archive/2010/02/22/handling-eav-data-timelines.aspx</id><published>2010-02-22T18:29:00Z</published><updated>2010-02-22T18:29:00Z</updated><content type="html">&lt;p&gt;As promised in my previous post about &lt;a href="http://blogs.infosupport.com/blogs/marks/archive/2009/06/01/handling-eav-data.aspx"&gt;working with EAV-data&lt;/a&gt; this post would about handling multiple timelines (or periods for that matter).     &lt;br /&gt;While this blog is specifically written for an EAV-environment, the technique explained in this post can be useful for other situations as well. &lt;/p&gt;  &lt;p&gt;The problem I’m discussing in this article is about working with multiple, overlapping periods of time and the way they affect the outcome of PIVOT statements.    &lt;br /&gt;Because there is so little to find about this subject, I decided to make a write-up of the solution me and my colleagues came up with.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;span style="font-size:medium;"&gt;The Problem&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Before diving into the technique I would like to position the problem, so first of all I’d like start off with a simple example of what’s going wrong.    &lt;br /&gt;Take a look at the following tables:&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: Entity&lt;/span&gt;     &lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="200"&gt;&lt;b&gt;EntityID&lt;/b&gt; &lt;/td&gt;          &lt;td valign="top" width="200"&gt;&lt;b&gt;Entity&lt;/b&gt; &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;1 &lt;/td&gt;          &lt;td valign="top" width="200"&gt;Employee &lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: Attribute&lt;/span&gt;     &lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="200"&gt;&lt;b&gt;AttributeID&lt;/b&gt; &lt;/td&gt;          &lt;td valign="top" width="200"&gt;&lt;b&gt;Attribute&lt;/b&gt; &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;100 &lt;/td&gt;          &lt;td valign="top" width="200"&gt;Name &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;101 &lt;/td&gt;          &lt;td valign="top" width="200"&gt;Birthday &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;102 &lt;/td&gt;          &lt;td valign="top" width="200"&gt;Nationality &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;103 &lt;/td&gt;          &lt;td valign="top" width="200"&gt;Marital Status &lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: EAV_Data&lt;/span&gt;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="100"&gt;&lt;b&gt;ID&lt;/b&gt; &lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;b&gt;EntityID&lt;/b&gt; &lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;b&gt;AttributeID&lt;/b&gt; &lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;b&gt;Value&lt;/b&gt; &lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;b&gt;ValidFrom&lt;/b&gt; &lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;b&gt;ValidTo&lt;/b&gt; &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;100 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;John &lt;/td&gt;        &lt;td valign="top" width="100"&gt;01-01-2000 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;31-12-2999 &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;101 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;12-12-1970 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;01-01-2000 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;31-12-2999 &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;102 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;English &lt;/td&gt;        &lt;td valign="top" width="100"&gt;01-01-2000 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;02-02-2009 &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;102 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;Dutch &lt;/td&gt;        &lt;td valign="top" width="100"&gt;02-02-2009 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;31-12-2999 &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;103 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;Single &lt;/td&gt;        &lt;td valign="top" width="100"&gt;01-01-2000 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;03-03-2010 &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;1 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;103 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;Married &lt;/td&gt;        &lt;td valign="top" width="100"&gt;03-03-2010 &lt;/td&gt;        &lt;td valign="top" width="100"&gt;31-12-2999 &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;They have a striking similarity with the tables I’ve used in my previous post, but this time the EAV_Data table is a bit more complex due to the added ValidFrom and ValidTo columns.    &lt;br /&gt;Because of these two columns, it is now possible to have multiple values&amp;#160; for different periods of time.     &lt;br /&gt;    &lt;br /&gt;Meet John.     &lt;br /&gt;John is an Englishman who chose the green wetlands of Holland to make his new home. He liked it so much that he even acquired the Dutch nationality at some point.     &lt;br /&gt;His actions did not go unnoticed and John conquered the heart of the girl of his dreams, who he married a year later.     &lt;br /&gt;    &lt;br /&gt;See underneath for a graphic representation of the situation:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_66849A00.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="285" alt="image" src="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_thumb_5F00_46698D43.png" width="640" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Now let’s say we ignore the time-axe and just start pivoting, just like we did last time:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;     &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; (  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt;     , ValidFrom&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum5" style="color:#606060;"&gt;   5:&lt;/span&gt;     , ValidTo &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum6" style="color:#606060;"&gt;   6:&lt;/span&gt;     , [100] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Name   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum7" style="color:#606060;"&gt;   7:&lt;/span&gt;     , [101] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Birthday   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum8" style="color:#606060;"&gt;   8:&lt;/span&gt;     , [102] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Nationality   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum9" style="color:#606060;"&gt;   9:&lt;/span&gt;     , [103] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Marital_Status   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum10" style="color:#606060;"&gt;  10:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum11" style="color:#606060;"&gt;  11:&lt;/span&gt;     (       &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID, EntityID, AttributeID, &lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;, ValidFrom, ValidTo &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum12" style="color:#606060;"&gt;  12:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum13" style="color:#606060;"&gt;  13:&lt;/span&gt;     ) p  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum14" style="color:#606060;"&gt;  14:&lt;/span&gt;     PIVOT     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum15" style="color:#606060;"&gt;  15:&lt;/span&gt;     (     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum16" style="color:#606060;"&gt;  16:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;MAX&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;)  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum17" style="color:#606060;"&gt;  17:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;FOR&lt;/span&gt; AttributeID &lt;span style="color:#0000ff;"&gt;IN&lt;/span&gt; ([100], [101], [102], [103])     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum18" style="color:#606060;"&gt;  18:&lt;/span&gt;     ) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; pvt  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum19" style="color:#606060;"&gt;  19:&lt;/span&gt; ) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;result&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

  &lt;br /&gt;This query yields the following result: 

  &lt;br /&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;&lt;b&gt;ID&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;&lt;b&gt;ValidFrom&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;&lt;b&gt;ValidTo&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;&lt;b&gt;Name&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;&lt;b&gt;Birthday&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;&lt;b&gt;Nationality&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;&lt;b&gt;Marital_Status&lt;/b&gt; &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2000-01-01 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2009-02-02 &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;English &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;NULL &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2000-01-01 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2010-03-03 &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;Single &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2000-01-01 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2999-12-31 &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;John &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;12-12-1970 &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;NULL &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2009-02-02 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2999-12-31 &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;Dutch &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;NULL &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="60"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2010-03-03 &lt;/td&gt;

      &lt;td valign="bottom" width="143"&gt;2999-12-31 &lt;/td&gt;

      &lt;td valign="bottom" width="60"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="85"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="84"&gt;NULL &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;Married &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;
  &lt;br /&gt;This result introduces a few new problems. 

  &lt;br /&gt;First of all, the periods overlap each other.&amp;#160; This is a bad thing, because now I don’t have one version the truth. 

  &lt;br /&gt;Second of all, most columns are blank (NULL). This means that in order to get a correct selection we have to combine the right periods AND merge the right values to get a complete, correct visualization of the record on a specific moment. 

  &lt;br /&gt;

  &lt;br /&gt;But what is causing this behavior in the first place? Because the PIVOT operator now ‘rotates’ the data around the ID, ValidFrom and ValidTo columns, data is only presented in a row if it matches the exact same period. This is incorrect behavior, because the name (John) is valid during the entire period and therefore should be present in every row. &lt;/p&gt;

&lt;p&gt;What we need, is what I’d like to call an Effective Timeline. 
  &lt;br /&gt;

  &lt;br /&gt;&lt;span style="font-size:medium;"&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;An effective timeline is a timeline that consist out of multiple consecutive periods, rather than multiple parallel/overlapping periods. 
  &lt;br /&gt;This way, a specific moment belongs to one period and one period only. 

  &lt;br /&gt;If we merge all the periods present in our example and remove the duplicates we get something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_264E8086.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="167" alt="image" src="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_thumb_5F00_6D37A383.png" width="643" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;See how this graph only has one line for every period? 
  &lt;br /&gt;But how do you achieve this programmatically? 

  &lt;br /&gt;In my project we came up with the following solution; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We simply gather all unique start-and enddates of every period in one big collection. &lt;/li&gt;

  &lt;li&gt;Next, we sort this collection from oldest to newest, ignoring the fact if the current record is a start- or enddate. &lt;/li&gt;

  &lt;li&gt;Now we have a temporary collection with all possible dates, conviently ordered by date. &lt;/li&gt;

  &lt;li&gt;Finally we join the collection with itself (one record shifted), combine all dates and effectively create a whole new set of consecutive periods. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the sample UDF below for clarity:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;
    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FUNCTION&lt;/span&gt; [dbo].[fn_EffectiveTimelines]&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;RETURNS&lt;/span&gt; @l_ResultTable &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum5" style="color:#606060;"&gt;   5:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum6" style="color:#606060;"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum7" style="color:#606060;"&gt;   7:&lt;/span&gt;          ID &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum8" style="color:#606060;"&gt;   8:&lt;/span&gt;         , ValidFrom DATETIME&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum9" style="color:#606060;"&gt;   9:&lt;/span&gt;         , ValidTo DATETIME&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum10" style="color:#606060;"&gt;  10:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum11" style="color:#606060;"&gt;  11:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum12" style="color:#606060;"&gt;  12:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BEGIN&lt;/span&gt;    &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum13" style="color:#606060;"&gt;  13:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum14" style="color:#606060;"&gt;  14:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EffectiveTimelines &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum15" style="color:#606060;"&gt;  15:&lt;/span&gt;     (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum16" style="color:#606060;"&gt;  16:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID, &lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;, Type, ROW_NUMBER() &lt;span style="color:#0000ff;"&gt;OVER&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;  ID, &lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Number &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum17" style="color:#606060;"&gt;  17:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum18" style="color:#606060;"&gt;  18:&lt;/span&gt;         (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum19" style="color:#606060;"&gt;  19:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;DISTINCT&lt;/span&gt; ID, &lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;, Type&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum20" style="color:#606060;"&gt;  20:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum21" style="color:#606060;"&gt;  21:&lt;/span&gt;             (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum22" style="color:#606060;"&gt;  22:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum23" style="color:#606060;"&gt;  23:&lt;/span&gt;                     , ValidFrom &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum24" style="color:#606060;"&gt;  24:&lt;/span&gt;                     , 1 &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Type&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum25" style="color:#606060;"&gt;  25:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum26" style="color:#606060;"&gt;  26:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;UNION&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum27" style="color:#606060;"&gt;  27:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum28" style="color:#606060;"&gt;  28:&lt;/span&gt;                     , ValidTo &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum29" style="color:#606060;"&gt;  29:&lt;/span&gt;                     , 0 &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Type&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum30" style="color:#606060;"&gt;  30:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum31" style="color:#606060;"&gt;  31:&lt;/span&gt;             ) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; TimeSet&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum32" style="color:#606060;"&gt;  32:&lt;/span&gt;         ) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; TimeSetOrdered&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum33" style="color:#606060;"&gt;  33:&lt;/span&gt;     )&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum34" style="color:#606060;"&gt;  34:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum35" style="color:#606060;"&gt;  35:&lt;/span&gt;     INSERT &lt;span style="color:#0000ff;"&gt;INTO&lt;/span&gt; @l_ResultTable&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum36" style="color:#606060;"&gt;  36:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum37" style="color:#606060;"&gt;  37:&lt;/span&gt;     t1.ID,&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum38" style="color:#606060;"&gt;  38:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;CASE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum39" style="color:#606060;"&gt;  39:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; t1.Type = 0 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; DATEADD(dd, 1, t1.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum40" style="color:#606060;"&gt;  40:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;ELSE&lt;/span&gt; t1.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum41" style="color:#606060;"&gt;  41:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;END&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum42" style="color:#606060;"&gt;  42:&lt;/span&gt;     ,&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum43" style="color:#606060;"&gt;  43:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;CASE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum44" style="color:#606060;"&gt;  44:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; t2.Type = 1 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; DATEADD(dd, -1, t2.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum45" style="color:#606060;"&gt;  45:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;ELSE&lt;/span&gt; t2.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum46" style="color:#606060;"&gt;  46:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;END&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum47" style="color:#606060;"&gt;  47:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EffectiveTimelines t1&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum48" style="color:#606060;"&gt;  48:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;INNER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; EffectiveTimelines t2&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum49" style="color:#606060;"&gt;  49:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; t1.Number + 1 = t2.Number&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum50" style="color:#606060;"&gt;  50:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; t1.ID = t2.ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum51" style="color:#606060;"&gt;  51:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; DATEADD(dd, 1, t1.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;) &amp;lt; t2.&lt;span style="color:#0000ff;"&gt;Date&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum52" style="color:#606060;"&gt;  52:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum53" style="color:#606060;"&gt;  53:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;RETURN&lt;/span&gt;;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum54" style="color:#606060;"&gt;  54:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;END&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Running this function will give the following result:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="bottom" width="64"&gt;&lt;b&gt;ID&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="152"&gt;&lt;b&gt;ValidFrom&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="160"&gt;&lt;b&gt;ValidTo&lt;/b&gt; &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="64"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="152"&gt;2000-01-01 &lt;/td&gt;

      &lt;td valign="bottom" width="160"&gt;2009-02-02 &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="64"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="152"&gt;2009-02-02 &lt;/td&gt;

      &lt;td valign="bottom" width="160"&gt;2010-03-03 &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="64"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="152"&gt;2010-03-03 &lt;/td&gt;

      &lt;td valign="bottom" width="160"&gt;2999-12-31 &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;
  &lt;br /&gt;Now that we have an unigue, effective timeline, we can now join the rest of the data to get a complete overview. See the following example:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;
    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; (  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt;     , ValidFrom&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum5" style="color:#606060;"&gt;   5:&lt;/span&gt;     , ValidTo &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum6" style="color:#606060;"&gt;   6:&lt;/span&gt;     , [100] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Name   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum7" style="color:#606060;"&gt;   7:&lt;/span&gt;     , [101] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Birthday   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum8" style="color:#606060;"&gt;   8:&lt;/span&gt;     , [102] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Nationality   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum9" style="color:#606060;"&gt;   9:&lt;/span&gt;     , [103] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Marital_Status   &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum10" style="color:#606060;"&gt;  10:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum11" style="color:#606060;"&gt;  11:&lt;/span&gt;     (       &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; eav.ID, eav.EntityID, eav.AttributeID, eav.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;, t.ValidFrom, t.ValidTo &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum12" style="color:#606060;"&gt;  12:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data eav&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum13" style="color:#606060;"&gt;  13:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;INNER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; fn_EffectiveTimelines() t&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum14" style="color:#606060;"&gt;  14:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; eav.ID = t.ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum15" style="color:#606060;"&gt;  15:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; eav.ValidFrom &amp;lt; t.ValidTo&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum16" style="color:#606060;"&gt;  16:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; eav.ValidTo &amp;gt; t.ValidFrom  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum17" style="color:#606060;"&gt;  17:&lt;/span&gt;     ) p  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum18" style="color:#606060;"&gt;  18:&lt;/span&gt;     PIVOT     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum19" style="color:#606060;"&gt;  19:&lt;/span&gt;     (     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum20" style="color:#606060;"&gt;  20:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;MAX&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;)  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum21" style="color:#606060;"&gt;  21:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;FOR&lt;/span&gt; AttributeID &lt;span style="color:#0000ff;"&gt;IN&lt;/span&gt; ([100], [101], [102], [103])     &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum22" style="color:#606060;"&gt;  22:&lt;/span&gt;     ) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; pvt  &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum23" style="color:#606060;"&gt;  23:&lt;/span&gt; ) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;result&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;In this step the data is assigned to every period is has overlap with. Because some values are longer valid than others, it can happen that one value is assigned to multiple periods, as shown in the graph below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_1F2F440E.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="351" alt="image" src="http://blogs.infosupport.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/marks/image_5F00_thumb_5F00_2D018A09.png" width="643" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;

  &lt;br /&gt;Because the periods are now neatly aligned, the PIVOT statement will come up with the following result:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="0" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="bottom" width="57"&gt;&lt;b&gt;ID&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;&lt;b&gt;ValidFrom&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;&lt;b&gt;ValidTo&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="57"&gt;&lt;b&gt;Name&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="87"&gt;&lt;b&gt;Birthday&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;&lt;b&gt;Nationality&lt;/b&gt; &lt;/td&gt;

      &lt;td valign="bottom" width="119"&gt;&lt;b&gt;Marital_Status&lt;/b&gt; &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="57"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2000-01-01 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2009-02-02 &lt;/td&gt;

      &lt;td valign="bottom" width="57"&gt;John &lt;/td&gt;

      &lt;td valign="bottom" width="87"&gt;12-12-1970 &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;English &lt;/td&gt;

      &lt;td valign="bottom" width="109"&gt;Single &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="57"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2009-02-02 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2010-03-03 &lt;/td&gt;

      &lt;td valign="bottom" width="57"&gt;John &lt;/td&gt;

      &lt;td valign="bottom" width="87"&gt;12-12-1970 &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;Dutch &lt;/td&gt;

      &lt;td valign="bottom" width="109"&gt;Single &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="bottom" width="57"&gt;1234 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2010-03-03 &lt;/td&gt;

      &lt;td valign="bottom" width="136"&gt;2999-12-31 &lt;/td&gt;

      &lt;td valign="bottom" width="57"&gt;John &lt;/td&gt;

      &lt;td valign="bottom" width="87"&gt;12-12-1970 &lt;/td&gt;

      &lt;td valign="bottom" width="77"&gt;Dutch &lt;/td&gt;

      &lt;td valign="bottom" width="109"&gt;Married &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;This results is a more accurate representation of the data. The NULL fields are gone and the data is now correctly divided amongst the periods. 
  &lt;br /&gt;With this technique you can pivot data with multiple timelines with little effort, it doesn’t matter if you have 3 periods or 3000: this technique will always calculate the effective timeline (albeit a bit slower ;)).&lt;/p&gt;

&lt;p&gt;&lt;span style="font-size:medium;"&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;By making use of effective timelines working with EAV is a bit less complex. 
  &lt;br /&gt;And like I said in the beginning of this blog, calculating an effective timeline by merging periods can be useful in other situations as well. For example, if you have multiple periods that have overlap and you need to count the unique days over all periods, having a effective timeline can really speed up the process. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=182635" width="1" height="1"&gt;</content><author><name>marks</name><uri>http://blogs.infosupport.com/members/marks/default.aspx</uri></author><category term="SQL" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/SQL/default.aspx" /><category term="EAV" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/EAV/default.aspx" /><category term="PIVOT" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/PIVOT/default.aspx" /></entry><entry><title>Is ISNUMERIC numeric?</title><link rel="alternate" type="text/html" href="/blogs/marks/archive/2009/10/30/is-isnumeric-numeric.aspx" /><id>/blogs/marks/archive/2009/10/30/is-isnumeric-numeric.aspx</id><published>2009-10-30T19:14:00Z</published><updated>2009-10-30T19:14:00Z</updated><content type="html">&lt;p&gt;In SQL, if you&amp;rsquo;re not sure if a value can be cast to a certain datatype there are some built-in functions that can help you out.    &lt;br /&gt;Take the following construction, where col_Value is a CHAR(4):&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;" id="codeSnippet"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CASE&lt;/span&gt; ISNUMERIC (col_Value)&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum2"&gt;   2:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 1 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (col_Value &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;)&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum3"&gt;   3:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;ELSE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum4"&gt;   4:&lt;/span&gt;             END&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;At first glance, this solution seems to handle col_Value just fine. If col_Value contains a numeric value it will be cast to an INT and if it&amp;rsquo;s not, a NULL value will be returned. 
  &lt;br /&gt;The developers expectations seem to be met: in this scenario no typecasting errors can occur. Or can they?&lt;/p&gt;
&lt;p&gt;Truth is, the ISNUMERIC function is often misinterpreted (at least, that&amp;rsquo;s my personal experience). 
  &lt;br /&gt;According to &lt;a href="http://msdn.microsoft.com/en-us/library/ms186272.aspx"&gt;MSDN&lt;/a&gt; the definition of ISNUMERIC is: &lt;em&gt;Determines whether an expression is a valid numeric type&lt;/em&gt;. 
  &lt;br /&gt;ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type.&lt;/p&gt;
&lt;p&gt;The catch is, a returnvalue of 1 means that the expression can be cast to at least &lt;strong&gt;one&lt;/strong&gt; numeric datatype, rather than &lt;strong&gt;all &lt;/strong&gt;available numeric datatypes supported by SQL Server. 
  &lt;br /&gt;The following queries illustrate this behavior:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;" id="codeSnippet"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ISNUMERIC (&lt;span style="color:#006080;"&gt;&amp;#39;100&amp;#39;&lt;/span&gt;)        &lt;span style="color:#008000;"&gt;-- Returns 1&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FLOAT&lt;/span&gt;)    &lt;span style="color:#008000;"&gt;-- Returns 100&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;)  &lt;span style="color:#008000;"&gt;-- Returns 100&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; MONEY)    &lt;span style="color:#008000;"&gt;-- Returns 100,00&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;)      &lt;span style="color:#008000;"&gt;-- Returns 100&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum7"&gt;   7:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum8"&gt;   8:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum9"&gt;   9:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ISNUMERIC (&lt;span style="color:#006080;"&gt;&amp;#39;1e2&amp;#39;&lt;/span&gt;)        &lt;span style="color:#008000;"&gt;-- Returns 1&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum10"&gt;  10:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum11"&gt;  11:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;1e2&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FLOAT&lt;/span&gt;)    &lt;span style="color:#008000;"&gt;-- Returns 100&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum12"&gt;  12:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;1e2&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;)  &lt;span style="color:#008000;"&gt;-- Error converting data type varchar to numeric.&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum13"&gt;  13:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;1e2&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; MONEY)    &lt;span style="color:#008000;"&gt;-- Cannot convert a char value to money. The char value has incorrect syntax.&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum14"&gt;  14:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;1e2&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;)      &lt;span style="color:#008000;"&gt;-- Conversion failed when converting the varchar value &amp;#39;1e2&amp;#39; to data type int&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum15"&gt;  15:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum16"&gt;  16:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ISNUMERIC (&lt;span style="color:#006080;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt;)       &lt;span style="color:#008000;"&gt;-- Returns 1&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum17"&gt;  17:&lt;/span&gt;  &lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum18"&gt;  18:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FLOAT&lt;/span&gt;)   &lt;span style="color:#008000;"&gt;-- Error converting data type varchar to float.&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum19"&gt;  19:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;) &lt;span style="color:#008000;"&gt;-- Error converting data type varchar to numeric.&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum20"&gt;  20:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; MONEY)   &lt;span style="color:#008000;"&gt;-- Returns 100,00&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;text-align:left;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;" id="lnum21"&gt;  21:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;)     -- &lt;span style="color:#008000;"&gt;Conversion failed &lt;span style="color:#008000;"&gt;&lt;span style="color:#008000;"&gt;&lt;span style="color:#008000;"&gt;&lt;span style="color:#008000;"&gt;when&lt;/span&gt; converting the &lt;/span&gt;&lt;span style="color:#008000;"&gt;varchar&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#008000;"&gt;value&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#008000;"&gt;&amp;#39;&amp;euro;100&amp;#39;&lt;/span&gt; &lt;span style="color:#008000;"&gt;to&lt;/span&gt; &lt;span style="color:#008000;"&gt;data&lt;/span&gt; type &lt;span style="color:#008000;"&gt;int&lt;/span&gt;.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So in conclusion; if an expression has to be cast into a specific numeric datatype, checking ISNUMERIC may not be sufficient. This is of course heavily dependant on the possible values of the expression, but if this is unpredictable extra checks may be necessary&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=34933" width="1" height="1"&gt;</content><author><name>marks</name><uri>http://blogs.infosupport.com/members/marks/default.aspx</uri></author><category term="SQL" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/SQL/default.aspx" /><category term="Functions" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/Functions/default.aspx" /></entry><entry><title>Handling EAV-data</title><link rel="alternate" type="text/html" href="/blogs/marks/archive/2009/06/01/handling-eav-data.aspx" /><id>/blogs/marks/archive/2009/06/01/handling-eav-data.aspx</id><published>2009-06-01T16:38:00Z</published><updated>2009-06-01T16:38:00Z</updated><content type="html">&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In my line of work I often have to develop reports on all kinds of environments, but most of the time the underlying databases are limited to relational or dimensional models.    &lt;br /&gt;A few months ago, however, I was faced with the &lt;span style="text-decoration:line-through;"&gt;tedious task &lt;/span&gt;challenge to build a couple of reports on a system that was entirely based on an EAV-architecture.     &lt;br /&gt;Even though I was familiar with the technique, I have never put it to use for reporting purposes.     &lt;br /&gt;After doing some research on the internet, looking for best practices and guidelines, I noticed that the amount of information about reporting on EAV-data was limited at best.     &lt;br /&gt;That’s why I decided to dedicate a few articles on my personal experiences (and pitfalls) with the wonderful world of EAV.&lt;/p&gt;  &lt;p&gt;There are a lot of interesting challenges to overcome when working with EAV-data, but I will try to start out with a few simple examples.    &lt;br /&gt;In this first article I will focus on the selection and formatting of EAV-data, the more complex techniques (like handling timelines) will be addressed in future articles. &lt;/p&gt;  &lt;p&gt;First of all, let’s dive into some of the basics concerning EAV-data (since this type of datamodelling isn’t very common in a reporting environment).    &lt;br /&gt;EAV is an abbreviation for Entity – Attribute – Value. Data stored in this type of model usually consists out of three ‘base’ columns: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Entity &lt;/strong&gt;: An Entity is a ‘thing’, and object. Examples could be ‘Person’, ‘Employee’, ‘Contract’. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Attribute &lt;/strong&gt;: An Attribute describes a part of the Entity. If we take the Entity ‘Person’ as an example, attributes could be ‘Name’, ‘Birthday’, ‘Nationality’, etc. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Value &lt;/strong&gt;: The Value of the Attribute, i.e. ‘John’, ’12-12-1970’, ‘English’. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In a relational datamodel, an Entity would most likely be a table, a Attribute would correspond with a column and a Value would match a field.    &lt;br /&gt;Following table is a representation of a very simple EAV dataset.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: Entity&lt;/span&gt;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="199"&gt;&lt;strong&gt;EntityID&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="199"&gt;&lt;strong&gt;Entity&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="199"&gt;1&lt;/td&gt;        &lt;td valign="top" width="199"&gt;Employee&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="199"&gt;33&lt;/td&gt;        &lt;td valign="top" width="199"&gt;Contract&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: Attribute&lt;/span&gt;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="200"&gt;&lt;strong&gt;AttributeID&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="200"&gt;&lt;strong&gt;Attribute&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="200"&gt;100&lt;/td&gt;        &lt;td valign="top" width="200"&gt;Name&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="200"&gt;101&lt;/td&gt;        &lt;td valign="top" width="200"&gt;Birthday&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="200"&gt;102&lt;/td&gt;        &lt;td valign="top" width="200"&gt;Nationality&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="200"&gt;360&lt;/td&gt;        &lt;td valign="top" width="200"&gt;Type&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="200"&gt;361&lt;/td&gt;        &lt;td valign="top" width="200"&gt;Startdate&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;div&gt;&lt;/div&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Table: EAV_Data&lt;/span&gt;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="100"&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;strong&gt;EntityID&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;strong&gt;AttributeID&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="100"&gt;&lt;strong&gt;Value&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;        &lt;td valign="top" width="100"&gt;1&lt;/td&gt;        &lt;td valign="top" width="100"&gt;100&lt;/td&gt;        &lt;td valign="top" width="100"&gt;John&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;        &lt;td valign="top" width="100"&gt;1&lt;/td&gt;        &lt;td valign="top" width="100"&gt;101&lt;/td&gt;        &lt;td valign="top" width="100"&gt;12-12-1970&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;        &lt;td valign="top" width="100"&gt;1&lt;/td&gt;        &lt;td valign="top" width="100"&gt;102&lt;/td&gt;        &lt;td valign="top" width="100"&gt;English&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;5678&lt;/td&gt;        &lt;td valign="top" width="100"&gt;33&lt;/td&gt;        &lt;td valign="top" width="100"&gt;360&lt;/td&gt;        &lt;td valign="top" width="100"&gt;Fulltime&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;5678&lt;/td&gt;        &lt;td valign="top" width="100"&gt;33&lt;/td&gt;        &lt;td valign="top" width="100"&gt;361&lt;/td&gt;        &lt;td valign="top" width="100"&gt;01-01-2009&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;In the above example, we notice a few things.    &lt;br /&gt;First of all, in a relational datamodel we probably would have had a ‘Employee’ table and a ‘Contract’ table.     &lt;br /&gt;In our EAV example, most the data is stored in 1 single table. Both the tables (Entities) and columns (Attributes) are stored in a row-wise manner, therefore there is no ‘fixed’ datastructure. Second, all the ‘Values’ are stored in the same column, which implicates that we have 1 datatype to store everything (varchars, datetimes, ints, etc.).     &lt;br /&gt;To keep this example simple, ill ignore this ‘datatype-problem’ for now.&lt;/p&gt;  &lt;p&gt;EAV-modelling works exceptionally well for systems that need a great amount of flexibility.    &lt;br /&gt;Both ‘tables’ and ‘columns’ can be dynamically added without changing the underlying model.     &lt;br /&gt;Systems that need this amount of flexibility are i.e. clinical applications.     &lt;br /&gt;So instead of having several (fixed) smaller tables, you end up with one (flexible) huge table.&lt;/p&gt;  &lt;p&gt;While this is all fun and games for systems that storing information, things can get a bit challenging when you need to build reports.    &lt;br /&gt;First challenge: How do you retrieve EAV data?     &lt;br /&gt;To demonstrate the differences between a relational model and a EAV model when building queries, I’ll try to make an easy selection on all employee’s with the name ‘John’ that have the ‘English’ nationality.&lt;/p&gt;  &lt;p&gt;The relation employee table in this example has the following columns: ID, Name, Last Name, Birthday, Nationality and Marriagal status.&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Example 1: Relational table&lt;/span&gt;&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="border-right:silver 1px solid;padding-right:4px;border-top:silver 1px solid;padding-left:4px;font-size:8pt;padding-bottom:4px;margin:20px 0px 10px;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;padding-top:4px;border-bottom:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;"&gt;   &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;     &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; Employee &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; Name = &amp;#39;John&amp;#39; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; Nationality = &amp;#39;English&amp;#39;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Result&lt;/span&gt;&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2" width="700" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="118"&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="113"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="113"&gt;&lt;strong&gt;Last name&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="117"&gt;&lt;strong&gt;Birthday&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="120"&gt;&lt;strong&gt;Nationality&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="117"&gt;&lt;strong&gt;Marriagal Status&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="118"&gt;1234&lt;/td&gt;

      &lt;td valign="top" width="113"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="112"&gt;Doe&lt;/td&gt;

      &lt;td valign="top" width="117"&gt;12-12-1970&lt;/td&gt;

      &lt;td valign="top" width="121"&gt;English&lt;/td&gt;

      &lt;td valign="top" width="117"&gt;Married&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="118"&gt;365&lt;/td&gt;

      &lt;td valign="top" width="113"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="113"&gt;Cook&lt;/td&gt;

      &lt;td valign="top" width="117"&gt;01-08-1983&lt;/td&gt;

      &lt;td valign="top" width="122"&gt;English&lt;/td&gt;

      &lt;td valign="top" width="118"&gt;Single&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;All the information from an Employee is stored in 1 record, which makes it a very easy task to select all the information we need. 
  &lt;br /&gt;In contrary to the Employee table, the EAV table has every attribute (column) stored in a new record.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Example 2: EAV table&lt;/span&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right:silver 1px solid;padding-right:4px;border-top:silver 1px solid;padding-left:4px;font-size:8pt;padding-bottom:4px;margin:20px 0px 10px;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;padding-top:4px;border-bottom:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;
    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; Attribute = &amp;#39;Name&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; = &amp;#39;John&amp;#39;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Result&lt;/span&gt;&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;EntityID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;AttributeID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Value&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;1&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;100&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;365&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;1&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;100&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Hmm, not exactly what we wanted to see. Even though this is a correct result, the other Attributes (like Last Name, Birthday, etc) are not returned by our query. 
  &lt;br /&gt;If we were to make a report on the Employees, we have to figure a way out to present the Attributes on the horizontal axe, as if they were columns. In short, we want the rows to be converted to columns. 

  &lt;br /&gt;One way to achieve this, is to access the same table several times by means of selfjoins.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Example 3: EAV table with selfjoins&lt;/span&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right:silver 1px solid;padding-right:4px;border-top:silver 1px solid;padding-left:4px;font-size:8pt;padding-bottom:4px;margin:20px 0px 10px;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;padding-top:4px;border-bottom:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;
    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  t1.ID &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#006080;"&gt;&amp;#39;ID&amp;#39;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt;       , t1.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &amp;#39;Name&amp;#39; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt;       , t2.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &amp;#39;Nationality&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt;       , t3.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Birthday&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum5" style="color:#606060;"&gt;   5:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data t1&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum6" style="color:#606060;"&gt;   6:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;LEFT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; EAV_Data t2&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum7" style="color:#606060;"&gt;   7:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; t1.ID = t2.ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum8" style="color:#606060;"&gt;   8:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;LEFT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; EAV_Data t3&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum9" style="color:#606060;"&gt;   9:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; t1.ID = t3.ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum10" style="color:#606060;"&gt;  10:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; t1.Attribute = &amp;#39;Name&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum11" style="color:#606060;"&gt;  11:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; t1.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; = &amp;#39;John&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum12" style="color:#606060;"&gt;  12:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; t2.Attribute = &amp;#39;Nationality&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum13" style="color:#606060;"&gt;  13:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; t2.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt; = &amp;#39;English&amp;#39;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum14" style="color:#606060;"&gt;  14:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; t3.Attribute = &amp;#39;Birthday&amp;#39;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Result&lt;/span&gt;&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Nationality&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Birthday&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;English&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;12-12-1970&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;365&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;English&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;01-08-1983&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;While this works to some extent, you’ll notice that the query for the EAV-data is much more complex that the relational one. On top of that, in order to get the same amount of columns as the relational query, we need even more selfjoins! Needless to say, using lots and lots of selfjoins is stressfull for the database and has a negative impact on the performance. 
  &lt;br /&gt;A better (and more elegant) solution is to ‘rotate’ the table by making use of the &lt;a href="http://msdn.microsoft.com/en-us/library/ms177410.aspx"&gt;PIVOT&lt;/a&gt; operator that was introduced with SQL Server 2005.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Example 4: EAV table with PIVOT&lt;/span&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border-right:silver 1px solid;padding-right:4px;border-top:silver 1px solid;padding-left:4px;font-size:8pt;padding-bottom:4px;margin:20px 0px 10px;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;padding-top:4px;border-bottom:silver 1px solid;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;
    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum1" style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum2" style="color:#606060;"&gt;   2:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum3" style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum4" style="color:#606060;"&gt;   4:&lt;/span&gt;            , [100] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Name&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum5" style="color:#606060;"&gt;   5:&lt;/span&gt;            , [101] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Birthday&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum6" style="color:#606060;"&gt;   6:&lt;/span&gt;            , [102] &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; Nationality&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum7" style="color:#606060;"&gt;   7:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum8" style="color:#606060;"&gt;   8:&lt;/span&gt;     (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum9" style="color:#606060;"&gt;   9:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; ID, EntityID, AttributeID, &lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum10" style="color:#606060;"&gt;  10:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; EAV_Data&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum11" style="color:#606060;"&gt;  11:&lt;/span&gt;     ) p&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum12" style="color:#606060;"&gt;  12:&lt;/span&gt;     PIVOT&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum13" style="color:#606060;"&gt;  13:&lt;/span&gt;     (&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum14" style="color:#606060;"&gt;  14:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;MAX&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum15" style="color:#606060;"&gt;  15:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;FOR&lt;/span&gt; AttributeID &lt;span style="color:#0000ff;"&gt;IN&lt;/span&gt; ([100], [101], [102])&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum16" style="color:#606060;"&gt;  16:&lt;/span&gt;     ) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; pvt&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum17" style="color:#606060;"&gt;  17:&lt;/span&gt; ) AS result&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum18" style="color:#606060;"&gt;  18:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; Name = &lt;span style="color:#006080;"&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-top-style:none;line-height:12pt;padding-top:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;border-right-style:none;border-left-style:none;background-color:white;text-align:left;border-bottom-style:none;"&gt;&lt;span id="lnum19" style="color:#606060;"&gt;  19:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; Nationality = &lt;span style="color:#006080;"&gt;&amp;#39;English&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Result&lt;/span&gt;&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Birthday&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;&lt;strong&gt;Nationality&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;1234&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;12-12-1970&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;English&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="100"&gt;365&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;John&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;01-08-1983&lt;/td&gt;

      &lt;td valign="top" width="100"&gt;English&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;By making use of the PIVOT operator we are capable of ‘rotating’ the table, which makes it possible to query the data as if it was stored in a relational table. 
  &lt;br /&gt;Of course this is a very simple example, but combined with metadata it can be a powerful technique.&lt;/p&gt;

&lt;p&gt;Systems that make extensive use of EAV-modelling are usually backed up with a great amount of metadata, which is used to describe properties like the datatypes of attributes, the attributes that belong to a certain entity, the relation between entities, etc. 
  &lt;br /&gt;In my current project we use the metadata to generate an extra layer in the database, based on certain combinations of attributes. 

  &lt;br /&gt;This way it’s like we have a couple of hundred different (relational) tables, while everything is effectively stored in 1 big table.&lt;/p&gt;

&lt;p&gt;I hope this was an interesting read, in my next article I’ll elaborate on the subject of &lt;a href="http://blogs.infosupport.com/blogs/marks/archive/2010/02/22/handling-eav-data-timelines.aspx"&gt;handling multiple timelines&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=16053" width="1" height="1"&gt;</content><author><name>marks</name><uri>http://blogs.infosupport.com/members/marks/default.aspx</uri></author><category term="SQL" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/SQL/default.aspx" /><category term="EAV" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/EAV/default.aspx" /><category term="PIVOT" scheme="http://blogs.infosupport.com/blogs/marks/archive/tags/PIVOT/default.aspx" /></entry></feed>