【发布时间】:2020-03-06 20:21:16
【问题描述】:
我在 Redshift (postgreSQL) 中有一个不寻常的问题。在这里复制太长了,但我团队中有人有这样的代码:
select (year) as year, (month) as month, (other_col) as other_col, sum(numerical_val) as num
from big_table
where (a few conditions)
group by (year), (month), (other_col)
union
select (year) as year, (month) as month, (other_col) as other_col, sum(numerical_val) as num
from big_table
where (more specific condition)
group by (year), (month), (other_col);
我认为没有必要将(年份)作为年份等,所以我删除了它们并这样做:
select year, month, other_col, sum(numerical_val) as num
from big_table
where (a few conditions)
group by year, month, other_col
union
select year, month, other_col, sum(numerical_val) as num
from big_table
where (more specific condition)
group by year, month, other_col;
我希望得到相同的输出,但 numeric_val 的差异只有百分之几。那是什么呀?
我尝试了几次实验,并且仅使用一个查询(无联合),与“(年)作为年”等相比,使用“年”得到了相同的确切结果。联合发生了一些变化。
你能猜出为什么“(年)作为年”部分会改变联合中的输出吗?
【问题讨论】:
-
可能是查询结果缓存?您更改了查询文本,使其在逻辑上保持不变,但原始查询可能已被缓存
-
您能否提供一些示例输入和输出来演示您所描述的内容?
标签: sql postgresql select amazon-redshift