2009-03-16 11 views
0

m'a donné une conception de base de données qui stocke des informations sur une organisation et des changements qui se sont produits ou se produiront aux organisations. Comme pratiquement tout peut changer au sujet d'une organisation, il y a une table qui ne contient que l'unique OrganizationID dans une table appelée "Organisations". Les modifications apportées à cette organisation ont tous une date et suivre un modèle de conception similaire, comme les suivantes, le changement de lieu:Suivi/Interrogation Statut Changements à l'aide SQL

Table: organization_locations 

organization_id (int, not null) - Relates back to the Organizations.ID column. 
location_id (int, not null) - Relates to Locations.ID 
eff_date (datetime, not null) - The date this change becomes effective 

Table: Locations 

ID (int, pk, identity, not null) - ID of the location 
Name (varchar(255), not null) - Name of the location 
... Other miscellaneous columns that aren't important for this discussion ... 

PAR EXEMPLE Les organisations peuvent simplement contenir deux lignes qui contiennent simplement les ID 1 et 2 respectivement. endroits peuvent avoir 3 emplacements (id, name):

1, Location1 
2, Location2 
3, Location3 

organization_locations (organization_id, location_id, eff_date): 

1, 1, 1/1/2000 <--- Organization 1 is starting at location 1 
1, 2, 1/1/2010 <--- On 1/1/2010, organization 1 moves to location 2 (from location 1) 
1, 3, 1/1/2011 <--- On 1/1/2011, organization 1 moves to location 3 (in this case from location 2) 

J'ai déjà un grand et peut-être question trop complexe pour spécifier une date et revenir un statut d'organisations à ce moment, mais je me sens comme il y a probablement un moyen plus facile. Cependant, le problème à résoudre est la suivante:

A partir de ce schéma, comment puis-je répondre à une question comme « Qu'est-ce que les organisations vont passer d'emplacement 1 à un autre endroit et ce que les organisations se déplacer vers l'emplacement 1 à un autre endroit dans la délai donné: date1 à date2? "

Une question similaire qui pourrait également répondre à la première est: Comment puis-je interroger l'emplacement de chaque organisation change (facile) tout en montrant l'emplacement précédent, ils passent de (dur?)?

Note: Y compris la balise LINQ dans le cas où il y a un moyen facile de le faire dans LINQ Je peux aller dans cette voie.

Répondre

1

à l'emplacement 1:

SELECT DISTINCT organization_id 
FROM organization_locations ol 
WHERE ol.eff_date BETWEEN @date1 AND @date2 
     AND ol.location = 1 

De 1 Lieu:

SELECT DISTINCT organization_id 
FROM (
     SELECT organization_id, 
       (
       SELECT TOP 1 location_id 
       FROM organization_locations oln 
       WHERE oln.organization_id = ol.organization_id 
         AND oln.eff_date < ol.eff_date 
       ORDER BY 
         organization_id DESC, eff_date DESC 
       ) AS previous_location 
     FROM organization_locations ol 
     WHERE eff_date BETWEEN @date1 AND @date2 
     ) olo 
WHERE previous_location = 1 

Montrer emplacement précédent:

SELECT ol.*, 
     (
     SELECT TOP 1 location_id 
     FROM organization_locations oln 
     WHERE oln.organization_id = ol.organization_id 
       AND oln.eff_date < ol.eff_date 
       AND location_id = 1 
     ORDER BY 
       organization_id DESC, eff_date DESC 
     ) AS previous_location 
FROM organization_locations ol 

Avoir un UNIQUE INDEX sur (organization_id, eff_date) vous aidera beaucoup.

Questions connexes