In a project I'm working on, there is a table of units and then there are several tables that pertain to the units' locations at a given point in time as well as other factors for a given point in time.
For example, Unit1 might be at LocationA on 1/1/2009 and have a second location entry that has Unit1 at LocationB on 3/1/2010. Likewise, Unit1 might be Size1 on 2/20/2009 and Size2 on 1/12/2010.
Table Structures
Table: Unit
int ID (identity, PK)
varchar Name
Table: Unit_Location
int OrgID (FK Unit.ID)
datetime eff_date
int LocationID (FK Location.ID)
Table: Unit_Size
int OrgID (FK Unit.ID)
datetime eff_date
int UnitSizeID (FK Size.ID)
I want to have a view that takes all of the changes from the various property change tables and has row for every date that the unit is different and have the current values for all the properties in that row. So for the example listed above, the view would like:
1/1/2009 - Unit1 - LocationA - Null
2/20/2009 - Unit1 - LocationA - Size1
1/12/2010 - Unit1 - LocationA - Size2
3/1/2010 - Unit1 - LocationB - Size2
If I do a full outer join based on the effective dates, I believe I can easily get to:
1/1/2009 - Unit1 - LocationA - Null
2/20/2009 - Unit1 - Null - Size1
1/12/2010 - Unit1 - Null - Size2
3/1/2010 - Unit1 - LocationB - Null
But that doesn't quite get me to what I want. What should my SQL look like to get a view with the full status of every Unit for every date that something with the unit changes? enter code here