2009-03-16 8 views
0

Ich erhielt ein Datenbankdesign, das Informationen über eine Organisation und alle Änderungen speichert, die den Organisationen passiert sind oder passieren werden. Da sich an einer Organisation so ziemlich alles ändern kann, gibt es eine Tabelle, die nur die eindeutigen Organisations-IDs in einer Tabelle mit dem Namen "Organisationen" enthält. Die Änderungen an diese Organisation haben alle ein effektives Datum und ein ähnliches Design-Mustern folgen, wie der folgenden, den Standortwechsel:Statusänderungen verfolgen/abfragen mit 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 ... 

Z.B. Organisationen können nur zwei Zeilen enthalten, die einfach die IDs 1 und 2 enthalten. Standorte haben 3 Standorte (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) 

Ich habe bereits eine große und möglicherweise übermäßig komplexe Abfrage für ein Datum anzugeben und einen Organisation Status zu diesem bestimmten Zeitpunkt immer wieder, aber ich fühle mich wie es ist wahrscheinlich ein einfacher Weg. Das vorliegende Problem ist jedoch das folgende:

Wie kann ich von diesem Schema eine Frage wie "Welche Organisationen werden von Standort 1 an einen anderen Standort verschoben werden und welche Organisationen von einem anderen Standort in den Standort 1 verschoben werden" beantworten gegebener Zeitrahmen: Datum1 bis Datum2? "

Eine ähnliche Frage, die auch die erste beantworten könnte, ist: Wie kann ich die Standortänderungen jeder Organisation (einfach) abfragen, während ich die VORHERIGE Position zeige, von der sie sich bewegen (schwer?)?

Hinweis: Beinhaltet das LINQ-Tag für den Fall, dass es eine einfache Möglichkeit gibt, es in LINQ zu tun, kann ich diese Route gehen.

Antwort

1

Zur Lage 1:

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

Von Standort 1:

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 

anzeigen vorherigen Standort:

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 

Mit einem UNIQUE INDEX auf (organization_id, eff_date) wird Ihnen helfen, eine Menge.

Verwandte Themen