编辑:为了使我们更容易,我们有两个表来进行此查询。
1)Account_Cycle_Activity
2)Card_Account
我们将从1)表中分析的五个列是。
和从2)表中需要知道的列是
逻辑是:
在ACCOUNT_CYCLE_ACTIVITY表中找到一行ACTIVITY_TYPE_CODE ='007'或'011'或'021'和ACTIVITY_GROUP_CODE ='R12'且CYCLE_ACTIVITY_AMOUNT大于$ 25且CYCLE_ACTIVITY_COUNT介于1到900之间 - 这是第一个条款我的询问。
对于我们刚刚找到的行,其CYCLE_CTR列应该等于CARD_ACCOUNT表CYCLE_CTR减去1的CYCLE_CTR列值。 - 这是第二个子句
现在这是查询的EXIST部分 如果您找到这样的行,并且表中也没有相同的ACCOUNT_ID具有相同的ACTIVITY_TYPE_CODE和相同的ACTIVITY_GROUP_CODE和CYCLE_ACTIVITY_COUNT = 1且CYCLE_CTR位于第一行的CYCLE_CTR -6到第一行的CYCLE_CTR - 1之间的任何位置,报告帐户。
我提出了以下查询
SELECT *
FROM ACCOUNT_CYCLE_ACTIVITY aca
inner join card_account ca on ca.account_id = aca.ACCOUNT_ID
WHERE ACTIVITY_TYPE_CODE in('011', '021', '007')
and ACTIVITY_GROUP_CODE= 'R12'
and (CYCLE_ACTIVITY_AMOUNT) > 25
and CYCLE_ACTIVITY_COUNT =1
and aca.cycle_ctr = ca.cycle_ctr - 1
and NOT EXISTS
(SELECT aca2.ACCOUNT_ID
FROM ACCOUNT_CYCLE_ACTIVITY aca2
where ca.account_id = aca2.ACCOUNT_ID
and aca2.ACTIVITY_TYPE_CODE=aca.activity_type_code
and aca2.ACTIVITY_GROUP_CODE= aca.activity_group_code
and CYCLE_ACTIVITY_COUNT =1
and aca2.cycle_ctr between aca.cycle_ctr - 6 and aca.cycle_ctr - 1);
关于如何更有效地编写它以改善运行时间的任何想法?
答案 0 :(得分:0)
尝试左(外)联接
SELECT aca.*, ca.*
FROM card_account ca
inner join ACCOUNT_CYCLE_ACTIVITY aca
on aca.account_id = ca.ACCOUNT_ID
and aca.ACTIVITY_TYPE_CODE in ('011', '021', '007')
and aca.ACTIVITY_GROUP_CODE = 'R12'
and aca.CYCLE_ACTIVITY_COUNT = 1
and aca.cycle_ctr = ca.cycle_ctr - 1
and aca.CYCLE_ACTIVITY_AMOUNT > 25
left join ACCOUNT_CYCLE_ACTIVITY aca2
on aca2.account_id = aca.ACCOUNT_ID
and aca2.ACTIVITY_TYPE_CODE = aca.activity_type_code
and aca2.ACTIVITY_GROUP_CODE = aca.activity_group_code
and aca2.CYCLE_ACTIVITY_COUNT = aca.CYCLE_ACTIVITY_COUNT
and aca2.cycle_ctr between aca.cycle_ctr - 6 and aca.cycle_ctr - 1
where aca2.ACCOUNT_ID is null