我的SQL Server有一个表。两列都是整数,但以YYYYMM的形式表示日期。我想查询该表并返回第三列,该列的每一行每一年/每个月在这两列的范围内都包括一个YYYYMM形式的整数。
这是表格:
+------------+------------+
| beg_YYYYMM | end_YYYYMM |
+------------+------------+
| 201802 | 201805 |
| 201711 | 201801 |
+------------+------------+
所需的输出:
+------------+------------+----------------+
| beg_YYYYMM | end_YYYYMM | month_in_range |
+------------+------------+----------------+
| 201802 | 201805 | 201802 |
| 201802 | 201805 | 201803 |
| 201802 | 201805 | 201804 |
| 201802 | 201805 | 201805 |
| 201711 | 201801 | 201711 |
| 201711 | 201801 | 201712 |
| 201711 | 201801 | 201801 |
+------------+------------+----------------+
答案 0 :(得分:3)
使用递归CTE:
with cte as (
select beg_YYYYMM, end_YYYYMM,
convert(date, convert(varchar(255), beg_YYYYMM) + '01') as dte,
convert(date, convert(varchar(255), end_YYYYMM) + '01') as end_dte
from t
union all
select beg_YYYYMM, end_YYYYMM,
dateadd(month, 1, dte),
end_dte
from cte
where dte < end_dte
)
select beg_yyyymm, end_yyyymm,
year(dte) * 100 + month(dte) as yyyymm
from cte
order by dte;
Here是db <>小提琴。
答案 1 :(得分:0)
我真的不认为我会实际部署它,但我想知道它的外观。如果创建的TVF做得很丑,那还不错。我使用Aaron Bertrand的DateDim code进行了快速修改,以仅获取传入的两个日期之间的月份日期。
CREATE OR ALTER FUNCTION dbo.tvf_MonthRange (@beg_YYYYMM int, @end_YYYYMM int)
RETURNS @Results TABLE
(month_in_range int)
AS
BEGIN
--Have to convert ints to dates
DECLARE @BegDate DATE;
SET @BegDate = DATEFROMPARTS(CAST(SUBSTRING(CAST(@beg_YYYYMM AS varchar(6)),1,4) AS INT), CAST(SUBSTRING(CAST(@beg_YYYYMM AS varchar(6)),5,2) AS INT), 1);
--This needs to be the second day of the month for the code below to work.
DECLARE @EndDate DATE;
SET @EndDate = DATEFROMPARTS(CAST(SUBSTRING(CAST(@end_YYYYMM AS varchar(6)),1,4) AS INT), CAST(SUBSTRING(CAST(@end_YYYYMM AS varchar(6)),5,2) AS INT), 2);
INSERT INTO @Results
SELECT (DATEPART(YEAR, d) *100) + DATEPART(MONTH, d)
FROM
(
SELECT d = DATEADD(DAY, rn - 1, @BegDate)
FROM
(
SELECT TOP (DATEDIFF(DAY, @BegDate, @EndDate))
rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
FROM sys.all_objects AS s1
CROSS JOIN sys.all_objects AS s2
-- on my system this would support > 5 million days
ORDER BY s1.[object_id]
) AS x
) AS y
WHERE DATEPART(DAY, d) = 1;
RETURN;
END
然后您可以这样称呼它。
DECLARE @Months TABLE (beg_YYYYMM int, end_YYYYMM int)
INSERT INTO @MONTHS SELECT 201802, 201805
INSERT INTO @MONTHS SELECT 201711, 201801
SELECT *
FROM @Months m
CROSS APPLY dbo.tvf_MonthRange(m.beg_YYYYMM, m.end_YYYYMM) mr ;
星期一不好主意吧?
答案 2 :(得分:0)
使用计数表的解决方案,在较大的数据集上可能更快。
with dates as(
select
201501 as beg_YYYYMM
,201504 as end_YYYYMM
union all
select '201711', '201801'
union all
select '201807', '201812'
),
--Tally table
ctedaterange AS (
SELECT top 15
rn = Row_number() OVER(ORDER BY (SELECT NULL)) -1
FROM sys.objects a
)
SELECT
dates.*
,months_in_range = convert(varchar(6), Dateadd(mm, rn, cast(cast(dates.beg_YYYYMM as varchar(8)) +'01' as date)), 112)
FROM dates
cross join ctedaterange
WHERE
rn <= Datediff(mm, cast(cast(dates.beg_YYYYMM as varchar(8)) +'01' as date), cast(cast(dates.end_YYYYMM as varchar(8)) +'01' as date))
ORDER BY
beg_YYYYMM
,Dateadd(mm, rn, cast(cast(dates.beg_YYYYMM as varchar(8)) +'01' as date))