我尝试做的是创建下面查询的函数,而不是使用+ 45天,函数迭代+ 35,36,37 ... + 45天的所有值并返回MAX值对于每个cobrand_id。
注意此查询中有45天出现两次。这将是输入变量。
with q1 as (
SELECT '17_Q4' qqyy, cobrand_id, sum(calc) AS a
FROM temp_08.jwn_calc s
where s.optimized_transaction_date > DATE '2016-10-29' AND
s.optimized_transaction_date <= DATE '2016-10-29' + interval '45 days'
GROUP BY qqyy, cobrand_id )
select q1.qqyy, q1.cobrand_id, q1.a/q2.d AS sum from q1
inner join (
SELECT cobrand_id AS c,
sum(CASE WHEN optimized_transaction_date > date '2015-10-31'
AND optimized_transaction_date <= date '2015-10-31' + interval '45 days' THEN Calc END) /
sum(CASE WHEN optimized_transaction_date > date '2015-10-31'
AND
optimized_transaction_date <= date '2015-10-31' + interval '91 days' THEN Calc END) AS d
FROM temp_08.jwn_calc
GROUP BY cobrand_id
) as q2 on q1.cobrand_id = q2.c
从概念上讲,我正在做的是查看去年45天完成财务季度的百分比,并用它来推算/除以得出今年季度的全部价值。
为了回应Generate_series,我的问题很明显,我在15个不同的日子(35,36,37 ...... 45天)查询以获取最大值。