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 6254469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:11:05+00:00 2026-05-24T14:11:05+00:00

I am in the process of rewriting an Access database and I have some

  • 0

I am in the process of rewriting an Access database and I have some awful MS Access queries that I need to rewrite. They basically consist of the following set up

SELECT *
FROM Query1, Query2, Query,...

These queries are used in 2 existing databases and typically consist of a Count/Sum of data. Then we use the totals for reporting. Most of the subqueries are like this or something similar

SELECT Count(Account), Reason, Sum(Amount)
FROM table1
GROUP BY Reason

This is easy enough to rewrite and that isn’t my problem. My problem is that I have about 20-30 of these queries that need to be rewritten. One of the requests that the users have now is to be have the option to get the Counts of the accounts as well as the List of accounts that make up the total. So now my 20-30 queries double because they want the lists as well.

So I am trying to determine the best way to design this to be able to provide the count or list for any particular day they want

I was thinking of creating a multi-statement table function to do this but I don’t know if that would be better than a stored proc or a view or anything else

I created the following function which allows them to search by any date and they can get either the list of accounts or the counts.

CREATE FUNCTION [udf_GeBreakdown]  
(
    @BusinessDate       datetime
    , @ListOfAccounts   bit
)
RETURNS @TableCount
TABLE 
(
    Account int, ReasonName varchar(50), Amount money
)
AS
BEGIN

    DECLARE @PreviousDate smalldatetime
    SET @PreviousDate = udf_GetNextImportDate(@BusinessDate, -1);

    IF @ListOfAccounts = 0
        BEGIN       
            INSERT INTO @TableCount
            SELECT Count(Account), R.ReasonName, Sum(Amount)
            FROM Debs.Resolved RS
            INNER JOIN Debs.Reason R
                ON RS.ReasonId = R.ReasonId
            WHERE RS.DebitDate = @PreviousDate
                AND RS.StatusId NOT IN (15, 17)
            GROUP BY R.ReasonName
        END
    ELSE
        BEGIN
            INSERT INTO @TableCount
            SELECT Account, R.ReasonName, Amount
            FROM Debs.Resolved RS
            INNER JOIN Debs.Reason R
                ON RS.ReasonId = R.ReasonId
            WHERE RS.DebitDate = @PreviousDate
                AND RS.StatusId NOT IN (15, 17)

        END

    RETURN 
END

I don’t know if this is a waste of time doing it this way or if I should just provide a list of accounts and then get my Count/Sum when I query this. I am looking for some help and or direction on how best to proceed with this.

  • 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-24T14:11:05+00:00Added an answer on May 24, 2026 at 2:11 pm

    Performance is going to be an issue in this multi-statement UDF.

    If you read from the system more than you write, I would try creating an indexed view similar to the following.

    CREATE VIEW V1 WITH SCHEMABINDING AS
    SELECT RS.DebitDate, Account, R.ReasonName, Amount
    FROM Debs.Resolved RS
    INNER JOIN Debs.Reason R
        ON RS.ReasonId = R.ReasonId
    WHERE RS.StatusId NOT IN (15, 17)
    
    CREATE INDEX X on V1 -- based on usage
    

    Then, use the following statement in a SP or single statement UDF to get the list of accounts. The statement becomes pretty trivial at that point. (you will have to do your previous day logic here…don’t store that function in the view)

    SELECT Count(Account), ReasonName, Sum(Amount)
    FROM V1 with(noexpand)
    GROUP BY DebitDate, ReasonName  -- or use DebitDate in the where clause
    

    I would still try this if your system is write intensive, however this indexed view will slow down inserts on the tables that are schemabound to it. Indexing views does come at a cost so you might be well served just creating a normal view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some part of my project I need a process-local scheduling system that will
I have a current process that exists in 2 MS Access databases that exports
We are in the process of rewriting a large legacy system that makes extensive
I am in the middle of rewriting an MS Access database and unfortunately we
I am rewriting an Access application that uses a lot of Temp tables. Meaning
I have a custom-built MVC PHP framework that I am in the process of
I am rewriting some old ASP applications that were not well structured and trying
So, my problem is this. I have a legacy MySQL database that I'm building
We are in the process of rewriting some of our websites and trying to
I'm maintaining an ancient RoR site that we're in the process of rewriting in

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.