Sql 为唯一列的每5行选择前10行

Sql 为唯一列的每5行选择前10行,sql,postgresql,group-by,Sql,Postgresql,Group By,在Postgres DB中,给定以下简化的订单数据集: 如何编写查询来选择前5个唯一的门店id,以及按从旧到新顺序订购的每个门店的前10个订单,从而最多返回50行? 例如: ID STORE_ID UPDATED_AT 1 "store-1" 2021-01-01 4 "store-1" 2021-01-04 ... 13 "store-1" 2021-01-12 2 "

在Postgres DB中,给定以下简化的订单数据集:

如何编写查询来选择前5个唯一的门店id,以及按从旧到新顺序订购的每个门店的前10个订单,从而最多返回50行? 例如:

ID    STORE_ID    UPDATED_AT
1     "store-1"   2021-01-01
4     "store-1"   2021-01-04
...
13    "store-1"   2021-01-12

2     "store-2"   2021-01-02
5     "store-2"   2021-01-05
...
18    "store-2"   2021-01-08

3     "store-3"   2021-01-03
8     "store-3"   2021-01-12
...
22    "store-3"   2021-01-22
我的目标是从最早的订单处理到最新的订单,但每个商店都要处理最早的10个订单,因为我可以使用Shopify API对它们进行批处理,这将更加高效

例如,在我的代码中,我会将其组合为:

{
  "store-1": [{ /* order */ }, { /* order */ }, { /* order */ }, ...], // Array of 10 orders
  "store-2": [{ /* order */ }, { /* order */ }, { /* order */ }, ...], // Array of 10 orders
  "store-3": [{ /* order */ }, { /* order */ }, { /* order */ }, ...], // Array of 10 orders
  "store-4": [{ /* order */ }, { /* order */ }, { /* order */ }, ...], // Array of 10 orders
  "store-5": [{ /* order */ }, { /* order */ }, { /* order */ }, ...], // Array of 10 orders
}
因此,我可以为每个存储并行运行5个API调用

我尝试了多个查询,包括以下内容:

从订单a中选择a.store\u id 内连接 按门店标识从订单组中选择门店标识按分钟更新的订单,限制为5b 在a.store\u id=b.store\u id上 a.U在ASC更新的订单; 但是,我不能将每个门店ID的行数限制为10行,st为 选择不同的存储\u id 从命令 订购人 限制5 选择st.store\u id,l.updated\u位于 从圣 横向交叉连接 在上选择更新的\u 从命令 其中o.store\u id=st.store\u id 在desc更新的订单 限制10 L 1 a每个门店仅返回一条记录,使用DISTINCT ON b按日期确定前5家门店的订单。
2.这些存储可以在横向联接中使用,以过滤每个存储的订单,再次使用更新的\u at order.

一个可能的查询:

select ID,    STORE_ID,    UPDATED_AT
from (
  select 
      orders.* ,
      row_number() over (partition by STORE_ID order by UPDATED_AT desc) rn_order,
      dense_rank() over(order by STORE_ID) store_rank
  from orders
  order by STORE_ID
) ranked 
where store_rank <= [count srores] and rn_order <= [count orders per store];

我会为此编写程序代码,可能是在数据库中。从问题中不清楚前五个StoreID是什么意思。我应该输入什么?我在ASC尝试了ORDER BY updated_,但对于SELECT DISTINCT,ORDER BY表达式必须出现在SELECT LIST中。此外,我不确定odate来自何处。我已经更新了问题底部的查询,显示了我的位置。我的错误。odate在时变为l.U。如果在插入语句的开头加上0,'zzz','2020-01-01',则st CTE应为@SMan.在您的小提琴中建议的。。我希望这会出现在结果中,因为它有最早的日期,但事实并非如此,因为它的名字以zYou'r right开头。这是因为按商店名称订购。我在这里改变了这一点:您最近对代码段的编辑似乎很有希望。。让我再做一些测试:
SELECT
    o.*
FROM (
    SELECT
        store_id
    FROM (
        SELECT DISTINCT ON (store_id)    -- 1a
            store_id, updated_at
        FROM orders
        ORDER BY store_id, updated_at
    ) s
    ORDER BY updated_at                  -- 1b
    LIMIT 5
) s
CROSS JOIN LATERAL (                     -- 2
    SELECT 
        *
    FROM orders o
    WHERE o.store_id = s.store_id
    ORDER BY updated_at
    LIMIT 10
) o
select ID,    STORE_ID,    UPDATED_AT
from (
  select 
      orders.* ,
      row_number() over (partition by STORE_ID order by UPDATED_AT desc) rn_order,
      dense_rank() over(order by STORE_ID) store_rank
  from orders
  order by STORE_ID
) ranked 
where store_rank <= [count srores] and rn_order <= [count orders per store];