1

我有下表

在此处输入图像描述

我使用了以下查询,我收到了错误消息。我可以确定错误的原因,但我该如何解决

select min(id),customer_id,created_at from thunderbolt_orders
  group by customer_id

我需要最小 id 的 customer_id 和 created_at 我怎样才能实现它。

4

4 回答 4

5
select distinct on (customer_id)
    customer_id, id, created_at
from thunderbolt_orders
order by customer_id, id
于 2013-08-23T12:32:24.053 回答
2
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
于 2013-08-23T12:33:57.050 回答
1
SELECT id,customer_id,created_at 
       FROM thunderbolt_orders 
       WHERE id IN 
       (SELECT MIN(id) FROM thunderbolt_orders GROUP BY customer_id); 
于 2013-08-23T12:28:51.797 回答
0

根据您是否只需要最小 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
于 2013-08-23T13:01:31.683 回答