[Solved] Tune Slow SQL Query

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  2. Avoid Subqueries (query line: 155): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 248): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  4. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  5. Use UNION ALL instead of UNION (query line: 169): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
CREATE INDEX gl10001_idx_bachnumb ON DBServer..GL10001 (BACHNUMB);
CREATE INDEX pc10000_idx_zsalesd_zprojec_ztaskid_ztransa ON DBServer.dbo.pc10000 (zSalesDocumentNumber,zProjectID,zTaskID,zTransactionNumber);
CREATE INDEX pc10000_idx_zdistdo_zprojec_ztaskid_ztransa ON DBServer.dbo.pc10000 (zDistDocumentNumber,zProjectID,zTaskID,zTransactionNumber);
The optimized query:
SELECT
        TOP 25 dbserver_dbo_pc10000_zprojectid,
        dbserver_dbo_pc10000_ztaskid,
        dbserver_dbo_pc10000_ztransactionnumber,
        dbserver_dbo_pc10000_ztransactiondate,
        dbserver_dbo_pc10000_zuserid,
        dbserver_dbo_pc10000_zcostcategoryddl,
        dbserver_dbo_pc10000_zcostcategorystring,
        dbserver_dbo_pc10000_zsubcostcategory,
        dbserver_dbo_pc10000_zsubcostcategorystring,
        dbserver_dbo_pc10000_zdepartmentid,
        dbserver_dbo_pc10000_zjournalentry,
        dbserver_dbo_pc10000_zpostingdate,
        dbserver_dbo_pc10000_zsalespostingdate,
        dbserver_dbo_pc10000_zperiodnumber,
        dbserver_dbo_pc10000_ztransactiondescriptio,
        dbserver_dbo_pc10000_zbillingdescriptionlin,
        dbserver_dbo_pc10000_zbillingdescriptionlin,
        dbserver_dbo_pc10000_zbillingdescriptionlin,
        dbserver_dbo_pc10000_zbillingdescriptionlin,
        dbserver_dbo_pc10000_zsalesaccountindex,
        dbserver_dbo_pc10000_zsalesaccountstring,
        dbserver_dbo_pc10000_zdistdocumenttypeddl,
        dbserver_dbo_pc10000_zdistdocumentnumber,
        dbserver_dbo_pc10000_zdistsequencenumber,
        dbserver_dbo_pc10000_zsalesdocumenttypeddl,
        dbserver_dbo_pc10000_zsalesdocumentnumber,
        dbserver_dbo_pc10000_zsaleslinenumber,
        dbserver_dbo_pc10000_zdisthistoryyear,
        dbserver_dbo_pc10000_zseriesddl,
        dbserver_dbo_pc10000_zsourcedoc,
        dbserver_dbo_pc10000_zwebsource,
        dbserver_dbo_pc10000_zorigdocumentnumber,
        dbserver_dbo_pc10000_zorigdocumentdate,
        dbserver_dbo_pc10000_zorigid,
        dbserver_dbo_pc10000_zorigname,
        dbserver_dbo_pc10000_zexpensestatusddl,
        dbserver_dbo_pc10000_zapprovaluseridcost,
        dbserver_dbo_pc10000_zaccountindex,
        dbserver_dbo_pc10000_zaccountnumberstring,
        dbserver_dbo_pc10000_zbillingstatusddl,
        dbserver_dbo_pc10000_zapprovaluseridbilling,
        dbserver_dbo_pc10000_zbillingworkqty,
        dbserver_dbo_pc10000_zbillingworkamt,
        dbserver_dbo_pc10000_zqty,
        dbserver_dbo_pc10000_zqtybilled,
        dbserver_dbo_pc10000_zunitcost,
        dbserver_dbo_pc10000_zunitprice,
        dbserver_dbo_pc10000_zrevenueamt,
        dbserver_dbo_pc10000_zoriginatingrevenueamt,
        dbserver_dbo_pc10000_zcostamtentered,
        dbserver_dbo_pc10000_zcostamt,
        dbserver_dbo_pc10000_zoriginatingcostamt,
        dbserver_dbo_pc10000_zpaygroupid,
        dbserver_dbo_pc10000_zpayrollstatusddl,
        dbserver_dbo_pc10000_ztotaltimestatusddl,
        dbserver_dbo_pc10000_zemployeeid,
        dbserver_dbo_pc10000_zhoursentered,
        dbserver_dbo_pc10000_zhourspaid,
        dbserver_dbo_pc10000_zpayrecord,
        dbserver_dbo_pc10000_zitemid,
        dbserver_dbo_pc10000_zitemdescription,
        dbserver_dbo_pc10000_zuofm,
        dbserver_dbo_pc10000_zitemqty,
        dbserver_dbo_pc10000_zburdenstatusddl,
        dbserver_dbo_pc10000_zuserdefineddate,
        dbserver_dbo_pc10000_zuserdefineddate2,
        dbserver_dbo_pc10000_zuserdefinedstring,
        dbserver_dbo_pc10000_zuserdefinedstring2,
        dbserver_dbo_pc10000_zuserdefinedcurrency,
        dbserver_dbo_pc10000_zuserdefinedcurrency2,
        dbserver_dbo_pc10000_znoteindex,
        dbserver_dbo_pc10000_zimporttype,
        dbserver_dbo_pc10000_dex_row_id 
    FROM
        ((SELECT
            TOP 25 DBServer.dbo.pc10000.zProjectID AS dbserver_dbo_pc10000_zprojectid,
            DBServer.dbo.pc10000.zTaskID AS dbserver_dbo_pc10000_ztaskid,
            DBServer.dbo.pc10000.zTransactionNumber AS dbserver_dbo_pc10000_ztransactionnumber,
            DBServer.dbo.pc10000.zTransactionDate AS dbserver_dbo_pc10000_ztransactiondate,
            DBServer.dbo.pc10000.zUserID AS dbserver_dbo_pc10000_zuserid,
            DBServer.dbo.pc10000.zCostCategoryDDL AS dbserver_dbo_pc10000_zcostcategoryddl,
            DBServer.dbo.pc10000.zCostCategoryString AS dbserver_dbo_pc10000_zcostcategorystring,
            DBServer.dbo.pc10000.zSubCostCategory AS dbserver_dbo_pc10000_zsubcostcategory,
            DBServer.dbo.pc10000.zSubCostCategoryString AS dbserver_dbo_pc10000_zsubcostcategorystring,
            DBServer.dbo.pc10000.zDepartmentID AS dbserver_dbo_pc10000_zdepartmentid,
            DBServer.dbo.pc10000.zJournalEntry AS dbserver_dbo_pc10000_zjournalentry,
            DBServer.dbo.pc10000.zPostingDate AS dbserver_dbo_pc10000_zpostingdate,
            DBServer.dbo.pc10000.zSalesPostingDate AS dbserver_dbo_pc10000_zsalespostingdate,
            DBServer.dbo.pc10000.zPeriodNumber AS dbserver_dbo_pc10000_zperiodnumber,
            DBServer.dbo.pc10000.zTransactionDescription AS dbserver_dbo_pc10000_ztransactiondescriptio,
            DBServer.dbo.pc10000.zBillingDescriptionLine1 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
            DBServer.dbo.pc10000.zBillingDescriptionLine2 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
            DBServer.dbo.pc10000.zBillingDescriptionLine3 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
            DBServer.dbo.pc10000.zBillingDescriptionLine4 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
            DBServer.dbo.pc10000.zSalesAccountIndex AS dbserver_dbo_pc10000_zsalesaccountindex,
            DBServer.dbo.pc10000.zSalesAccountString AS dbserver_dbo_pc10000_zsalesaccountstring,
            DBServer.dbo.pc10000.zDistDocumentTypeDDL AS dbserver_dbo_pc10000_zdistdocumenttypeddl,
            DBServer.dbo.pc10000.zDistDocumentNumber AS dbserver_dbo_pc10000_zdistdocumentnumber,
            DBServer.dbo.pc10000.zDistSequenceNumber AS dbserver_dbo_pc10000_zdistsequencenumber,
            DBServer.dbo.pc10000.zSalesDocumentTypeDDL AS dbserver_dbo_pc10000_zsalesdocumenttypeddl,
            DBServer.dbo.pc10000.zSalesDocumentNumber AS dbserver_dbo_pc10000_zsalesdocumentnumber,
            DBServer.dbo.pc10000.zSalesLineNumber AS dbserver_dbo_pc10000_zsaleslinenumber,
            DBServer.dbo.pc10000.zDistHistoryYear AS dbserver_dbo_pc10000_zdisthistoryyear,
            DBServer.dbo.pc10000.zSeriesDDL AS dbserver_dbo_pc10000_zseriesddl,
            DBServer.dbo.pc10000.zSourceDoc AS dbserver_dbo_pc10000_zsourcedoc,
            DBServer.dbo.pc10000.zWebSource AS dbserver_dbo_pc10000_zwebsource,
            DBServer.dbo.pc10000.zOrigDocumentNumber AS dbserver_dbo_pc10000_zorigdocumentnumber,
            DBServer.dbo.pc10000.zOrigDocumentDate AS dbserver_dbo_pc10000_zorigdocumentdate,
            DBServer.dbo.pc10000.zOrigID AS dbserver_dbo_pc10000_zorigid,
            DBServer.dbo.pc10000.zOrigName AS dbserver_dbo_pc10000_zorigname,
            DBServer.dbo.pc10000.zExpenseStatusDDL AS dbserver_dbo_pc10000_zexpensestatusddl,
            DBServer.dbo.pc10000.zApprovalUserIDCost AS dbserver_dbo_pc10000_zapprovaluseridcost,
            DBServer.dbo.pc10000.zAccountIndex AS dbserver_dbo_pc10000_zaccountindex,
            DBServer.dbo.pc10000.zAccountNumberString AS dbserver_dbo_pc10000_zaccountnumberstring,
            DBServer.dbo.pc10000.zBillingStatusDDL AS dbserver_dbo_pc10000_zbillingstatusddl,
            DBServer.dbo.pc10000.zApprovalUserIDBilling AS dbserver_dbo_pc10000_zapprovaluseridbilling,
            DBServer.dbo.pc10000.zBillingWorkQty AS dbserver_dbo_pc10000_zbillingworkqty,
            DBServer.dbo.pc10000.zBillingWorkAmt AS dbserver_dbo_pc10000_zbillingworkamt,
            DBServer.dbo.pc10000.zQty AS dbserver_dbo_pc10000_zqty,
            DBServer.dbo.pc10000.zQtyBilled AS dbserver_dbo_pc10000_zqtybilled,
            DBServer.dbo.pc10000.zUnitCost AS dbserver_dbo_pc10000_zunitcost,
            DBServer.dbo.pc10000.zUnitPrice AS dbserver_dbo_pc10000_zunitprice,
            DBServer.dbo.pc10000.zRevenueAmt AS dbserver_dbo_pc10000_zrevenueamt,
            DBServer.dbo.pc10000.zOriginatingRevenueAmt AS dbserver_dbo_pc10000_zoriginatingrevenueamt,
            DBServer.dbo.pc10000.zCostAmtEntered AS dbserver_dbo_pc10000_zcostamtentered,
            DBServer.dbo.pc10000.zCostAmt AS dbserver_dbo_pc10000_zcostamt,
            DBServer.dbo.pc10000.zOriginatingCostAmt AS dbserver_dbo_pc10000_zoriginatingcostamt,
            DBServer.dbo.pc10000.zPayGroupID AS dbserver_dbo_pc10000_zpaygroupid,
            DBServer.dbo.pc10000.zPayrollStatusDDL AS dbserver_dbo_pc10000_zpayrollstatusddl,
            DBServer.dbo.pc10000.zTotalTimeStatusDDL AS dbserver_dbo_pc10000_ztotaltimestatusddl,
            DBServer.dbo.pc10000.zEmployeeID AS dbserver_dbo_pc10000_zemployeeid,
            DBServer.dbo.pc10000.zHoursEntered AS dbserver_dbo_pc10000_zhoursentered,
            DBServer.dbo.pc10000.zHoursPaid AS dbserver_dbo_pc10000_zhourspaid,
            DBServer.dbo.pc10000.zPayRecord AS dbserver_dbo_pc10000_zpayrecord,
            DBServer.dbo.pc10000.zItemID AS dbserver_dbo_pc10000_zitemid,
            DBServer.dbo.pc10000.zItemDescription AS dbserver_dbo_pc10000_zitemdescription,
            DBServer.dbo.pc10000.zUofM AS dbserver_dbo_pc10000_zuofm,
            DBServer.dbo.pc10000.zItemQty AS dbserver_dbo_pc10000_zitemqty,
            DBServer.dbo.pc10000.zBurdenStatusDDL AS dbserver_dbo_pc10000_zburdenstatusddl,
            DBServer.dbo.pc10000.zUserDefinedDate AS dbserver_dbo_pc10000_zuserdefineddate,
            DBServer.dbo.pc10000.zUserDefinedDate2 AS dbserver_dbo_pc10000_zuserdefineddate2,
            DBServer.dbo.pc10000.zUserDefinedString AS dbserver_dbo_pc10000_zuserdefinedstring,
            DBServer.dbo.pc10000.zUserDefinedString2 AS dbserver_dbo_pc10000_zuserdefinedstring2,
            DBServer.dbo.pc10000.zUserDefinedCurrency AS dbserver_dbo_pc10000_zuserdefinedcurrency,
            DBServer.dbo.pc10000.zUserDefinedCurrency2 AS dbserver_dbo_pc10000_zuserdefinedcurrency2,
            DBServer.dbo.pc10000.zNoteIndex AS dbserver_dbo_pc10000_znoteindex,
            DBServer.dbo.pc10000.zImportType AS dbserver_dbo_pc10000_zimporttype,
            DBServer.dbo.pc10000.DEX_ROW_ID AS dbserver_dbo_pc10000_dex_row_id 
        FROM
            DBServer.dbo.pc10000 
        WHERE
            (
                DBServer.dbo.pc10000.zSalesDocumentNumber IN (
                    SELECT
                        CAST(DBServer..GL10001.JRNENTRY AS varchar (20)) 
                    FROM
                        DBServer..GL10001 
                    WHERE
                        DBServer..GL10001.BACHNUMB = 'PMCHK00004283'
                )
            ) 
        ORDER BY
            DBServer.dbo.pc10000.zProjectID ASC,
            DBServer.dbo.pc10000.zTaskID ASC,
            DBServer.dbo.pc10000.zTransactionNumber ASC) 
        UNION
        (
            SELECT
                TOP 25 DBServer.dbo.pc10000.zProjectID AS dbserver_dbo_pc10000_zprojectid,
                DBServer.dbo.pc10000.zTaskID AS dbserver_dbo_pc10000_ztaskid,
                DBServer.dbo.pc10000.zTransactionNumber AS dbserver_dbo_pc10000_ztransactionnumber,
                DBServer.dbo.pc10000.zTransactionDate AS dbserver_dbo_pc10000_ztransactiondate,
                DBServer.dbo.pc10000.zUserID AS dbserver_dbo_pc10000_zuserid,
                DBServer.dbo.pc10000.zCostCategoryDDL AS dbserver_dbo_pc10000_zcostcategoryddl,
                DBServer.dbo.pc10000.zCostCategoryString AS dbserver_dbo_pc10000_zcostcategorystring,
                DBServer.dbo.pc10000.zSubCostCategory AS dbserver_dbo_pc10000_zsubcostcategory,
                DBServer.dbo.pc10000.zSubCostCategoryString AS dbserver_dbo_pc10000_zsubcostcategorystring,
                DBServer.dbo.pc10000.zDepartmentID AS dbserver_dbo_pc10000_zdepartmentid,
                DBServer.dbo.pc10000.zJournalEntry AS dbserver_dbo_pc10000_zjournalentry,
                DBServer.dbo.pc10000.zPostingDate AS dbserver_dbo_pc10000_zpostingdate,
                DBServer.dbo.pc10000.zSalesPostingDate AS dbserver_dbo_pc10000_zsalespostingdate,
                DBServer.dbo.pc10000.zPeriodNumber AS dbserver_dbo_pc10000_zperiodnumber,
                DBServer.dbo.pc10000.zTransactionDescription AS dbserver_dbo_pc10000_ztransactiondescriptio,
                DBServer.dbo.pc10000.zBillingDescriptionLine1 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
                DBServer.dbo.pc10000.zBillingDescriptionLine2 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
                DBServer.dbo.pc10000.zBillingDescriptionLine3 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
                DBServer.dbo.pc10000.zBillingDescriptionLine4 AS dbserver_dbo_pc10000_zbillingdescriptionlin,
                DBServer.dbo.pc10000.zSalesAccountIndex AS dbserver_dbo_pc10000_zsalesaccountindex,
                DBServer.dbo.pc10000.zSalesAccountString AS dbserver_dbo_pc10000_zsalesaccountstring,
                DBServer.dbo.pc10000.zDistDocumentTypeDDL AS dbserver_dbo_pc10000_zdistdocumenttypeddl,
                DBServer.dbo.pc10000.zDistDocumentNumber AS dbserver_dbo_pc10000_zdistdocumentnumber,
                DBServer.dbo.pc10000.zDistSequenceNumber AS dbserver_dbo_pc10000_zdistsequencenumber,
                DBServer.dbo.pc10000.zSalesDocumentTypeDDL AS dbserver_dbo_pc10000_zsalesdocumenttypeddl,
                DBServer.dbo.pc10000.zSalesDocumentNumber AS dbserver_dbo_pc10000_zsalesdocumentnumber,
                DBServer.dbo.pc10000.zSalesLineNumber AS dbserver_dbo_pc10000_zsaleslinenumber,
                DBServer.dbo.pc10000.zDistHistoryYear AS dbserver_dbo_pc10000_zdisthistoryyear,
                DBServer.dbo.pc10000.zSeriesDDL AS dbserver_dbo_pc10000_zseriesddl,
                DBServer.dbo.pc10000.zSourceDoc AS dbserver_dbo_pc10000_zsourcedoc,
                DBServer.dbo.pc10000.zWebSource AS dbserver_dbo_pc10000_zwebsource,
                DBServer.dbo.pc10000.zOrigDocumentNumber AS dbserver_dbo_pc10000_zorigdocumentnumber,
                DBServer.dbo.pc10000.zOrigDocumentDate AS dbserver_dbo_pc10000_zorigdocumentdate,
                DBServer.dbo.pc10000.zOrigID AS dbserver_dbo_pc10000_zorigid,
                DBServer.dbo.pc10000.zOrigName AS dbserver_dbo_pc10000_zorigname,
                DBServer.dbo.pc10000.zExpenseStatusDDL AS dbserver_dbo_pc10000_zexpensestatusddl,
                DBServer.dbo.pc10000.zApprovalUserIDCost AS dbserver_dbo_pc10000_zapprovaluseridcost,
                DBServer.dbo.pc10000.zAccountIndex AS dbserver_dbo_pc10000_zaccountindex,
                DBServer.dbo.pc10000.zAccountNumberString AS dbserver_dbo_pc10000_zaccountnumberstring,
                DBServer.dbo.pc10000.zBillingStatusDDL AS dbserver_dbo_pc10000_zbillingstatusddl,
                DBServer.dbo.pc10000.zApprovalUserIDBilling AS dbserver_dbo_pc10000_zapprovaluseridbilling,
                DBServer.dbo.pc10000.zBillingWorkQty AS dbserver_dbo_pc10000_zbillingworkqty,
                DBServer.dbo.pc10000.zBillingWorkAmt AS dbserver_dbo_pc10000_zbillingworkamt,
                DBServer.dbo.pc10000.zQty AS dbserver_dbo_pc10000_zqty,
                DBServer.dbo.pc10000.zQtyBilled AS dbserver_dbo_pc10000_zqtybilled,
                DBServer.dbo.pc10000.zUnitCost AS dbserver_dbo_pc10000_zunitcost,
                DBServer.dbo.pc10000.zUnitPrice AS dbserver_dbo_pc10000_zunitprice,
                DBServer.dbo.pc10000.zRevenueAmt AS dbserver_dbo_pc10000_zrevenueamt,
                DBServer.dbo.pc10000.zOriginatingRevenueAmt AS dbserver_dbo_pc10000_zoriginatingrevenueamt,
                DBServer.dbo.pc10000.zCostAmtEntered AS dbserver_dbo_pc10000_zcostamtentered,
                DBServer.dbo.pc10000.zCostAmt AS dbserver_dbo_pc10000_zcostamt,
                DBServer.dbo.pc10000.zOriginatingCostAmt AS dbserver_dbo_pc10000_zoriginatingcostamt,
                DBServer.dbo.pc10000.zPayGroupID AS dbserver_dbo_pc10000_zpaygroupid,
                DBServer.dbo.pc10000.zPayrollStatusDDL AS dbserver_dbo_pc10000_zpayrollstatusddl,
                DBServer.dbo.pc10000.zTotalTimeStatusDDL AS dbserver_dbo_pc10000_ztotaltimestatusddl,
                DBServer.dbo.pc10000.zEmployeeID AS dbserver_dbo_pc10000_zemployeeid,
                DBServer.dbo.pc10000.zHoursEntered AS dbserver_dbo_pc10000_zhoursentered,
                DBServer.dbo.pc10000.zHoursPaid AS dbserver_dbo_pc10000_zhourspaid,
                DBServer.dbo.pc10000.zPayRecord AS dbserver_dbo_pc10000_zpayrecord,
                DBServer.dbo.pc10000.zItemID AS dbserver_dbo_pc10000_zitemid,
                DBServer.dbo.pc10000.zItemDescription AS dbserver_dbo_pc10000_zitemdescription,
                DBServer.dbo.pc10000.zUofM AS dbserver_dbo_pc10000_zuofm,
                DBServer.dbo.pc10000.zItemQty AS dbserver_dbo_pc10000_zitemqty,
                DBServer.dbo.pc10000.zBurdenStatusDDL AS dbserver_dbo_pc10000_zburdenstatusddl,
                DBServer.dbo.pc10000.zUserDefinedDate AS dbserver_dbo_pc10000_zuserdefineddate,
                DBServer.dbo.pc10000.zUserDefinedDate2 AS dbserver_dbo_pc10000_zuserdefineddate2,
                DBServer.dbo.pc10000.zUserDefinedString AS dbserver_dbo_pc10000_zuserdefinedstring,
                DBServer.dbo.pc10000.zUserDefinedString2 AS dbserver_dbo_pc10000_zuserdefinedstring2,
                DBServer.dbo.pc10000.zUserDefinedCurrency AS dbserver_dbo_pc10000_zuserdefinedcurrency,
                DBServer.dbo.pc10000.zUserDefinedCurrency2 AS dbserver_dbo_pc10000_zuserdefinedcurrency2,
                DBServer.dbo.pc10000.zNoteIndex AS dbserver_dbo_pc10000_znoteindex,
                DBServer.dbo.pc10000.zImportType AS dbserver_dbo_pc10000_zimporttype,
                DBServer.dbo.pc10000.DEX_ROW_ID AS dbserver_dbo_pc10000_dex_row_id 
            FROM
                DBServer.dbo.pc10000 
            WHERE
                (
                    DBServer.dbo.pc10000.zDistDocumentNumber IN (
                        SELECT
                            CAST(DBServer..GL10001.JRNENTRY AS varchar (20)) 
                        FROM
                            DBServer..GL10001 
                        WHERE
                            DBServer..GL10001.BACHNUMB = 'PMCHK00004283'
                    )
                ) 
            ORDER BY
                DBServer.dbo.pc10000.zProjectID ASC,
                DBServer.dbo.pc10000.zTaskID ASC,
                DBServer.dbo.pc10000.zTransactionNumber ASC)
        ) AS union1 
    ORDER BY
        union1.dbserver_dbo_pc10000_zprojectid ASC,
        union1.dbserver_dbo_pc10000_ztaskid ASC,
        union1.dbserver_dbo_pc10000_ztransactionnumber ASC

Related Articles



* original question posted on StackOverflow here.