postgresql 选择ID重复的所有数据

olmpazwi  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(166)

我试图从客户购物超过1的数据库中获取所有数据
当前代码:

select * from sales s 
join closest_dealerships cd 
on s.customer_id=cd.customer_id 
order by s.customer_id;

我尝试过:

select *,
    case
        when s.customer_id in (select s.customer_id from sales s 
                            group by (s.customer_id)
                            having count(s.customer_id) >1
                            order by s.customer_id)
    end 
from sales s 
join closest_dealerships cd 
on s.customer_id=cd.customer_id 
order by s.customer_id;

电流输出:

| customer_id | product_id | column1 | columnx
|      1      |      8     |         |
|      2      |      7     |         |
|      2      |      1     |         |
|      3      |     12     |         |
|      4      |     23     |         |

输出我希望:

| customer_id | product_id | column1 | columnx
|      2      |      7     |         |
|      2      |      1     |         |
|      4      |     23     |         |
|      4      |      9     |         |
|      4      |     12     |         |
yh2wf1be

yh2wf1be1#

您想要显示链接的数据列,但只有在客户有一个以上的数据列时才显示。因此,请计算每个客户的数据列,并只保留计数大于1的数据。

select *
from
(
  select s.*, c.*, count(*) over (partition by s.customer_id) as cnt
  from sales s 
  join closest_dealerships cd using (customer_id)
) counted
where cnt > 1
order by customer_id;

相关问题