在SQL递归中使用MAX(Date)值

时间:2018-07-10 17:19:18

标签: sql-server

我有一个SQL查询,该查询基于从多个表中选择的Min(Date)和Max(Date)执行递归。但是,当我尝试运行查询时,它会抛出GROUP BY, HAVING, or aggregate functions are not allowed in the recursive part of a recursive common table expression 'ctedaterange'.异常。我也尝试过使用“前1个日期”,但仍然没有希望。

SQL查询

ALTER VIEW [dbo].[CYExtraction]
AS

WITH ctedaterange
AS (SELECT
    [Dates] = (SELECT
    MIN(CreatedOn) AS CreatedOn
    FROM (SELECT
    MIN(CreatedOn) AS CreatedOn
    FROM (SELECT
    MIN(CreatedOn) AS CreatedOn
    FROM Items) AS it
    UNION ALL
    SELECT
    *
    FROM (SELECT
    MIN(CreatedOn) AS CreatedOn
    FROM Bibs) AS bib
    UNION ALL
    SELECT
    *
    FROM (SELECT
    MIN(CreatedOn) AS CreatedOn
    FROM Porders) AS po) AS AllItems)
UNION ALL
SELECT
    [dates] + 1
FROM ctedaterange
WHERE [dates] + 1 <= (SELECT
    MAX(CreatedOn) AS CreatedOn
FROM (SELECT
    MAX(CreatedOn) AS CreatedOn
FROM (SELECT
    MAX(CreatedOn) AS CreatedOn
FROM Items) AS it
UNION ALL
SELECT
    *
FROM (SELECT
    MAX(CreatedOn) AS CreatedOn
FROM Bibs) AS bib
UNION ALL
SELECT
    *
FROM (SELECT
    MAX(CreatedOn) AS CreatedOn
FROM Porders) AS po) AS AllItems))
SELECT
    [Dates] AS PK_Date,
    DATENAME(MONTH, [Dates]) + ' ' + CAST(YEAR([Dates]) AS varchar(4)) AS 
Month,
    CASE
        WHEN DATEPART(m, [Dates]) <= 3 THEN 'Q1 ' + CAST(YEAR([Dates]) AS varchar(4))
        ELSE CASE
            WHEN DATEPART(m, [Dates]) <= 6 THEN 'Q2 ' + CAST(YEAR([Dates]) AS varchar(4))
        ELSE CASE
            WHEN DATEPART(m, [Dates]) <= 9 THEN 'Q3 ' + CAST(YEAR([Dates]) AS varchar(4))
            ELSE CASE
                WHEN DATEPART(m, [Dates]) <= 12 THEN 'Q4 ' + CAST(YEAR([Dates]) AS varchar(4))
                ELSE ''
            END
        END
    END
END AS 'Quarter',
CASE
    WHEN DATEPART(m, [Dates]) <= 6 THEN 'HY1 ' + CAST(YEAR([Dates]) AS varchar(4))
    ELSE CASE
        WHEN DATEPART(m, [Dates]) <= 12 THEN 'HY2  ' + CAST(YEAR([Dates]) AS varchar(4))
        ELSE ''
    END
END AS 'HalfYear',
YEAR([Dates]) AS 'Year',
CAST('1-' AS varchar(5)) + CAST(DATEPART(M, [Dates]) AS varchar(10)) + '-' + CAST(YEAR([Dates]) AS varchar(4)) AS DateName
FROM ctedaterange


GO

主要是我需要将其包装在SQL视图中。对我的问题的任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将聚合移动到额外的CTE,所以这可能是您想要的:

ALTER VIEW [dbo].[CYExtraction] 
  (PK_Date, [Month], [Quarter], [HalfYear], [Year], [DateName])
AS
WITH cteAllItems (mindates, maxdates) AS (
  SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Items
  UNION ALL
  SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Bibs
  UNION ALL
  SELECT MIN(CreatedOn), MAX(CreatedOn) FROM Porders
),
   ctedaterange (Dates, MaxDate) AS (
  SELECT MIN(mindates), MAX(maxdates) 
  FROM cteAllItems
  UNION ALL
  SELECT DATEADD(day, 1, Dates), MaxDate 
  FROM ctedaterange
  WHERE DATEADD(day, 1, Dates) <= MaxDate
)
SELECT
  Dates AS PK_Date,
  DATENAME(MONTH, Dates) + N' ' + DATENAME(YEAR, Dates),
  N'Q' + DATENAME(QUARTER, Dates) + N' ' + DATENAME(YEAR, Dates),
  N'HY' + CAST((MONTH(Dates)-1)/6+1 AS nvarchar(1)) + N' ' + DATENAME(YEAR, Dates),
  YEAR(Dates),
  CONVERT(nvarchar(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, Dates), 0), 105)
FROM ctedaterange

GO