Sql server 使用SQL跟踪/查询状态更改

Sql server 使用SQL跟踪/查询状态更改,sql-server,linq,tsql,Sql Server,Linq,Tsql,我得到了一个数据库设计,其中存储了有关组织的信息以及组织已经发生或将要发生的任何更改。由于几乎任何组织都可能发生变化,因此有一个表在名为Organizations的表中只包含唯一的OrganizationID。对该组织的所有变更都有一个生效日期,并遵循类似的设计模式,如以下位置变更: Table: organization_locations organization_id (int, not null) - Relates back to the Organizations.ID colum

我得到了一个数据库设计,其中存储了有关组织的信息以及组织已经发生或将要发生的任何更改。由于几乎任何组织都可能发生变化,因此有一个表在名为Organizations的表中只包含唯一的OrganizationID。对该组织的所有变更都有一个生效日期,并遵循类似的设计模式,如以下位置变更:

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 ...
例如。 组织可能只包含两行,分别保存id的1和2。 位置可以有3个位置id、名称:

我已经有了一个大型的、可能过于复杂的查询,用于指定日期并返回给定时间的组织状态,但我觉得可能有一种更简单的方法。然而,目前的问题如下:

从这个模式中,我如何回答这样一个问题:在给定的时间范围内,哪些组织将从位置1移动到另一个位置,哪些组织将从另一个位置移动到位置1:date1到date2

同样可以回答第一个问题的一个类似问题是:如何在显示每个组织从硬位置移动到硬位置的同时轻松查询每个组织的位置更改

注意:包括LINQ标签,以防在LINQ中有一种简单的方法,我可以走这条路线。

到位置1:

SELECT  DISTINCT organization_id
FROM    organization_locations ol
WHERE   ol.eff_date BETWEEN @date1 AND @date2
        AND ol.location = 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
从位置1:

SELECT  DISTINCT organization_id
FROM    organization_locations ol
WHERE   ol.eff_date BETWEEN @date1 AND @date2
        AND ol.location = 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
显示以前的位置:

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
在组织id上有一个唯一的索引,eff\u date将对您有很大帮助