如何从时间戳属性中找到 GROUP BY 特定属性和 day/month.year 的中位数?

How to find median that was GROUP BY specific attribute and the day/month.year from timestamp attribute?

我需要跟踪某些产品的每月交易价格中值,但到目前为止我的查询根本不起作用

这是原始的 table,结果是我希望看到的结果:

原始:

预期结果:

到目前为止,我尝试编写类似这样的查询

    SELECT
  DISTINCT year_confirm, month_confirm, item_info, median_price
FROM (SELECT
    item_info,
    product_price,
    extract(month from created_date) as month_confirm,
    extract(year from created_date) as year_confirm,
    PERCENTILE_CONT(product_price, 0.5) OVER (PARTITION BY item_info) 
        AS median_price
  FROM
    table_name
    order by item_info asc, year_confirm asc, month_confirm asc
)

但结果将显示每个月产品的相同中位数价格。有解决办法吗?谢谢

关于您的 SQL,在 PARTITION BY 中添加您想要的列会有所帮助。

SELECT DISTINCT item_info, month_confirm, year_confirm, PERCENTILE_CONT(product_price, 0.5) OVER (PARTITION BY item_info, month_confirm, year_confirm) as median_price
FROM (SELECT item_info,
             product_price,
             extract(month from created_date) as month_confirm,
             extract(year from created_date) as year_confirm,
             product_price
FROM table_name
ORDER BY item_info asc, year_confirm asc, month_confirm asc)