在Postgres中,给定以下简化的orders数据集
ID STORE_ID UPDATED_AT
1 "store-1" 2021-01-01
2 "store-2" 2021-01-02
3 "store-3" 2021-01-03
4 "store-1" 2021-01-04
5 "store-2" 2021-01-05
6 "store-3" 2021-01-06
...
1000 "store-1" 2021-02-05如何编写一个查询来选择前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调用。
我尝试了多个查询,包括以下内容:
SELECT a.store_id FROM orders a
INNER JOIN
(SELECT store_id FROM orders GROUP BY store_id ORDER BY MIN(updated_at) LIMIT 5) b
ON a.store_id = b.store_id
ORDER BY a.updated_at ASC;但我不能将每个存储ID的行限制为10行。
发布于 2021-03-10 10:53:29
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
) o1( a)使用DISTINCT ON b只返回每个商店一条记录),确定按日期排序的前5家商店。2.这些存储可以在横向连接中用于筛选每个商店的订单,也可以使用updated_at订单。
发布于 2021-03-10 10:28:38
with st as
(
select distinct store_id
from orders
order by <your business rule here>
limit 5
)
select st.store_id, l.updated_at
from st
cross join lateral
(
select updated_at
from orders o
where o.store_id = st.store_id
order by updated_at desc
limit 10
) l;发布于 2021-03-10 11:11:55
还有一个可能的查询:
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];https://stackoverflow.com/questions/66562668
复制相似问题