例如: -
Customer_ID Transaction_type
111 Payroll
111 Saving
112 payroll
113 Online
113 Payroll
114 Payroll
1)我想要Customer_Id 112和114谁只有工资单帐户。 2)我希望客户111和113分别拥有其他交易类型的工资单。
问题仍然不太清楚,但简单的GROUP BY
条款足以满足上述要求
谁只有一个工资核算帐户
select Customer_ID
from table
group by Customer_ID
having count(Transaction_type) = 1 and
sum(case when Transaction_type = 'payroll' then 1 else 0 end) = 1
谁有其他交易类型与工资单分开
select Customer_ID
from table
group by Customer_ID
having count(Transaction_type) > 1 and
sum(case when Transaction_type = 'payroll' then 1 else 0 end) = 1
假设您要查询只有一种交易类型的客户
SELECT Customer_ID, COUNT(*)
FROM yourtable
GROUP BY Customer_ID
HAVING COUNT(*) = 1;
以明确查询具有不同交易类型的客户的相同方式
SELECT Customer_ID, COUNT(*)
FROM yourtable
GROUP BY Customer_ID
HAVING COUNT(*) > 1;
PS:这可以根据你的问题进行改进