如何对表行进行分区获取每个进程的最小值和最大值

时间:2017-06-06 19:08:24

标签: sql amazon-redshift

有人可以让我知道如何从第一张表中获取输出。请注意,我不允许使用循环。我试过窗口功能,但没有运气。

CUSTOMER_NUMBER ORDER_NO PROCESS
100             1001        1
100             1002        1
100             1003        2
100             1004        2
100             1005        2
100             1006        3
100             1007        3
100             1008        3
101             1009        1
101             1010        1
101             1011        2
101             1012        2
101             1013        2

CUSTOMER_NUMBER MIN_VALUE, MAX_VALUE, PROCESS
100             1001        1002            1
100             1003        1005            2
100             1006        1008            3
101             1009        1010            1
101             1011        1013            2

2 个答案:

答案 0 :(得分:0)

你去吧

select
  min(ORDER_NO) as MIN_VALUE,
  max(ORDER_NO) as MAX_VALUE,
  PROCESS
from yourTable
group by PROCESS

答案 1 :(得分:0)

如果您想通过窗口函数获得结果,可以使用max()over()和min()over()窗口函数:

 SELECT
  customer_number,
  order_no,
  min(order_no)
  OVER (
    PARTITION BY customer_number, process ) AS min_value,
  max(order_no)
  OVER (
    PARTITION BY customer_number, process ) AS max_value,
  process
FROM table_1

这将产生具有额外2列(min_value,max_value)的原始表。