SQL:基于两个日期之间的月份为记录创建多行

SQL:基于两个日期之间的月份为记录创建多行,sql,netezza,Sql,Netezza,我的表格有如下不同Id和不同开始和结束日期的记录 ID,开始日期,结束日期 2017年02月14日2018年11月05日 我想编写一个SQL而不使用date维度表,该表给出以下输出:基本上,在开始日期和结束日期之间,每个月都有一条记录 1, 2017, 02 1, 2017, 03 1, 2017, 04 1, 2017, 05 1, 2017, 06 1, 2017, 07 1, 2017, 08 1, 2017, 09 1, 2017, 10 1, 2017, 11 1, 2017, 12

我的表格有如下不同Id和不同开始和结束日期的记录 ID,开始日期,结束日期 2017年02月14日2018年11月05日


我想编写一个SQL而不使用date维度表,该表给出以下输出:基本上,在开始日期和结束日期之间,每个月都有一条记录

1, 2017, 02
1, 2017, 03
1, 2017, 04
1, 2017, 05
1, 2017, 06
1, 2017, 07
1, 2017, 08
1, 2017, 09
1, 2017, 10
1, 2017, 11
1, 2017, 12
1, 2018, 01
1, 2018, 02
1, 2018, 03
1, 2018, 04
1, 2018, 05
1, 2018, 06
1, 2018, 07
1, 2018, 09
1, 2018, 10
1, 2018, 11

您必须生成日期,并从中选择年份和月份

select distinct year(date),month( date) from
(select  * from (
select 
 date_add('2017-02-14 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) as date 
  from
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n1,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n2,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n3,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n4,
(select 0 as num
   union all select 1
   union all select 2
   union all select 3
   union all select 4
   union all select 5
   union all select 6
   union all select 7
   union all select 8
   union all select 9) n5
) a
where date >'2017-02-14 00:00:00.000' and date < '2018-11-05'
) as t

请使用以下查询示例:

set @start_date = '2017-02-14';
set @end_date = LAST_DAY('2018-11-05');


WITH RECURSIVE date_range AS
(
select MONTH(@start_date) as month_, YEAR(@start_date) as year_, DATE_ADD(@start_date, INTERVAL 1 MONTH) as next_month_date
UNION
SELECT MONTH(dr.next_month_date) as month_, YEAR(dr.next_month_date) as year_, DATE_ADD(dr.next_month_date, INTERVAL 1 MONTH) as next_month_date
FROM date_range dr
where next_month_date <= @end_date
)
select month_, year_ from date_range 
order by next_month_date desc

这就是我所做的,它就像一个符咒:

-- sample data  
WITH table_data
AS (
    SELECT 1 AS id
        ,cast('2017-08-14' AS DATE) AS start_dt
        ,cast('2018-12-16' AS DATE) AS end_dt

UNION ALL

SELECT 2 AS id
    ,cast('2017-09-14' AS DATE) AS start_dt
    ,cast('2019-01-16' AS DATE) AS end_dt
)

-- find minimum date from the data  
,starting_date (start_date)
AS (
SELECT min(start_dt)
FROM TABLE_DATA
)

--get all months between min and max dates  
,all_dates
AS (
SELECT last_day(add_months(date_trunc('month', start_date), idx * 1)) month_date
FROM starting_date
CROSS JOIN _v_vector_idx
WHERE month_date <= add_months(start_date, abs(months_between((
                    SELECT min(start_dt) FROM TABLE_DATA), (SELECT max(end_dt) FROM TABLE_DATA))) + 1)
ORDER BY month_date
)
SELECT id  
,extract(year FROM month_date)  
,extract(month FROM month_date)  
,td.start_dt  
,td.end_dt  
FROM table_data td  
INNER JOIN all_dates ad  
    ON ad.month_date > td.start_dt  
        AND ad.month_date <= last_day(td.end_dt)  
ORDER BY 1  
    ,2  

你能添加更多的样本数据吗?我看不出你告诉我们的单记录和预期的输出表之间的连接。基本上每个月在开始日期和结束日期之间都有一个记录。认真考虑应用程序代码中的数据显示问题,你是使用MySQL还是NETEZZA?请正确标记您的问题。我使用的是Netezza,没有使用维度date维度表有日期字段,您需要从中选择月份和年份。我们没有日期维度表。如果我的表有两行怎么办?那么您需要更正您的设计,因为第二个日期持续时间已由1ST更改。有多条记录具有不同的开始和结束日期。我刚才举了一个Record1的例子,请您提供表名,这样我就可以给您提供完整的查询解决方案。因为对于这个解决方案,一条记录或更多记录都无关紧要。表名就是数据。它有3个字段ID、STARTDT、ENDDT。不同的ID具有不同的开始和结束日期。Netezza不支持递归查询