简体   繁体   English

计算 SQL 中多列中的不同值对

[英]Count distinct value pairs in multiple columns in SQL

What query will count the number of rows, but distinct by three parameters?什么查询会计算行数,但由三个参数区分?

Example:例子:

Id      Name        Address 
==============================
1     MyName        MyAddress
2     MySecondName  Address2

Something like:就像是:

select count(distinct id,name,address) from mytable

To get a count of the number of unique combinations of id , name and address :要计算idnameaddress的唯一组合的数量:

SELECT Count(*)
FROM   (
        SELECT DISTINCT
               id
             , name
             , address
        FROM   your_table
       ) As distinctified

获取所有不同的idnameaddress列并计算结果行。

SELECT COUNT(*) FROM mytable GROUP BY id, name, address

我刚刚想出的另一种(可能不是生产就绪或推荐的)方法是将值连接到一个字符串并特别地计算这个字符串:

SELECT count(DISTINCT concat(id, name, address)) FROM mytable;

您还可以执行以下操作:

SELECT COUNT(DISTINCT id + name + address) FROM mytable

Having to return the count of a unique Bill of Materials (BOM) where each BOM have multiple positions, I dd something like this:必须返回唯一的物料清单 (BOM) 的计数,其中每个 BOM 都有多个位置,我添加如下内容:

select t_item, t_pono, count(distinct ltrim(rtrim(t_item)) + cast(t_pono as varchar(3))) as [BOM Pono Count]
from BOMMaster
where t_pono = 1
group by t_item, t_pono

Given t_pono is a smallint datatype and t_item is a varchar(16) datatype给定 t_pono 是 smallint 数据类型,t_item 是 varchar(16) 数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM