我正在尝试获取“已发生的交易数量”的计数。 数据可能如下所示。
Cust # Trans# TransType LineItem
42 5000 1 1
42 6000 1 1
42 6000 1 2
42 6000 2 1
42 6000 2 2
42 6000 2 3
任何给定的交易编号可以有多种交易类型。在此示例中,我期望返回的“交易数量”计数为“3”,因为 Trans# 5000 只有一种不同的 TransType,而 6000 有两种。如果我对 Trans# 进行不同计数,我会得到“2”,如果我只进行一次计数,我会得到“6”。
我尝试过使用:
COUNT(DISTINCT CASE Trans# WHEN ???? THEN 1 ELSE null END) AS [Num of Transactions],
但我知道我的方向并不完全正确。如果有人能指出我正确的方向,我将不胜感激。
最佳答案
试试这个:-
with cte as
(
Select Cust,Trans,row_number() over (partition by trans,TransType order by cust) rn
from Sample
)
Select count(*) as TransCount from cte
where rn=1
关于t-sql - 计数 'Number of Transactions',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573621/