交叉连接以获取特定年份特定月份的所有季度末月份
Cross join to get all quarter end months for specific month in particular year
我尝试编写逻辑代码以获得低于输出但没有成功。我脑海
现在 运行 空白。你能和我分享你的想法吗?我确定我们在一起
可以找出 SQL 中的一些最简单的解决方案以获得以下结果
输出。
加更,我不要Q4键。
请注意,table 是一个包含更多列的月份维度,例如
Prev01, 02 ... 月键。
标记有 SQL 服务器和 Teradata,而且我只关心逻辑而不关心语法。我正在使用 Teradata。
现有数据:
预期输出:
所以一年的预期记录总数为 12*3 = 36。
编辑: 对于每一年,我们都有估算和预算值。例如
对于 2016 年,估算值将来自 2016 年 3 月(Q1)、2016 年 6 月(Q2)、2016 年 9 月(Q3)。还有一个预算值是从 2015 年 12 月到 2015 年(第 4 季度)。
因此,2016 年以来的任何一个月都将具有相同的预算估算值。只有在年份发生变化时才会发生变化。我希望现在清楚了...
不幸的是,我无法共享 table DDL 或样本数据。所以试图尽可能简单地解释这个场景。我能够编写 SQL 代码来获取预算值,但对于估算,情况有点棘手。所以假设对于 2016 年 4 月,我想要的估计值是 2016 年 3 月、2016 年 6 月、2016 年 9 月。
现有数据是类似于示例数据的截图。
添加更多,月键就像一个标识列。 Jan-2017 的月份键为 13 等等...
在MSSQL中,我们可以进行如下操作:
declare @temp table (month_key int,Months varchar(100),Years int,Quarter_Start int)
insert into @temp values (1 ,'Jan',2016, 1 )
insert into @temp values (2 ,'Feb',2016, 1 )
insert into @temp values (3 ,'Mar',2016, 1 )
insert into @temp values (4 ,'Apr',2016, 2 )
insert into @temp values (5 ,'May',2016, 2 )
insert into @temp values (6 ,'Jun',2016, 2 )
insert into @temp values (7 ,'Jul',2016, 3 )
insert into @temp values (8 ,'Aug',2016, 3 )
insert into @temp values (9 ,'Sep',2016, 3 )
insert into @temp values (10,'Oct',2016, 4 )
insert into @temp values (11,'Nov',2016, 4)
insert into @temp values (12,'Dec',2016, 4)
select 1 as 'Month_Key','Jan' as 'Month',Years,Months+'-16' as 'Scenario',month_key as 'Quarter End' from @temp where month_key in
(select max(month_key) from @temp group by Quarter_Start)
这里是
select t1.month_key, t1.Month, Year, t3.Month+'-'+right(CAST(Year as varchar(4)),2) Scenario, t2.month_key Quarter_End
from YourData t1
cross join (
select Quarter_Start, MAX(month_key) month_key
from YourData
group by Quarter_Start
) t2
join (
select month_key, Month
from YourData
) t3 on t2.month_key = t3.month_key
where t2.month_key<>12 -- if you do not want last quarter
order by 1,5
我尝试编写逻辑代码以获得低于输出但没有成功。我脑海 现在 运行 空白。你能和我分享你的想法吗?我确定我们在一起 可以找出 SQL 中的一些最简单的解决方案以获得以下结果 输出。
加更,我不要Q4键。 请注意,table 是一个包含更多列的月份维度,例如 Prev01, 02 ... 月键。
标记有 SQL 服务器和 Teradata,而且我只关心逻辑而不关心语法。我正在使用 Teradata。
现有数据:
预期输出:
所以一年的预期记录总数为 12*3 = 36。
编辑: 对于每一年,我们都有估算和预算值。例如
对于 2016 年,估算值将来自 2016 年 3 月(Q1)、2016 年 6 月(Q2)、2016 年 9 月(Q3)。还有一个预算值是从 2015 年 12 月到 2015 年(第 4 季度)。 因此,2016 年以来的任何一个月都将具有相同的预算估算值。只有在年份发生变化时才会发生变化。我希望现在清楚了...
不幸的是,我无法共享 table DDL 或样本数据。所以试图尽可能简单地解释这个场景。我能够编写 SQL 代码来获取预算值,但对于估算,情况有点棘手。所以假设对于 2016 年 4 月,我想要的估计值是 2016 年 3 月、2016 年 6 月、2016 年 9 月。 现有数据是类似于示例数据的截图。
添加更多,月键就像一个标识列。 Jan-2017 的月份键为 13 等等...
在MSSQL中,我们可以进行如下操作:
declare @temp table (month_key int,Months varchar(100),Years int,Quarter_Start int)
insert into @temp values (1 ,'Jan',2016, 1 )
insert into @temp values (2 ,'Feb',2016, 1 )
insert into @temp values (3 ,'Mar',2016, 1 )
insert into @temp values (4 ,'Apr',2016, 2 )
insert into @temp values (5 ,'May',2016, 2 )
insert into @temp values (6 ,'Jun',2016, 2 )
insert into @temp values (7 ,'Jul',2016, 3 )
insert into @temp values (8 ,'Aug',2016, 3 )
insert into @temp values (9 ,'Sep',2016, 3 )
insert into @temp values (10,'Oct',2016, 4 )
insert into @temp values (11,'Nov',2016, 4)
insert into @temp values (12,'Dec',2016, 4)
select 1 as 'Month_Key','Jan' as 'Month',Years,Months+'-16' as 'Scenario',month_key as 'Quarter End' from @temp where month_key in
(select max(month_key) from @temp group by Quarter_Start)
这里是
select t1.month_key, t1.Month, Year, t3.Month+'-'+right(CAST(Year as varchar(4)),2) Scenario, t2.month_key Quarter_End
from YourData t1
cross join (
select Quarter_Start, MAX(month_key) month_key
from YourData
group by Quarter_Start
) t2
join (
select month_key, Month
from YourData
) t3 on t2.month_key = t3.month_key
where t2.month_key<>12 -- if you do not want last quarter
order by 1,5