Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each attribute is actually a distinct entity in this model, but they are all sharing the same storage in the same physical table (Why?). This yields:</p> <pre><code>with data as ( select entity = 1, attribute = 'ben', attributeType=1, enddate = convert(datetime,'99990101') union all select entity = 1, attribute = 'alt', attributeType=2, enddate = convert(datetime,'99990101') union all select entity = 1, attribute = '12345', attributeType=3, enddate = convert(datetime,'99990101') union all select entity = 2, attribute = 'sam', attributeType=1, enddate = convert(datetime,'99990101') union all select entity = 2, attribute = 'smith', attributeType=2, enddate = convert(datetime,'99990101') union all select entity = 2, attribute = '67890', attributeType=3, enddate = convert(datetime,'99990101') union all select entity = 2, attribute = '68790', attributeType=3, enddate = convert(datetime,'20130331') union all select entity = 2, attribute = '876', attributeType=3, enddate = convert(datetime,'19810101') ) select top 1 FirstName, LastName, AccountNum from ( select top 1 a1.entity, FirstName, LastName from ( select entity, enddate, attribute as FirstName from data d where d.enddate &gt;= getdate() and attributeType = 1 ) a1 join ( select entity, enddate, attribute as LastName from data where enddate &gt;= getdate() and attributeType = 2 ) a2 on a1.entity = a2.entity and a1.enddate = a2.enddate where FirstName = 'sam' and LastName = 'smith' and a1.enddate &gt;= getdate() and a2.enddate &gt;= getdate() order by a1.enddate ) E left join ( select entity, enddate, attribute as AccountNum from data where enddate &gt;= getdate() and attributeType = 3 ) a3 on a3.entity = E.entity order by a3.enddate </code></pre> <p>returning:</p> <pre><code>FirstName LastName AccountNum --------- -------- ---------- sam smith 68790 </code></pre> <p>Note that it is quite common for accountign departments, at least, to enter future transactions during quiet times of the month, especially if those transactions will take effect during busy times of the month (ie month-end). The same for annual transactions. One should not assume that only one record can exist with an expiry > getdate().</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload