Use same derived table twice as virtual source for UNION queries, how? (1 Viewer)

mdlueck

Sr. Application Developer
Local time
Today, 17:02
Joined
Jun 23, 2011
Messages
2,631
Greetings,

I am thinking to further optimize a query I developed yesterday. Rather than have the UNION query have to harvest records from the real tables twice (as all records are being considered for both queries feeding the UNION) I would like to have the query create a derived table once and use the same derived table for both queries in the UNION.

Is anyone aware of a sample query that is similar to what I am envisioning?

The working query for now is as follows:

Code:
DECLARE @prodid int;
SET @prodid = 1;

SELECT [statuscode],[partnumber],[id],[title],[econumber]
FROM (SELECT 'BOM Not Complete' AS [statuscode],[p].[partnumber],[p].[id],[p].[title],
        CASE
          WHEN [p].[econumber] IS NULL THEN 'N/A'
          ELSE [p].[econumber]
        END AS [econumber]
      FROM [dbo].[products] AS [prod] 
      INNER JOIN [dbo].[productpartlink] AS [ppl] ON [prod].[id] = [ppl].[productid] 
      INNER JOIN [dbo].[parts] AS [p] ON [ppl].[partid] = [p].[id] 
      INNER JOIN [dbo].[partsstocktype] AS [ps] ON [p].[stocktypeid] = [ps].[id] 
      WHERE [prod].[id] = @prodid
      AND [ps].[sykassyflg] = 1
      AND [p].[bomloadedflg] = 0
      UNION
      SELECT 'Router Not Complete' AS [statuscode],[p].[partnumber],[p].[id],[p].[title],
        CASE
          WHEN [p].[econumber] IS NULL THEN 'N/A'
          ELSE [p].[econumber]
        END AS [econumber]
      FROM [dbo].[products] AS [prod] 
      INNER JOIN [dbo].[productpartlink] AS [ppl] ON [prod].[id] = [ppl].[productid] 
      INNER JOIN [dbo].[parts] AS [p] ON [ppl].[partid] = [p].[id] 
      INNER JOIN [dbo].[partsstocktype] AS [ps] ON [p].[stocktypeid] = [ps].[id] 
      WHERE [prod].[id] = @prodid
      AND [ps].[sykassyflg] = 1
      AND [p].[rtrcompflg] = 0)
AS [rpt]
ORDER BY [statuscode],[partnumber];
Which produces the following output:

Code:
statuscode    partnumber    id    title    econumber
BOM Not Complete    0715001021    2    L  BRACKET Test    21346587
BOM Not Complete    0715001134    3    BELLOWS Test    N/A
BOM Not Complete    0721660035    8    SECHRIST BELLOWS    N/A
Router Not Complete    0715001021    2    L  BRACKET Test    21346587
Router Not Complete    0715001134    3    BELLOWS Test    N/A
Router Not Complete    0716201107    7    PUMP LINK PIN (LONG) 12/09    12345
 

mdlueck

Sr. Application Developer
Local time
Today, 17:02
Joined
Jun 23, 2011
Messages
2,631
Might the VIEW function be an avenue to explore?

I just want to be sure that SQL Server would not query to populate the VIEW twice as that would defeat the further optimization I am endeavoring to obtain.

1) Build the result set once (Parts list for Product)
2) Query that result set twice for each SELECT being combined by UNION
 

mdlueck

Sr. Application Developer
Local time
Today, 17:02
Joined
Jun 23, 2011
Messages
2,631
I ended up using a Common Table Expression (CTE) do select out all of the parts records to be considered harvesting all columns which would be of interest, then used that CTE in both queries of the UNION query.
 

Users who are viewing this thread

Top Bottom