按年和月分组,并获得一个月的最小值,以及日期

时间:2018-11-06 07:31:54

标签: sql postgresql greatest-n-per-group postgresql-9.5

这就是我的桌子的样子

Column  |     Type     |
--------+--------------+
id      | integer      |
date    | date         |
open    | numeric(9,2) |
close   | numeric(9,2) |
low     | numeric(9,2) |
high    | numeric(9,2) |

我想获取给定月份中所有月份的最低收盘价日期。

这是我设法得到的,

SELECT 
    temp_table.year, temp_table.month, MIN(close)
FROM
    (
        SELECT 
            date_trunc('year', date) as year, date_trunc('month', date) as month, date, close 
        FROM
            sensex_sensex
        GROUP BY 
            year, month, date, close
        ORDER BY 
            year, month
    ) AS temp_table
GROUP BY
    temp_table.year, temp_table.month
ORDER BY
    temp_table.year DESC, temp_table.month DESC;

这给了我年份月份和最低收盘价。 但是当我尝试添加日期时,我得到了所有行,而不是按年和月分组。 如何获得结果

Year | Month | Date of Lowest Close in a Month | Lowest Close in a Month

此外,

我还希望能够在一个月中找到包含日期的最小5个结束值,并再次按年和月分组。

3 个答案:

答案 0 :(得分:1)

使用corelatted子查询

SELECT 
  date_trunc('year', date) as year, date_trunc('month', date) as month, date, close 
            FROM
                sensex_sensex t where t.close=( select min(close)
                                    from 
                                   sensex_sensex t1
  where   date_trunc('year', t1.date)=date_trunc('year', t.date)
    and
     date_trunc('month', t1.date)=date_trunc('month', t.date)
 )

或使用窗口功能

with cte (   
    select  
         date_trunc('year', date) as year,
         date_trunc('month', date) as month, date, close,
         min (close) over ( order by date ) rn 
    FROM
         sensex_sensex
) select * from cte where cte.rn=1

答案 1 :(得分:1)

demo:db<>fiddle

使用window function MIN选择每帧的最小值(在您的情况下为月份)。

SELECT
    extract('year' FROM mydate) as year,
    extract('month' FROM mydate) as month,
    mydate, close
FROM (
    SELECT
        mydate,
        close,
        MIN(close) OVER (PARTITION BY date_trunc('month', mydate)) as min_close
    FROM
        temp_table
) s
WHERE close = min_close

您可以使用MIN代替ROW_NUMBER。如果您不仅要选择一个最小值,还要选择两个或五个最小值(n),这将对您有帮助:

SELECT
    extract('year' FROM mydate) as year,
    extract('month' FROM mydate) as month,
    mydate, close
FROM (
    SELECT
        mydate,
        close,
        ROW_NUMBER() OVER (PARTITION BY date_trunc('month', mydate) ORDER BY close) as rn
    FROM
        temp_table
) s
WHERE rn <= n              -- n is the number of values you get.

您是否真的需要从日期轻松计算的年份和月份的单独列?

答案 2 :(得分:0)

您可以使用相关子查询

     SELECT date_trunc('year', date) as year, date_trunc('month', date) as month, date, close 
     FROM sensex_sensex a where close in 
    (select min(close) from sensex_sensex b 
     where date_trunc('year', a.date)=date_trunc('year', b.date) 
     and date_trunc('month', a.date)=date_trunc('month', b.date))

或者您可以通过窗口函数row_number()

select * from
(
SELECT date_trunc('year', date) as year, date_trunc('month', date) as month, date, close,row_number() over(partition by date_trunc('year', date),date_trunc('month', date) order by close) as rn
         FROM sensex_sensex
)A where rn=1