Skip to content
Advertisement

Optimizing Access Query with Multiple sub-queries

I have a table of data that contains baseline project information, called ADMIN_WORK. I have a separate table that contains various attributes related to each project listed in ADMIN_WORK called WR_ATTRIBUTE. The structure of WR_ATTRIBUTE is such that each project has about 300 attributes (300 rows in the table) each with a unique name associated with the project number. I need to pull approximately 15 of these attributes into a final table.

I built a query with a number of sub-queries, but it takes multiple hours to run. The code below represents pulling a single attribute (called Circuit) from WR_ATTRIBUTE for all projects.

SELECT ADMIN_WORK.WR_NO AS [WR Number], 
ADMIN_WORK.PREMISE_ID AS [Premise ID],
AttributeCircuit.Circuit

FROM ADMIN_WORK INNER JOIN 

(SELECT ADMIN_WORK.WR_NO AS [WR Number], WR_ATTRIBUTE.ATTRIBUTE_VALUE AS Circuit 

FROM ADMIN_WORK INNER JOIN 
WR_ATTRIBUTE ON ADMIN_WORK.WR_NO = WR_ATTRIBUTE.WR_NO 

WHERE (((WR_ATTRIBUTE.WR_ATTRIBUTE_CODE)="Circuit") AND ((ADMIN_WORK.WR_TYPE_CODE)="ACNEM"))

) AS AttributeCircuit 

ON ADMIN_WORK.WR_NO = [AttributeCircuit].[WR Number]);

My final query has about 14 more of these subqueries similarly implemented, each with a different WR_ATTRIBUTE_CODE.

My final table is generated accurately, but it is extremely slow. I am wondering if there is a better way of structuring my query.

Advertisement

Answer

You probably just want conditional aggregation:

SELECT w.WR_NO AS [WR Number], 
       w.PREMISE_ID AS [Premise ID],
       a.Circuit
FROM ADMIN_WORK as INNER JOIN 
     (SELECT a.WR_NO AS [WR Number], 
             MAX(IIF(a.WR_ATTRIBUTE_CODE)="Circuit" a.ATTRIBUTE_VALUE, NULL) AS Circuit ,
             . . . 
      FROM WR_ATTRIBUTE as a
      GROUP BY WR_NO
     ) as a
     ON w.WR_NO = a.WR_NO ;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement