postgres中的窗口分区

时间:2014-02-21 02:24:05

标签: sql postgresql window postgresql-8.4

在下面的查询中,我尝试使用字段product_id上的分区,并按week_end日期对其进行排序。我的问题是我们是否需要使用或rank()或某些聚合函数才能按特定列进行分区。因为如果我不使用rank()函数,下面的查询失败,但我不需要输出中的排名。有没有办法避免聚合功能,仍然使用分区。谢谢!

 select c.week_end,a.product_id,b.metric_id,c."week_value",c."13week_value",c."52week_value",rank() over(partition by a.product_id order by c.week_end) from 
    Product_t a,metric_t b, vin_example_unpivot c where  
    c.prod_nm = a.product_nm and
    c."Metric" = b.metric_code

1 个答案:

答案 0 :(得分:0)

您是否只想按产品ID和结束日期对结果进行排序?

select c.week_end, a.product_id, b.metric_id, c."week_value", c."13week_value", c."52week_value"
from vin_example_unpivot c join
     Product_t a
     on c.prod_nm = a.product_nm  join
     metric_t b
     on c."Metric" = b.metric_code
order by product_id, week_end;

请注意,我还修复了连接以使用正确的连接语法。

相关问题