Redshift-按周对数据进行排序

时间:2019-03-22 07:20:41

标签: sql sql-order-by amazon-redshift

我正在尝试按周对Redshift中的某些数据进行排序。

select CONCAT(CONCAT('Week', ' '), 
EXTRACT(WEEK FROM sale_date::date + '1 day'::interval)) as date,count(*) as total 
from sales 
where sale_date between ('2019-03-22' - 30) and '2019-03-22' 
group by EXTRACT(WEEK FROM  sale_date::date + '1 day'::interval)
order by 1 asc;

上面的查询工作正常,但是输出如下:

Week 10, 10
Week 11, 20
Week 12, 3
Week 7, 12
Week 8, 3
Week 9, 5

如何修改上面的查询,使输出如下:

Week 7, 12
Week 8, 3
Week 9, 5
Week 10, 10
Week 11, 20
Week 12, 3

1 个答案:

答案 0 :(得分:2)

您需要为numeric type语句将提取的星期值添加到order by中。因此,转换

order by 1 asc

order by cast(EXTRACT(WEEK FROM sale_date::date + '1 day'::interval) as integer)