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

Group by year and month and get min value for a month, with the date

这就是我的 table 的样子

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 个收盘价及其日期,再次按年和月分组。

可以使用关联子查询

     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))

或者您可以 window 函数 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

使用关联子查询

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)
 )

或使用window函数

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

demo:db<>fiddle

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

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

您可以使用 ROW_NUMBER 而不是 MIN。如果想要 select 不仅是最小的一个,而且是两个或五个最小的数字 (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.

您真的需要可以从日期轻松计算出的年月单独列吗?