我需要编写一个查询,以便在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
提前致谢
Chaminda。
答案 0 :(得分:0)
您的查询中似乎有无关的标准。我认为这可以满足您的所有要求:
SELECT T.Id, O.Name
FROM Orders T INNER JOIN Customer O
ON T.CustomerId = O.ID
答案 1 :(得分: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