0

我需要使用 SQL Server 2016 中的时态表功能编写查询以获取以下输出。

客户表(系统版本表)

Id
Name
[period_start] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL
[period_end] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL

订单表

Id
CustomerId
..
[creation_date] DATETIME default SYSUTCDATETIME()

输出:OrderId,...,客户名称

客户名称应反映与订单日期相关的正确名称

以下查询将为我提供所需的输出,但我需要检查这是否是执行此类查询的正确方法..

SELECT T.Id, O.Name
FROM Orders T
INNER JOIN (
SELECT *
FROM Customer 
FOR SYSTEM_TIME FROM '1900-01-01' TO '9999-12-31 23:59:59.9999999'
) O ON T.CustomerId = O.ID 
AND T.creation_date BETWEEN O.period_start AND O.period_end

提前致谢

查明达。

4

2 回答 2

0

您的查询中似乎有无关的标准。我认为这可以满足您的要求:

SELECT T.Id, O.Name
FROM Orders T INNER JOIN Customer O 
ON T.CustomerId = O.ID 
于 2017-10-27T07:59:02.390 回答
0

我认为它没有任何问题,除了嵌套查询可能是不必要的:

SELECT T.Id, O.Name
FROM Orders T
JOIN Customer FOR SYSTEM_TIME FROM '1900-01-01' TO '9999-12-31 23:59:59.9999999' O 
    ON T.CustomerId = O.ID 
WHERE T.creation_date BETWEEN O.period_start AND O.period_end
于 2017-10-27T07:59:22.403 回答