我已经根据今天的日期编写了SQL来生成12行。基本上它是月份编号,月份名称和从当前月份开始使用GETDATE()的年份列表,然后在适当的时候为每一行和一年添加一个月。我现在需要限制此数据,以便仅显示包括March在内的行。所以今天我想看到2014年12月,2015年1月,2015年2月和2015年3月的4行。
我已经尝试添加一个额外的列,其中四月始终为1,一直到三月,总是十二。我还尝试在行号中包含另一列,其中1始终位于顶部,因此这个月。我无法理解如何构建WHERE子句以满足在随后几个月中全年使用的SQL。如果仅仅是这个月,那么没问题,但是如何限制显示在三月之前可以在任何月份运行的行?
出于测试的目的,我已经用@DATE替换了GETDATE(),并将其设置为全年的各个日期。 SQL的第一部分看起来像这样 -
DECLARE @DATE AS DATE
SET @DATE = '2014-10-01'
SELECT CAST (DATEPART (MONTH,@DATE) AS VARCHAR) AS MONTH_NO,
CAST (DATENAME (MONTH,@DATE) AS VARCHAR) AS MONTH_NAME,
CAST (DATEPART (YEAR,@DATE) AS VARCHAR) AS YEAR,
CASE WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 3 THEN 12
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 4 THEN 1
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 5 THEN 2
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 6 THEN 3
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 7 THEN 4
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 8 THEN 5
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 9 THEN 6
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 10 THEN 7
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 11 THEN 8
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 12 THEN 9
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 1 THEN 10
WHEN (CAST (DATEPART (MONTH,@DATE) AS VARCHAR)) = 2 THEN 11
ELSE 0 END AS DISPLAY,
1 AS ROW
然后有11个UNION在每个适当的时候添加1个月的值和1年的值,所以接下来的位看起来像这样 -
UNION ALL
SELECT CASE WHEN DATEPART (MONTH,@DATE) = 12 THEN 1 ELSE DATEPART (MONTH,@DATE) + 1 END AS MONTH_NO,
CAST(DATENAME(MONTH, DATEADD(month, + 1, @DATE)) AS VARCHAR) AS MONTH_NAME,
CASE WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) < CAST (DATEPART (MONTH,@DATE AS INTEGER) THEN CAST (DATEPART (YEAR, DATEADD (YEAR, + 1, (@DATE))) AS VARCHAR) ELSE CAST (DATEPART (YEAR,@DATE) AS VARCHAR) END AS YEAR,
CASE WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 3 THEN 12
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 4 THEN 1
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 5 THEN 2
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 6 THEN 3
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 7 THEN 4
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 8 THEN 5
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 9 THEN 6
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 10 THEN 7
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 11 THEN 8
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 12 THEN 9
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 1 THEN 10
WHEN CAST (DATEPART (MONTH, DATEADD (MONTH, + 1, (@DATE))) AS INTEGER) = 2 THEN 11
ELSE 0 END AS DISPLAY,
2 AS ROW
然后在SQL的下一位等等中使用+ 2。
仅包含DISPLAY和ROW列以尝试提出解决方案,并且可以将其删除。由于我一直在尝试各种解决方案,因此在过去的几天里,上述情况变得相当臃肿。我根本无法根据March在12个结果中出现的位置选择顶部'n'行?
感谢。
弗朗西斯
答案 0 :(得分:0)
有一种更简单的方法可以使用基于CTE的数字表完全按照您的要求进行操作。
首先,在CTE中获取12个数字的列表,将它们添加到您开始的日期,直到有12个月。
其次,使用这些月份来执行计算和日期名称。
第三,返回CTE并检查与下一个可用March相对应的数字,并过滤到仅包括到那时为止的记录。
完整代码:
DECLARE @DATE AS DATE
SET @DATE = '2014-10-01';
WITH
E1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) s(N)),
cteMonths(N, D) AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
DATEADD(MONTH, ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1, @DATE)
FROM E1 )
SELECT
CAST(DATEPART(MONTH, D) AS VARCHAR(2)) AS MONTH_NO,
CAST(DATENAME(MONTH, D) AS VARCHAR(20)) AS MONTH_NAME,
CAST(DATEPART(YEAR, D) AS VARCHAR(4)) AS MONTH_YEAR,
N AS ROW_NO
FROM cteMonths
WHERE
N <= (SELECT N FROM cteMonths WHERE DATEPART(MONTH,D) = 3)