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

Sql query for selecting the distinct values in multiple columns

我有一个 table 说,它有两列 zip code and customer code。假设有多个 customer code 具有相同的 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

输出为

我不知道这是怎么回事about.If你需要更多信息请在评论中告诉我。

您正在描述 group by

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

我不知道你的查询与问题有什么关系。问题明确是关于一个有两列的 table。

注意:您可能不需要 count(distinct) -- 除非在一个邮政编码中有多个行代表一个客户。