什么查询将计算行数,但由三个参数区分?
例子:
Id Name Address
==============================
1 MyName MyAddress
2 MySecondName Address2
就像是:
select count(distinct id,name,address) from mytable
id
计算,name
和的唯一组合数address
:
SELECT Count(*)
FROM (
SELECT DISTINCT
id
, name
, address
FROM your_table
) As distinctified
获取所有 distinctid
和列name
并address
计算结果行。
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
必须返回每个 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
给定 t_pono 是 smallint 数据类型,而 t_item 是 varchar(16) 数据类型