0

我正在将旧的报告系统迁移到现代 Java 代码库,并偶然发现了 Microsoft Sql Server 存储过程,它生成了带有日期(Year、Period、WeekStarting、WeekEnding)的表。我需要将此代码迁移到 Java 中并使其动态化,而不是生成一个表并占用数据库中的空间。

寻求 Sql Server 专家的帮助,以帮助我了解这些日期是如何得出的,尤其是Period列中的数字

USE [Reporting]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GenDataforPeriodsTable]
    @enddate VARCHAR(10)
AS
    DECLARE @startdate VARCHAR(10)
BEGIN
    SET NOCOUNT ON; 

    SELECT @startdate = DATEADD(DAY, 1, MAX(WeekEnding)) FROM Periods;

    WITH CTE_DatesTable
    AS
    (
      SELECT CAST(@startdate as date) AS tempdate
      UNION ALL
      SELECT DATEADD(dd, 1, tempdate)
      FROM CTE_DatesTable
      WHERE DATEADD(dd, 1, tempdate) <= @enddate
    )
    INSERT INTO Periods (YEAR, Period, WeekStarting, WeekEnding)
    SELECT YEAR(tempdate) as Year, MONTH(DATEADD(DAY, -3, tempdate)) as Period, 
            DATEADD(DAY, -6, tempdate) as WeekStarting, tempdate as WeekEnding 
    FROM CTE_DatesTable 
    WHERE DATENAME(weekday, tempdate) = 'SUNDAY'        
    OPTION (MAXRECURSION 0)

END
GO

它生成这样的表:

在此处输入图像描述

4

1 回答 1

2

Java 8 代码:

import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

import java.time.LocalDate;
static void genDataforPeriodsTable(LocalDate endDate) {
    String sql = "SELECT MAX(WeekEnding) FROM Periods";
    LocalDate maxWeekEnding = /* Result of running query */;
    genDataforPeriodsTable(maxWeekEnding.plusDays(1), endDate);
}

static void genDataforPeriodsTable(LocalDate startDate, LocalDate endDate) {
    System.out.println("Year Period WeekStarting WeekEnding");
    System.out.println("==== ====== ============ ==========");
    for (LocalDate tempdate = startDate.with(nextOrSame(SUNDAY));
                   tempdate.compareTo(endDate) <= 0;
                   tempdate = tempdate.plusDays(7)) {
        int year = tempdate.getYear();
        int period = tempdate.minusDays(3).getMonthValue();
        LocalDate weekStarting = tempdate.minusDays(6);
        LocalDate weekEnding = tempdate;
        System.out.printf("%4d %-6d %-12s %s%n", year, period, weekStarting, weekEnding);
    }
}

测试

genDataforPeriodsTable(LocalDate.of(2019, 10, 25), LocalDate.of(2020, 5, 5));

输出

此处的输出与您包含的内容的顺序相反,但数据是相同的,除了 2020 年第一周的开始日期错误,正如Lothar 所评论的那样

Year Period WeekStarting WeekEnding
==== ====== ============ ==========
2019 10     2019-10-21   2019-10-27
2019 10     2019-10-28   2019-11-03
2019 11     2019-11-04   2019-11-10
2019 11     2019-11-11   2019-11-17
2019 11     2019-11-18   2019-11-24
2019 11     2019-11-25   2019-12-01
2019 12     2019-12-02   2019-12-08
2019 12     2019-12-09   2019-12-15
2019 12     2019-12-16   2019-12-22
2019 12     2019-12-23   2019-12-29
2020 1      2019-12-30   2020-01-05
2020 1      2020-01-06   2020-01-12
2020 1      2020-01-13   2020-01-19
2020 1      2020-01-20   2020-01-26
2020 1      2020-01-27   2020-02-02
2020 2      2020-02-03   2020-02-09
2020 2      2020-02-10   2020-02-16
2020 2      2020-02-17   2020-02-23
2020 2      2020-02-24   2020-03-01
2020 3      2020-03-02   2020-03-08
2020 3      2020-03-09   2020-03-15
2020 3      2020-03-16   2020-03-22
2020 3      2020-03-23   2020-03-29
2020 4      2020-03-30   2020-04-05
2020 4      2020-04-06   2020-04-12
2020 4      2020-04-13   2020-04-19
2020 4      2020-04-20   2020-04-26
2020 4      2020-04-27   2020-05-03
于 2020-02-22T22:36:08.030 回答