在JOIN子句中添加条件

时间:2019-07-17 21:16:51

标签: sql join query-optimization inner-join data-analysis

我在下面编写了查询以解决以下提示。我没有WHERE子句,只在JOIN中使用AND作为条件:

确定某人成为工资直接存款人的日期。直接存款人是指每月从直接存款中至少存款$ 100,由同一实体支付的金额大致相同(+或-10%),并在31天内至少支付2个付款期。

样本数据:https://docs.google.com/spreadsheets/d/11CFFhkXyo9CuikMDhCKadOXZ1URhBc_c9C_nYPqHozo/edit?usp=sharing

SELECT TRUNC(d2.TRANSACTION_TIMESTAMP),
       d2.USER_DATA_2,
       d2.USER_ID
FROM dataset d1
INNER JOIN dataset d2 ON d1.USER_ID = d2.USER_ID
AND d1.USER_DATA_2 = d2.USER_DATA_2
AND (TRUNC(d2.TRANSACTION_TIMESTAMP) - TRUNC(d1.TRANSACTION_TIMESTAMP)) <= 31
AND (SUM(d1.TRANSACTION_AMOUNT) + SUM(d2.TRANSACTION_AMOUNT)) >= $100
AND ABS(((SUM(d2.TRANSACTION_AMOUNT) - SUM(d1.TRANSACTION_AMOUNT)) / ((SUM(d2.TRANSACTION_AMOUNT) + SUM(d1.TRANSACTION_AMOUNT)) / 2))*100) <= 10
GROUP BY 1,2,3;

查询将成功运行吗?

0 个答案:

没有答案