运行 `SELECT` 用于填充日期范围的 table
Running a `SELECT` for a range of dates to populate a table
我有一个(有点复杂的)SELECT
语句,它根据特定时间戳为特定日期从 table 中提取行的子集。天真地,像
SELECT id, runTime
FROM ...
...
WHERE runTime < @MyTime
其中 @MyTime
是一些日期时间参数。我需要 运行 SELECT
很长一段时间的连续日期(比如从今年 6 月 1 日到 11 月 1 日),将结果存储在 MyOutTable
中创建,字段 id
、runTime
、controlDate
,其中 controlDate
是用于从我的 [=33 检索 ID 和 runTime
的日期值=].
在 SQL 中是否有避免使用 Python 的捷径?假设有一些存储过程接受开始和结束日期参数并生成所需的数据,填充输出 table?
非常感谢
您可以使用带有 date/times 的子查询并使用 join
逻辑:
select d.dte, . . .
from (select cast('2017-06-01' as datetime) as dte union all
select cast('2017-06-02' as datetime) as dte union all
. . .
) d join
. . .
on runTime < d.dte;
INSERT INTO MyOutTable ( ControlDate, id, runTime )
SELECT '2017-10-30' as ControlDate, id, runTime
FROM ...
...
WHERE runTime < @MyTime
请注意,没有 VALUES
关键字,并且 ControlDate
值是常量文字,但您也可以在此处使用 CURDATE()
或类似内容。
我有一个(有点复杂的)SELECT
语句,它根据特定时间戳为特定日期从 table 中提取行的子集。天真地,像
SELECT id, runTime
FROM ...
...
WHERE runTime < @MyTime
其中 @MyTime
是一些日期时间参数。我需要 运行 SELECT
很长一段时间的连续日期(比如从今年 6 月 1 日到 11 月 1 日),将结果存储在 MyOutTable
中创建,字段 id
、runTime
、controlDate
,其中 controlDate
是用于从我的 [=33 检索 ID 和 runTime
的日期值=].
在 SQL 中是否有避免使用 Python 的捷径?假设有一些存储过程接受开始和结束日期参数并生成所需的数据,填充输出 table?
非常感谢
您可以使用带有 date/times 的子查询并使用 join
逻辑:
select d.dte, . . .
from (select cast('2017-06-01' as datetime) as dte union all
select cast('2017-06-02' as datetime) as dte union all
. . .
) d join
. . .
on runTime < d.dte;
INSERT INTO MyOutTable ( ControlDate, id, runTime )
SELECT '2017-10-30' as ControlDate, id, runTime
FROM ...
...
WHERE runTime < @MyTime
请注意,没有 VALUES
关键字,并且 ControlDate
值是常量文字,但您也可以在此处使用 CURDATE()
或类似内容。