在SQl中编写嵌套查询的更好方法

时间:2019-08-09 12:19:01

标签: sql oracle

我写了一段代码,我想知道是否有一种更优雅,更有效的编写方式。

SELECT act1.ACTIVITY_DETAIL_ID FROM CPMS.ACTIVITY_TRANSACTION act1
  WHERE act1.USER_CREATED ='CPMSAutoRecovery'
  AND act1.TRANSACTION_GROUP_ID =23    --commitment, not entitled
  GROUP BY  act1.ACTIVITY_DETAIL_ID HAVING COUNT(*)>1
  AND act1.ACTIVITY_DETAIL_ID IN (
    select ad.ACTIVITY_DETAIL_ID from CPMS.activity_detail ad, CPMS.PARTY_ROLE pr 
    where pr.ACTIVITY_REGISTRY_ID = ad.activity_registry_id 
    AND pr.PARTY_ROLE_type_ID=2 
    AND pr.PARTY_ID IN (
      select distinct i.PARTY_ID FROM CPMS.INDIVIDUAL i, CPMS.PARTY_ROLE pr where 
      I.PARTY_ID = PR.PARTY_ID AND i.EXPIRY_DATE is null
    )
  )
  order by act1.ACTIVITY_DETAIL_ID;

这是表的关键依赖性:

INDIVIDUAL
----------
PARTY_ID
PARTY_ROLE

------------
ACTIVITY_REGISTRY_ID 
PARTY_ID

ACTIVITY_DETAIL
-----------------
ACTIVITY_DETAIL_ID 
ACTIVITY_REGISTRY_ID

ACTIVITY_TRANSACTION
----------------------
ACTIVITY_TRANSACTION_ID 
ACTIVITY_DETAIL_ID

更新: 我加入了

SELECT act1.ACTIVITY_DETAIL_ID FROM CPMS.ACTIVITY_TRANSACTION act1
JOIN CPMS.ACTIVITY_DETAIL ad ON act1.ACTIVITY_DETAIL_ID = ad.ACTIVITY_DETAIL_ID
JOIN CPMS.PARTY_ROLE pr ON ad.ACTIVITY_REGISTRY_ID = pr.ACTIVITY_REGISTRY_ID
JOIN CPMS.INDIVIDUAL i ON pr.PARTY_ID = i.PARTY_ID
  WHERE act1.USER_CREATED ='CPMSAutoRecovery'
  AND act1.TRANSACTION_GROUP_ID =23
  AND pr.PARTY_ROLE_type_ID=2 
  AND i.EXPIRY_DATE is null
  GROUP BY  act1.ACTIVITY_DETAIL_ID HAVING COUNT(*)>1
order by act1.ACTIVITY_DETAIL_ID;

0 个答案:

没有答案