【问题标题】:Query to find count of tagging for item in column A to item in column B查询以查找 A 列中的项目到 B 列中的项目的标记计数
【发布时间】:2020-11-14 13:57:54
【问题描述】:

我需要找出 SQL 中一列(州 - 北达科他州)到另一列(陆上、离岸)中相同条目的不同条目数。在随附的样本中 - 错配数为 1(北达科他州 - 近海)。非常感谢您的帮助。

.

【问题讨论】:

    标签: mysql sql database count


    【解决方案1】:

    你想要count(distinct)吗?

    select state, count(distinct terrain)
    from t
    group by state;
    

    【讨论】:

      【解决方案2】:

      您似乎想要计算具有超过 1 个不同 terrainstates 的数量。如果是这样,您可以使用两个级别的聚合:

      select count(*) no_mismatches
      from (select state from mytable group by state having min(state) <> max(state)) t
      

      【讨论】:

      • 感谢 GMB - 请再提供一项帮助。如果有多个州,我怎样才能只计算北达科他州。再次感谢
      • @Nishant: 你想where state = 'North Dakota' 在子查询中吗?
      • 是的 - 我在哪里可以在代码中使用它来计算仅在北达科他州的不匹配数。
      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      相关资源
      最近更新 更多