Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:46:50+00:00 2026-05-13T15:46:50+00:00

This query originally came from a VB6 program accessing MS Access tables which are

  • 0

This query originally came from a VB6 program accessing MS Access tables which are linked to external databases through ODBC. It takes about 3:30 to run.

Now I’ve setup a SQL Server 2008 Express box to evaluate how we can migrate to a better database system. So I setup a linked server to the external server (we call it DWPROD) and when I converted the query (changed iif statements to case) and run it, I let it run for 12 minutes and it still doesn’t finish. I’m new to SQL Server, so what can I look at to see what is taking so long? Any suggestions for speeding this up? Any resources recommended so I can learn more about this? This is a simple query compared to what we have so I’ll hit this problem again.

Query:

SELECT [FISCAL_YEAR] AS FISCAL_YEAR, 
[Budgets].[dbo].[Departments].strdepentity AS ENTITY_CODE,
[Budgets].[dbo].[Departments].depdiv1 AS DIVISION_CODE, 
FINANCIAL_DEPTID AS DEPTID, 
FUND_CODE, 
[CLASS_FLD] AS CLASS_CODE, 
[PROJECT_ID], 
[PROGRAM_CODE], 
[ACCOUNT] AS ACCOUNT_CODE, 
CASE
    WHEN [ACCOUNT] in ('500020','520000','520220','520240') THEN 2
    WHEN LEFT([ACCOUNT],1)='5' THEN 1
    WHEN Left([ACCOUNT],1)='6' THEN 3
    WHEN Left([ACCOUNT],1)='7' THEN 4
    WHEN Left([ACCOUNT],1)='8' THEN 5
    ELSE 0
    END AS ACCT_GRP_CODE,
Sum([BUDGET_AMOUNT]) AS GL_BUD_AMT, 
Sum([ENCUMBRANCE_AMOUNT]) AS GL_ENC_AMT, 
Sum([EXPENDITURE_AMOUNT]) AS GL_EXP_AMT, 
CASE
    WHEN Left([CLASS_FLD],2)='12' THEN 0
    WHEN Left([CLASS_FLD],3)='113' THEN 3
    WHEN Left([CLASS_FLD],3)='112' THEN 14
    WHEN Left([CLASS_FLD],3)='115' THEN 10
    WHEN Left([CLASS_FLD],3)='116' THEN 13
    WHEN Left([CLASS_FLD],3)='117' THEN 12
    WHEN Left([CLASS_FLD],3)='118' THEN 11
    WHEN Left([CLASS_FLD],2)='13' THEN 2
    WHEN Left([CLASS_FLD],2)='14' THEN 3
    WHEN Left([CLASS_FLD],1)='4' THEN 4
    WHEN Left([CLASS_FLD],1)='6' THEN 6
    ELSE 9
    END AS FUND_SOURCE 

FROM [DWPROD]..[DISC].[PS_LEDGER_DETAIL] LEFT JOIN [Budgets].[dbo].[Departments] ON    FINANCIAL_DEPTID=[Budgets].[dbo].[Departments].deporg 
WHERE ((([BUDGET_PERIOD])='2010') And (([ACCOUNTING_PERIOD]) Between 1 And 12)) 


GROUP BY 
[FISCAL_YEAR],
[Budgets].[dbo].[Departments].strdepentity, [Budgets].[dbo].[Departments].depdiv1, 
[FINANCIAL_DEPTID],
FUND_CODE, 
[CLASS_FLD], 
[PROJECT_ID], 
[PROGRAM_CODE],
[ACCOUNT], 
CASE
    WHEN [ACCOUNT] in ('500020','520000','520220','520240') THEN 2
    WHEN LEFT([ACCOUNT],1)='5' THEN 1
    WHEN Left([ACCOUNT],1)='6' THEN 3
    WHEN Left([ACCOUNT],1)='7' THEN 4
    WHEN Left([ACCOUNT],1)='8' THEN 5
    ELSE 0
    END,
    CASE
    WHEN Left([CLASS_FLD],2)='12' THEN 0
    WHEN Left([CLASS_FLD],3)='113' THEN 3
    WHEN Left([CLASS_FLD],3)='112' THEN 14
    WHEN Left([CLASS_FLD],3)='115' THEN 10
    WHEN Left([CLASS_FLD],3)='116' THEN 13
    WHEN Left([CLASS_FLD],3)='117' THEN 12
    WHEN Left([CLASS_FLD],3)='118' THEN 11
    WHEN Left([CLASS_FLD],2)='13' THEN 2
    WHEN Left([CLASS_FLD],2)='14' THEN 3
    WHEN Left([CLASS_FLD],1)='4' THEN 4
    WHEN Left([CLASS_FLD],1)='6' THEN 6
    ELSE 9
    END
HAVING (((FISCAL_YEAR)=2010) 
AND ((FINANCIAL_DEPTID) Between '100' And '999') 
AND ((ACCOUNT) Between '500000' And '899999')) 
ORDER BY [FINANCIAL_DEPTID], [CLASS_FLD], [PROJECT_ID], [ACCOUNT]

I know it’s long. Thanks for looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:46:50+00:00Added an answer on May 13, 2026 at 3:46 pm

    I re-wrote your query:

    WITH ledger_detail AS (
          SELECT pld.fiscal_year,
                 pld.financial_deptid AS DEPTID, 
                 pld.fund_code,
                 pld.class_fld AS CLASS_CODE, 
                 pld.project_id,
                 pld.program_code,
                 CASE
                   WHEN pld.account IN ('500020','520000','520220','520240') THEN 2
                   WHEN LEFT(pld.account,1) = '5' THEN 1
                   WHEN LEFT(pld.account,1) = '6' THEN 3
                   WHEN LEFT(pld.account,1) = '7' THEN 4
                   WHEN LEFT(pld.account,1) = '8' THEN 5
                   ELSE 0
                 END AS ACCT_GRP_CODE,
                 pld.budget_amount,
                 pld.encumbrance_amount,
                 pld.expenditure_amount,
                 pld.account AS ACCOUNT_CODE, 
                 CASE
                   WHEN LEFT(pld.class_fld, 2) ='12' THEN 0
                   WHEN LEFT(pld.class_fld, 3)='113' THEN 3
                   WHEN LEFT(pld.class_fld, 3)='112' THEN 14
                   WHEN LEFT(pld.class_fld, 3)='115' THEN 10
                   WHEN LEFT(pld.class_fld, 3)='116' THEN 13
                   WHEN LEFT(pld.class_fld, 3)='117' THEN 12
                   WHEN LEFT(pld.class_fld, 3)='118' THEN 11
                   WHEN LEFT(pld.class_fld, 2)='13' THEN 2
                   WHEN LEFT(pld.class_fld, 2)='14' THEN 3
                   WHEN LEFT(pld.class_fld, 1)='4' THEN 4
                   WHEN LEFT(pld.class_fld, 1)='6' THEN 6
                   ELSE 9
                 END AS FUND_SOURCE 
            FROM [DWPROD]..[DISC].[PS_LEDGER_DETAIL] pld
           WHERE pld.budget_period = '2010' 
             AND pld.accounting_period BETWEEN 1 AND 12
             AND pld.fiscal_year = 2010
             AND pld.financial_deptid BETWEEN '100' AND '999'
             AND pld.account BETWEEN '500000' AND '899999')
       SELECT x.fiscal_year,
              y.strdepentity AS ENTITY_CODE,
              y.depdiv1 AS DIVISION_CODE,
              x.deptid, 
              x.fund_code,
              x.class_code,
              x.project_id,
              x.program_code,
              x.acct_grp_code,
              SUM(x.budget_amount) AS GL_BUD_AMT, 
              SUM(x.encumbrance_amount) AS GL_ENC_AMT, 
              SUM(x.expenditure_amount) AS GL_EXP_AMT,
              x.account AS ACCOUNT_CODE, 
              x.fund_source
         FROM ledger_detail x
    LEFT JOIN [Budgets].[dbo].[Departments] y ON y.deporg = x.financial_deptid    
     GROUP BY x.fiscal_year, y.strdepentity, y.depdiv1, x.deptid, x.fund_code, x.class_code, x.project_id, x.program_code, x.acct_grp_code
     ORDER BY x.financial_deptid, x.class_fld, x.project_id, x.account
    

    Even if you have indexes on [DWPROD]..[DISC].[PS_LEDGER_DETAIL]’s account and class_fld, the use of a function (LEFT) on them renders them un-usable for this query.

    Also, you’re including filter criteria in the HAVING clause rather than the WHERE clause.

    UPDATE: Non-CTE Equivalent

       SELECT x.fiscal_year,
              y.strdepentity AS ENTITY_CODE,
              y.depdiv1 AS DIVISION_CODE,
              x.deptid, 
              x.fund_code,
              x.class_code,
              x.project_id,
              x.program_code,
              x.acct_grp_code,
              SUM(x.budget_amount) AS GL_BUD_AMT, 
              SUM(x.encumbrance_amount) AS GL_ENC_AMT, 
              SUM(x.expenditure_amount) AS GL_EXP_AMT,
              x.account AS ACCOUNT_CODE, 
              x.fund_source
         FROM (SELECT pld.fiscal_year,
                      pld.financial_deptid AS DEPTID, 
                      pld.fund_code,
                      pld.class_fld AS CLASS_CODE, 
                      pld.project_id,
                      pld.program_code,
                      CASE
                        WHEN pld.account IN ('500020','520000','520220','520240') THEN 2
                        WHEN LEFT(pld.account,1) = '5' THEN 1
                        WHEN LEFT(pld.account,1) = '6' THEN 3
                        WHEN LEFT(pld.account,1) = '7' THEN 4
                        WHEN LEFT(pld.account,1) = '8' THEN 5
                        ELSE 0
                      END AS ACCT_GRP_CODE,
                      pld.budget_amount,
                      pld.encumbrance_amount,
                      pld.expenditure_amount,
                      pld.account AS ACCOUNT_CODE, 
                      CASE
                        WHEN LEFT(pld.class_fld, 2) ='12' THEN 0
                        WHEN LEFT(pld.class_fld, 3)='113' THEN 3
                        WHEN LEFT(pld.class_fld, 3)='112' THEN 14
                        WHEN LEFT(pld.class_fld, 3)='115' THEN 10
                        WHEN LEFT(pld.class_fld, 3)='116' THEN 13
                        WHEN LEFT(pld.class_fld, 3)='117' THEN 12
                        WHEN LEFT(pld.class_fld, 3)='118' THEN 11
                        WHEN LEFT(pld.class_fld, 2)='13' THEN 2
                        WHEN LEFT(pld.class_fld, 2)='14' THEN 3
                        WHEN LEFT(pld.class_fld, 1)='4' THEN 4
                        WHEN LEFT(pld.class_fld, 1)='6' THEN 6
                        ELSE 9
                      END AS FUND_SOURCE 
                 FROM [DWPROD]..[DISC].[PS_LEDGER_DETAIL] pld
                WHERE pld.budget_period = '2010' 
                  AND pld.accounting_period BETWEEN 1 AND 12
                  AND pld.fiscal_year = 2010
                  AND pld.financial_deptid BETWEEN '100' AND '999'
                  AND pld.account BETWEEN '500000' AND '899999') x
    LEFT JOIN [Budgets].[dbo].[Departments] y ON y.deporg = x.financial_deptid    
     GROUP BY x.fiscal_year, y.strdepentity, y.depdiv1, x.deptid, x.fund_code, x.class_code, x.project_id, x.program_code, x.acct_grp_code
     ORDER BY x.financial_deptid, x.class_fld, x.project_id, x.account
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You cannot assign a DEFAULT value to a TINYTEXT and… May 13, 2026 at 8:09 pm
  • Editorial Team
    Editorial Team added an answer You can specify the error message in your DataAnnotations attribute.… May 13, 2026 at 8:09 pm
  • Editorial Team
    Editorial Team added an answer You should be able to do this with Retroweaver. May 13, 2026 at 8:09 pm

Related Questions

I'm using mod_rewrite to rewrite pretty URLs to a form supported by a Spring
Original Question: I am currently using Zend Framework with Zend_Db_* , and I am
In ASP.NET MVC you can return a redirect ActionResult quite easily: return RedirectToAction("Index"); or
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
The following query is pretty simple. It selects the last 20 records from a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.