sql查询,用于在多个列中选择不同的值

gdrx4gfi  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(349)

我有一个有两列的表 zip code and customer code . 假设有多个 customer code 同样的 zipcode . 我需要获取上述两列,以便一列包含唯一的zipcode,第二列包含对应邮政编码的唯一客户代码的数量。
这里的问题是,对于一个邮政编码,可能有多个唯一的客户(有点像一个客户可能多次使用同一邮政编码去商店,但我需要将他们视为一个)
我怎么能在家里做呢 SQLite . 我做了以下事情 SQL query 我只有单身 zip code ```
select distinct(C.pin_code),count(distinct(C.customer_code)) from customers as C inner join Sales as S on S.customer_code=C.customer_code

输出为
![](https://i.stack.imgur.com/xMw6V.png)
我不知道这是怎么发生的。如果你需要更多的信息,请让我在评论中知道。
dgenwo3n

dgenwo3n1#

你在描述 group by :

select zip_code, count(distinct customer_code)
from t
group by zip_code;

我不知道你的问题和这个问题有什么关系。问题是关于一个有两列的表。
注意:您可能不需要 count(distinct) --除非一个邮政编码中有多个行用于一个客户。

相关问题