It depends on the expected result set.
You might have to put all other columns in your select list in a GROUP BY 1,3,4,5,6,7,8,9 or you might need a MAX(b.Effective_Dt) OVER (PARTITION BY whatever).
When i look at your query i see several possible improvements:
- There's a lot of DISTINCTs, i doubt they're actually neccessary. When the optimizer is not able to eliminate them during optimization (e.g. GROUP BY already resturns unique rows) it's a lot of overhead.
- The MAX(date) selects to find the latest row can be replace with a simple RANK/ROW_NUMBER:
Select Distinct
a.GROUP_CUSTOMER_ID
,Max(EXPIRATION_DT) ExpDat
From
PSOR_Temp.PRODUCER_X_GROUP a
Group by
1
) Grp
Inner Join
(
Select Distinct
a.GROUP_CUSTOMER_ID
,a.PRODUCER_ID
,a.EXPIRATION_DT ExpDat
From
PSOR_Temp.PRODUCER_X_GROUP a
with:
Select Distinct -- probably not needed
a.GROUP_CUSTOMER_ID
,a.PRODUCER_ID
,a.EXPIRATION_DT ExpDat
From
PSOR_Temp.PRODUCER_X_GROUP a
QUALIFY RANK()
OVER (PARTITION BY GROUP_CUSTOMER_ID
ORDER BY ExpDat DESC) = 1
Dieter
It depends on the expected result set.
You might have to put all other columns in your select list in a GROUP BY 1,3,4,5,6,7,8,9 or you might need a MAX(b.Effective_Dt) OVER (PARTITION BY whatever).
When i look at your query i see several possible improvements:
- There's a lot of DISTINCTs, i doubt they're actually neccessary. When the optimizer is not able to eliminate them during optimization (e.g. GROUP BY already resturns unique rows) it's a lot of overhead.
- The MAX(date) selects to find the latest row can be replace with a simple RANK/ROW_NUMBER:
with:
Dieter