我想编写一个输出a / c(除以c)的查询,该变量由名为cobrand_id的变量分组。
编写此查询的最无缝方式是什么?谢谢。
select sum(calc) a, cobrand_id b
from temp_08.jwn_calc
where optimized_transaction_date > '2015-10-31'
and optimized_transaction_date <= '2015-10-31' + 45
GROUP BY cobrand_id
select sum(calc) c, cobrand_id d
from temp_08.jwn_calc
where optimized_transaction_date > '2015-10-31'
and optimized_transaction_date <= '2015-10-31' + 91
GROUP BY cobrand_id
答案 0 :(得分:2)
您可以将现有查询用作子查询,并将它们连接起来以划分a / c
Select q1.b, q1.a/q2.c as result
From (your first query) q1
Inner join (your second query) q2 on q1.b = q2.d
答案 1 :(得分:1)
使用条件聚合:
select cobrand_id,
Sum(case when optimized_transaction_date > '2015-10-31'
and optimized_transaction_date <= '2015-10-31' + 45 then Calc end) /
Sum(case when optimized_transaction_date > '2015-10-31'
and optimized_transaction_date <= '2015-10-31' + 91 then Calc end)
from temp_08.jwn_calc
where optimized_transaction_date > '2015-10-31'
and optimized_transaction_date <= '2015-10-31' + 91
GROUP BY cobrand_id