我想总结根据2个不同的值达到阈值的次数:日期和项目。
Redshift给我一个错误,要求在group by中添加更多的列(我不希望在group by中使用:“列必须出现在group by子句中或在聚合函数中使用”)。
将会出现的另一个问题是代码将聚合我的列,并且我想复制SUM编号。
是)我有的
|------------|--------|------------|
| item_type | date |thresh_rchd |
|------------|--------|------------|
| baby |monday | 2 |
|------------|--------|------------|
| tom |monday | 6 |
|------------|--------|------------|
| baby |monday | 8 |
|------------|--------|------------|
| baby |tuesday | 4 |
|------------|--------|------------|
我想要的是:
|------------|--------|------------|-------------|
| item_type | date |thresh_rchd |total thresh |
|------------|--------|------------|-------------|
| baby |monday | 2 | 10 |
|------------|--------|------------|-------------|
| tom |monday | 6 | 6 |
|------------|--------|------------|-------------|
| baby |monday | 8 | 10 |
|------------|--------|------------|-------------|
| baby |tuesday | 4 | 4 |
|------------|--------|------------|-------------|
您可以通过使用窗口功能尝试如下
select item_type, date, thresh_rchd,
sum(thresh_rchd) over(partition by item_type, date) as total_thresh
from tablename
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句