具有多个约束条件的左联接排除

时间:2019-03-25 00:58:13

标签: sql sql-server

我正在尝试隔离在任何给定日期仅进行某种交易(现金,卡,支票)的客户。他们可能在某个日期进行过多次交易,但是我想排除那些具有某种类型的交易。

我称为交易的表存储了客户编号,交易日期,交易类型,金额。如果唯一的一项或多项交易属于“卡”类型,我想查找客户和日期。

因此,以下数据将仅返回:3456,26/03/2018

+----------+------------+------+--------+
| Customer |    Date    | Type | Amount |
+----------+------------+------+--------+
|     1234 | 26/03/2018 | Card |     10 |
|     1234 | 26/03/2018 | Cash |     20 |
|     1234 | 28/03/2018 | Cash |     20 |
|     2345 | 26/03/2018 | Cash |     20 |
|     2345 | 28/03/2018 | Cash |     20 |
|     3456 | 26/03/2018 | Card |     10 |
|     3456 | 26/03/2018 | Card |     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

1 个答案:

答案 0 :(得分:2)

我建议汇总和Add-Printer -ConnectionName "\\printsrv01\$($default_printer.sharename)"

having

Here是db <>小提琴,表明它可以正常工作。