0

我在 SQL Server 2012 Express 中有一个会计日历表。我希望能够提取每周的第一个日期并从下周开始连续显示它们。意思是,我希望第一行显示 2013 年 9 月 2 日,然后是 2013 年 9 月 9 日、2013 年 9 月 16 日等。这是我如何构建表格以及一些数据。

[dbo].[FiscalCalendar](
    [FiscalDate] [datetime] NULL,
    [FiscalWeekNum] [smallint] NULL,
    [FiscalMonthNum] [smallint] NULL,
    [FiscalMonthName] [varchar](9) NULL,
    [FiscalYear] [smallint] NULL

FiscalDate          FiscalWeekNum   FiscalMonthNum  FiscalMonthName FiscalYear
2013-09-02 00:00:00.000 36          9           September   2013
2013-09-03 00:00:00.000 36          9           September   2013
2013-09-04 00:00:00.000 36          9           September   2013
2013-09-05 00:00:00.000 36          9           September   2013
2013-09-06 00:00:00.000 36          9           September   2013
2013-09-07 00:00:00.000 36          9           September   2013
2013-09-08 00:00:00.000 36          9           September   2013
2013-09-09 00:00:00.000 37          9           September   2013
2013-09-10 00:00:00.000 37          9           September   2013
2013-09-11 00:00:00.000 37          9           September   2013
2013-09-12 00:00:00.000 37          9           September   2013
2013-09-13 00:00:00.000 37          9           September   2013
2013-09-14 00:00:00.000 37          9           September   2013
2013-09-15 00:00:00.000 37          9           September   2013
4

1 回答 1

1
SET DATEFIRST 7;

;WITH x AS 
(
  SELECT <cols>, rn = ROW_NUMBER() OVER 
    (PARTITION BY FiscalWeekNum ORDER BY FiscalDate)
  FROM dbo.FiscalCalendar
  WHERE FiscalDate >= CONVERT(DATE, GETDATE())
    AND DATEPART(WEEKDAY, FiscalDate) = 2
)
SELECT <cols> FROM x WHERE rn = 1;
于 2013-08-29T21:43:05.083 回答