【发布时间】:2019-03-25 00:58:13
【问题描述】:
我试图隔离在任何一天只进行某种类型交易(现金、卡、支票)的客户。他们可能在某个日期进行了多次交易,但我想排除具有某种类型的交易。
我的交易表存储了客户编号、交易日期、交易类型、金额。如果唯一的交易(或交易)属于“卡”类型,我想查找客户和日期。
所以以下数据只会返回:3456, 26/03/2018
+---------+------------+------+--------+ |客户 |日期 |类型 |金额 | +---------+------------+------+--------+ |第1234章26/03/2018 |卡 | 10 | |第1234章26/03/2018 |现金 | 20 | |第1234章28/03/2018 |现金 | 20 | |第2345章26/03/2018 |现金 | 20 | |第2345章28/03/2018 |现金 | 20 | | 3456 | 26/03/2018 |卡 | 10 | | 3456 | 26/03/2018 |卡 | 20 | +---------+------------+------+--------+这是我的代码,但它只返回空白行。
select t1.customer, t1.date
from transaction t1
left join transaction t2 on t1.customer = t2.customer
and t1.date = t2.date
where t1.type = 'card' and t2.type <> 'card'
and (t2.customer is null and t2.date is null) group by t1.customer, t1.date
【问题讨论】:
标签: sql sql-server