我有下表
我使用了以下查询,我收到了错误消息。我可以确定错误的原因,但我该如何解决
select min(id),customer_id,created_at from thunderbolt_orders
group by customer_id
我需要最小 id 的 customer_id 和 created_at 我怎样才能实现它。
我有下表
我使用了以下查询,我收到了错误消息。我可以确定错误的原因,但我该如何解决
select min(id),customer_id,created_at from thunderbolt_orders
group by customer_id
我需要最小 id 的 customer_id 和 created_at 我怎样才能实现它。
select distinct on (customer_id)
customer_id, id, created_at
from thunderbolt_orders
order by customer_id, id
with cte as (
select
*, row_number() over(partition by customer_id order by id) as row_num
from Table1
)
select *
from cte
where row_num = 1
SELECT id,customer_id,created_at
FROM thunderbolt_orders
WHERE id IN
(SELECT MIN(id) FROM thunderbolt_orders GROUP BY customer_id);
根据您是否只需要最小 ID 或是否需要每个客户的最小 ID,这些都是解决方案。
最低身份证:
select top 1 id,customer_id,created_at from thunderbolt_orders order by id asc
每个客户的最低 ID:
with cte as (
select min(id) as id
from thunderbolt_orders
group by customer_id
)
select *
from cte c
inner join thunderbolt_orders t on t.id = c.id