kieriosity
IS-IT--Management
Does anyone know a better way to do the following query?
SELECT [CCM_DETAIL_1_ACT].[FNAME],
[CCM_DETAIL_1_ACT].[LNAME],
[CCM_DETAIL_1_ACT].[ORG],
[CCM_MASTER_1_ACT].[INPUT],
[CCM_MASTER_1_ACT].[DUE],
[CCM_MASTER_1_ACT].[FILENO],
[CCM_MASTER_1_ACT].[DOCKET],
[CCM_MASTER_1_ACT].[OFFICE],
[CCM_MASTER_1_ACT].[DOCTYPE],
[CCM_MASTER_1_ACT].[SYNOP],
[CCM_MASTER_1_ACT].[SIGN],
[CCM_ROUTING_1_ACT].[ASGNDATE],
[CCM_ROUTING_1_ACT].[GROUPNAME],
[CCM_ROUTING_1_ACT].[ASGNTO],
[CCM_ROUTING_1_ACT].[TASK],
[CCM_ROUTING_1_ACT].[ROUTID],
[CCM_MASTER_1_ACT].[CLOSED]
FROM (CCM_MASTER_1_ACT INNER JOIN CCM_ROUTING_1_ACT ON [CCM_MASTER_1_ACT].[MASTERID]=[CCM_ROUTING_1_ACT].[MASTERID]) INNER JOIN CCM_DETAIL_1_ACT ON [CCM_MASTER_1_ACT].[DETAILID]=[CCM_DETAIL_1_ACT].[DETAILID]
WHERE CCM_MASTER_1_ACT.DOCTYPE NOT IN ('WI','B','BR','V','VBI','VBL','VFR','VSI','VSL','VVM') AND CCM_ROUTING_1_ACT.ASGNTO Like 'rrs%' AND CCM_ROUTING_1_ACT.ROUTID IN (SELECT Max(CCM_ROUTING_1_ACT.ROUTID) AS MaxOfROUTID FROM CCM_ROUTING_1_ACT GROUP BY CCM_ROUTING_1_ACT.MASTERID) AND CCM_MASTER_1_ACT.CLOSED Is Null;
I'm trying to select the most recent routing for a record from the table, so I used a subselect to get the max RoutID for a grouped FK and matched it to the RoutID I want to show. Seems straight forward, but I've never tried anything like this before so I would like to make sure.
Thanks.
SELECT [CCM_DETAIL_1_ACT].[FNAME],
[CCM_DETAIL_1_ACT].[LNAME],
[CCM_DETAIL_1_ACT].[ORG],
[CCM_MASTER_1_ACT].[INPUT],
[CCM_MASTER_1_ACT].[DUE],
[CCM_MASTER_1_ACT].[FILENO],
[CCM_MASTER_1_ACT].[DOCKET],
[CCM_MASTER_1_ACT].[OFFICE],
[CCM_MASTER_1_ACT].[DOCTYPE],
[CCM_MASTER_1_ACT].[SYNOP],
[CCM_MASTER_1_ACT].[SIGN],
[CCM_ROUTING_1_ACT].[ASGNDATE],
[CCM_ROUTING_1_ACT].[GROUPNAME],
[CCM_ROUTING_1_ACT].[ASGNTO],
[CCM_ROUTING_1_ACT].[TASK],
[CCM_ROUTING_1_ACT].[ROUTID],
[CCM_MASTER_1_ACT].[CLOSED]
FROM (CCM_MASTER_1_ACT INNER JOIN CCM_ROUTING_1_ACT ON [CCM_MASTER_1_ACT].[MASTERID]=[CCM_ROUTING_1_ACT].[MASTERID]) INNER JOIN CCM_DETAIL_1_ACT ON [CCM_MASTER_1_ACT].[DETAILID]=[CCM_DETAIL_1_ACT].[DETAILID]
WHERE CCM_MASTER_1_ACT.DOCTYPE NOT IN ('WI','B','BR','V','VBI','VBL','VFR','VSI','VSL','VVM') AND CCM_ROUTING_1_ACT.ASGNTO Like 'rrs%' AND CCM_ROUTING_1_ACT.ROUTID IN (SELECT Max(CCM_ROUTING_1_ACT.ROUTID) AS MaxOfROUTID FROM CCM_ROUTING_1_ACT GROUP BY CCM_ROUTING_1_ACT.MASTERID) AND CCM_MASTER_1_ACT.CLOSED Is Null;
I'm trying to select the most recent routing for a record from the table, so I used a subselect to get the max RoutID for a grouped FK and matched it to the RoutID I want to show. Seems straight forward, but I've never tried anything like this before so I would like to make sure.
Thanks.