【问题标题】:Sql query for selecting the distinct values in multiple columns用于选择多列中不同值的 Sql 查询
【发布时间】:2020-10-02 06:36:18
【问题描述】:

我有一张表说它有两列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

输出是

我不知道这是怎么回事。如果您需要更多信息,请在 cmets 中告诉我。

【问题讨论】:

    标签: mysql sql sqlite count distinct


    【解决方案1】:

    你在描述group by

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

    我不知道您的查询与该问题有什么关系。这个问题明确地是关于一个有两列的表。

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

    【讨论】:

    • 谢谢,对不起,我是 SQL 新手。假设如果我需要在一个列中重复邮政编码并获取相应的客户代码,这可能吗?
    • @RAMSHANKERG 。 . .您可能应该用适当的样本数据和期望的结果提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2017-01-21
    • 2020-08-20
    • 2022-09-27
    相关资源
    最近更新 更多