SQL 查询,限制2个日期之间的数据

SQL Query, bring data between 2 dates with limitation

在 SQL 中,我将编写一个查询,插入 2 个日期之间的所有数据,然后我想将其放入 1000 个批次中,但由于这些天之间的数据数量超过我的限制我打算写一个循环,使更小的周期来带来数据。 这是我的代码:

DECLARE @StartDate DATETIME = CAST('2021-06-02 01:00:00.000' AS DATETIME)
DECLARE @EndDate DATETIME = CAST('2021-06-23 01:00:00.000' AS DATETIME)

DECLARE @RealRowCount INT = (SELECT DISTINCT SUM(@@ROWCOUNT) OVER() FROM GetReport (
                         @StartDate, @EndDate))
DECLARE @TransactionCount INT = (SELECT DISTINCT TransactionCount FROM GetReport (
                             @StartDate, @EndDate))

WHILE @RealRowCount < @TransactionCount
BEGIN
DECLARE @DiffDate INT = (SELECT DATEDIFF(DAY, @StartDate, @EndDate))
SET @EndDate =  DATEADD(DAY, @DiffDate/2 ,@StartDate)

SELECT *,@StartDate, @EndDate  FROM GetReport (@StartDate, @EndDate)
END

PS: 我想找到日期的中间部分,然后将它们更改为新的 EneDate 和 StartDate,但是这里有问题!

你的问题不是很清楚。假设您在两个日期之间有 10,000 条记录,并且您不想一次检索超过一千条记录。在这种情况下,您可以使用分页。在程序代码和 SQL.

DECLARE @StartDate DATETIME = CAST('2021-06-02 01:00:00.000' AS DATETIME)
DECLARE @EndDate DATETIME = CAST('2021-06-23 01:00:00.000' AS DATETIME)

DECLARE @RealRowCount INT = (SELECT DISTINCT COUNT(*) FROM Products WHERE InsertDate BETWEEN @StartDate AND @EndDate)



DECLARE @Counter INT = 0
WHILE @Counter <= @RealRowCount
BEGIN
  SELECT *
  FROM Products
  WHERE InsertDate BETWEEN @StartDate AND @EndDate
  ORDER BY InsertDate
  OFFSET @Counter ROWS -- skip @Counter rows
  FETCH NEXT 1000 ROWS ONLY -- take 1000 rows

  SET @Counter = @Counter + 1000
END

或者您可以获取两个日期之间的时间差,并在特定步骤中每次添加开始日期并检索该日期的数据。

例如,日期相差20天。开始日期每次递增5步至开始日期与结束日期

我创建了另一个 table 来放置日期,如果这个 table 有任何行我可以得到 'EndDate',但如果它没有任何记录我只是使用日期我指定的。 AccSync 是我插入记录详细信息的 table,AccTransformation 是我要插入所有记录的 table。

DECLARE @Count INT = (SELECT COUNT(*) FROM [AccTransaction])

    DECLARE @Flag BIT = (SELECT IIF(@Count > 1, 1, 0))

    DECLARE @End DATETIME = GETDATE();
    DECLARE @Start DATETIME

    IF(@Flag = 0)
    BEGIN
        SET @Start = CAST('2021-03-08' AS DATETIME2);       
        SET @Flag = 1
    END

    ELSE IF(@Flag = 1)
    BEGIN
        SET @Start = (SELECT TOP 1 EndDate FROM (SELECT EndDate FROM [AccSync] ORDER BY ActionDate DESC OFFSET 0 ROW) AS TT);
    END 

    DECLARE @RealRowCount INT = (SELECT DISTINCT SUM(@@ROWCOUNT) FROM [GetReport] (@Start, @End));
    DECLARE @TransactionCount INT = (SELECT DISTINCT TransactionCount FROM [GetReport] (@Start, @End));
    ----------------------------------------------------------------------------------------------
    WHILE (@RealRowCount <> @TransactionCount)
    BEGIN
        DECLARE @DiffDate INT = (SELECT DATEDIFF(SECOND, @Start, @End))
        
        SET @End = DATEADD(SECOND, (@DiffDate/2), @Start)

        SET @RealRowCount = (SELECT DISTINCT SUM(@@ROWCOUNT) FROM [GetReport] (@Start, @End))
        SET @TransactionCount  = (SELECT DISTINCT TransactionCount FROM [GetReport] (@Start, @End))
    END
    ----------------------------------------------------------------------------------------------
    INSERT INTO [AccTransaction]
    SELECT *
    FROM [GetReport](@Start, @End)
    ----------------------------------------------------------------------------------------------
    INSERT INTO [AccSync]
    VALUES(NEWID(), GETDATE(), @Start, @End, ISNULL(@TransactionCount,0), DATEDIFF(SECOND, @Start, @End))