我一直在这个墙上撞到墙上一段时间。我有一组数据,我需要识别其事务组满足特定条件的CUSTOMER。我已粘贴在下面的数据集中。我们可以将表格称为“A”。我希望我在输出问题时能够清楚地记录这个问题!
我正在寻找一种方法来识别其交易组满足以下条件的CUSTOMER:
非常感谢任何有关这方面的帮助!
CUSTOMERID, TRANSID, TRANSTYPE, DATE
0027, 2, 17, 40948
0027, 3, 21, 40950
0085, 4, 17, 40955
0187, 14, 17, 41199
0187, 15, 21, 41214
0187, 16, 17, 41244
0513, 1, 17, 40940
0513, 5, 21, 40981
0565, 8, 21, 41032
0565, 11, 17, 41080
0715, 6, 17, 41009
0715, 7, 21, 41009
2780, 9, 17, 41039
2985, 10, 17, 41069
2985, 12, 21, 41091
2985, 13, 17, 41144
答案 0 :(得分:2)
SELECT a.CUSTOMERID
FROM a
INNER JOIN a b ON a.CUSTOMERID = b.CUSTOMERID
WHERE a.DATE > b.DATE
AND a.TRANSTYPE = 21
AND b.TRANSTYPE = 17
答案 1 :(得分:1)
Select CUSTOMERID, TRANSID, TRANSTYPE, DATE
from A a1
where TRANSTYPE=17
and Exists(select * from a a2 where a2.CUSTOMERID=a1.CUSTOMERID and a2.TRANSID=21 and a2.Date>a1.Date)