使用 date/time 字段查询时间 table
Querying temporal table with date/time fields
我需要使用 SQL 服务器 2016 中的临时 table 功能编写查询以获取以下输出。
客户Table(系统版本table)
Id
Name
[period_start] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL
[period_end] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL
订单Table
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
提前致谢
查明达
您的查询中似乎有无关的条件。我认为这可以满足您的所有要求:
SELECT T.Id, O.Name
FROM Orders T INNER JOIN Customer O
ON T.CustomerId = O.ID
我看不出有什么问题,除了可能不需要嵌套查询:
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
我需要使用 SQL 服务器 2016 中的临时 table 功能编写查询以获取以下输出。
客户Table(系统版本table)
Id
Name
[period_start] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL
[period_end] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL
订单Table
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
提前致谢
查明达
您的查询中似乎有无关的条件。我认为这可以满足您的所有要求:
SELECT T.Id, O.Name
FROM Orders T INNER JOIN Customer O
ON T.CustomerId = O.ID
我看不出有什么问题,除了可能不需要嵌套查询:
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