我有一个表由TransactionTime通过employeeID进行交易。每个员工可能有多个同时发生的交易。例如:EmployeeID 1在12处有2个事务。我需要在每个时间间隔按EmployeeID对事务求和。因此对于employeeID 1,新列(TotalTransactionsByTime)结果将为2.接下来,如果给定TransactionTime的CODE的CODE为BAD,我需要在该时间增量中排除所有事务。因此,对于EmployeeID 2,我需要从结果集中排除所有三个事务,因为它们的CODE为'BAD',它使该增量的所有事务无效。
我的表
|EmployeeID|TransactionTime|CODE|
1 12 GOOD
1 12 GOOD
1 5 GOOD
2 1 BAD --need to omit all 3 transactions for employeeID 2
2 1 GOOD
2 1 GOOD
3 3 GOOD
3 3 GOOD
A correct result would look like:
|EmployeeID | TransactionTime | CODE | NUMBERTRNS
1 12 GOOD | 2
1 5 GOOD | 1
3 3 GOOD | 2
答案 0 :(得分:1)
select mt1.EmployeeID, mt1.TransactionTime, mt1.CODE, count(*) as NUMBERTRNS
from MyTable mt1
where mt1.EmployeeID not in (select EmployeeID from MyTable where CODE = 'BAD')
group by mt1.EmployeeID, mt1.TransactionTime, mt1.CODE